Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am doing an application in c# and in my application I am opening some exe file like MS word, calculator so when I open the application and then I hide it I would like to create a sort of shortcut in the app so that when I click on the shotcut the window will open again.

What I have tried:

What I did is that I was able to put the window of the exe file on topMost but when it is hidden or resized I would like to create a shortcut in the application on C# so that when I click on the shorcut the exe file will reopen.
Thank you.
Posted
Updated 11-Mar-19 5:10am

Can you try below. I have now tested it and it is working fine.

C#
[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

public static void ShowToFront(string windowName)
{
    try
    {
        IntPtr firstInstance = FindWindow(null, windowName);
        ShowWindow(firstInstance, 1);
        SetForegroundWindow(firstInstance);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}


You have to call the function with the full name of the application that you want to restore. (exactly as seen on the windows task manager -> application tab). You can get the name from process.MainWindowTitle. Check the below event..

C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                Process[] Processes = Process.GetProcessesByName("winword");
                
                foreach (Process p in Processes)
                {
                    ShowToFront(p.MainWindowTitle);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
 
Share this answer
 
Comments
TatsuSheva 5-Sep-16 10:18am    
I need to check if the window is minimized , so a button is created and then when we click on the button the window will restore.
TatsuSheva 5-Sep-16 11:06am    
What you gave me is the correct one but how I can check if the window is minimized ? Because I want to create a button dynamically when the window is minimized.
NaibedyaKar 5-Sep-16 14:36pm    
Sorry for the late response, I was busy on something else.
You can use IsIconic(instance) to know whether the application is minimised or not. Let me show the whole code as a separate answer.
Here is how you can check whether the window is minimised and then restore it.

C#
private void button1_Click(object sender, EventArgs e)
{
    try
    {
        var processes = Process.GetProcessesByName("winword");
        foreach (var p in processes)
        {
            MaximizeWindowIfMinized(p.MainWindowTitle);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);

public static void MaximizeWindowIfMinized(string windowName)
{
    try
    {
        var instance = FindWindow(null, windowName);

        if (IsIconic(instance))
        {
            MessageBox.Show(@"Window is minimized");

            ShowWindow(instance, 1);
            SetForegroundWindow(instance);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
 
Share this answer
 
Comments
[no name] 5-Sep-16 16:42pm    
Good grief. How many times do you have to answer the same question? Those worthless internet points must really mean something to you.
TatsuSheva 6-Sep-16 4:26am    
I have to put it on a timer ?
TatsuSheva 6-Sep-16 4:39am    
When I compile nothing happen even if I put it on a timer.
Hey guys, i've tried something like this... and the problem was that I had no control over the overall System Wide applications.... when I tried using the following:

<pre>           List<Form> oForms = new List<Form>();
                
                foreach (Form form in Application.OpenForms)
                {
                    MessageBox.Show("form.text");
                    MessageBox.Show(form.Text);
                    oForms.Add(form);
                }
                foreach (var form in oForms)
                {
                    //MessageBox.Show("form.text");
                    //MessageBox.Show(form.Text);
                    form.Hide();
                }


the "Application.OpenForms" was always just something that applied to the application I was working on... It wouldn't cover anything else that was open on the screen like "CALC", "NOTEPAD", "IEXPLORE", etc... I tried several things and it would only display the title of my application in the 'commented-out' MessageBoxes... Is there maybe another C# 'Global' type variable I should be using instead...?

Thanks in advance guys,
 
Share this answer
 
Comments
Richard MacCutchan 11-Mar-19 11:17am    
This is not an answer to the above question. If you have a problem then please open a new question.

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