Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I did this code and I put in a timer in order to check if the exe is open or closed.
I create a class and put it inside and then call it after in Winforms.
Here is the code:

C#
private static WINDOWPLACEMENT GetPlacement(IntPtr hwnd)
        {
            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
            placement.length = Marshal.SizeOf(placement);
            GetWindowPlacement(hwnd, ref placement);
            return placement;
        }

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetWindowPlacement(
            IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
        internal enum ShowWindowCommands : int
        {
            Hide = 0,
            Normal = 1,
            Minimized = 2,
            Maximized = 3,
        }

        [Serializable]
        [StructLayout(LayoutKind.Sequential)]
        internal struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public ShowWindowCommands showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        public static void MaximizeWindowIfMinized(string windowName,Timer tim,FlowLayoutPanel f1)
        {
            tim.Start();
            Button btn = new Button();
            Process[] procs = Process.GetProcesses();
            try
            {


                foreach (Process proc in procs)
                {
                    if (proc.ProcessName.Contains(windowName))
                    {
                        var placement = GetPlacement(proc.MainWindowHandle);
                        Console.WriteLine(placement.showCmd.ToString());

                        if (placement.showCmd == ShowWindowCommands.Normal)
                        {

                            btn = new Button();
                          //  btn.Width = 150;
                          //  btn.Height = 120;
                            f1.Controls.Add(btn);
                         //   btn.Left = 500;                           
                          //  btn.Visible = true;
                            // btn.Click += button1_Click(windowName);
                            tim.Stop();
                        }
                        tim.Start();

                         if (placement.showCmd == ShowWindowCommands.Hide)
                        {
                            btn.Visible = false;
                            tim.Stop();

                        }

                    }
                }
            }
            catch
            {

            }

        }

Here, the problem is that it is looping in the timer and I don't know how to control it...
At each time an app is clicked the button will be created and if the app is closed the button must disappear.

Thank you.

What I have tried:

My idea is to create a button only if one does not already exist for the process in question. I may do that by associating the process ID (or perhaps Process to the button (via Tag property) and then searching the buttons for each process. I don't want to have duplicates. That will at least resolve the replicating button issue. What do you think ?
Posted
Updated 6-Sep-16 11:21am
Comments
Philippe Mori 6-Sep-16 13:33pm    
It is hard to understand what you want to do and the code does not make any sense as it would break for sure if 2 windows have the same name...
TatsuSheva 6-Sep-16 13:41pm    
it is simple, in my application I want to open exe applications like Notepad etc, so when the notepad is open, a button is created and when the notepad is closed the button disappear simply and when we click on the button if the window of the notepad is open it will be minmized and whenb the window is minimized it will be restored.
Philippe Mori 6-Sep-16 15:30pm    
Windows already put button for opened application in the taskbar...
johannesnestler 8-Sep-16 8:25am    
I don't get what you do with the timer here? start-stop inside a Loop? Can you please explain us what you want to do - because for me your (timer-) code makes no sense... you mentioned (other comments) that your problem is with this timer, but then you say that dublicate Windows are open (another problem, you didn't write correct code for this, other solutions already showed you)...
johannesnestler 8-Sep-16 8:27am    
@ idea with process-id: this ID is unique for every process - so 10 instances of Notepad = 10 different process id's..

1 solution

When an executable is open in the system... there will be a process with the exact same name as the file. For example if you are looking for an exe file which is called: SystemCleaner.exe you can find it as a process with the name: SystemCleaner.
Accordingly if the file name has a space, for example: System Cleaner.exe Then you will looking for a process called: System Cleaner.

You can count how many processes with a specific name exist in the system.. with the following lines of code(in C#):

C#
int counter = 0;
        foreach (Process process in Process.GetProcessesByName("SystemCleaner"))
        {
            counter++;
        }
	if(counter > 0)
          {
             YourButtonName.Visible = true;
          }
          else
          {
             YourButtonName.Visible = false;
          }
 
Share this answer
 
v6
Comments
TatsuSheva 7-Sep-16 5:32am    
Hello, my aim is my c# application is when I an exe file is open then a button is created on my winform and when the window of the exe is closed the button disappear. the function of the button will be if we click on the button the window is minimized then the window will be restored.
xXxRevolutionxXx 7-Sep-16 5:38am    
Man, are you kidding me ? Put an effort on your own. Anyway, i edited the answer. No one can write you something more clearly than that. I cannot write all the other parts of the code. But i just showed you how to check if an app is open or closed.
TatsuSheva 7-Sep-16 6:26am    
I know what you did but my problem is not here... My code works perfectly what it is not working is that it is in a timer and when I open an exe buttons are duplicated here is my problem I need to create only one button for one app , I hope you understood my problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900