Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5849397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:59:37+00:00 2026-05-22T12:59:37+00:00

I have used a mutex to run a single instance program, and now I

  • 0

I have used a mutex to run a single instance program, and now I want the window to become maximized if it is currently minimized when the user reopens the application.

Here is the code I currently have in my Program.cs file:

static class Program
{
    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool Ok = true;
        string ProductName = Application.ProductName;
        Mutex m = new Mutex(true, ProductName, out Ok);
        if (!Ok)
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName(ProductName);
            SetForegroundWindow(p[0].MainWindowHandle);

    }
    else
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:59:38+00:00Added an answer on May 22, 2026 at 12:59 pm

    You’re looking for the ShowWindow function and the SW_MAXIMIZE flag.

    In C#, the P/Invoke declaration would look like this:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
    private const int SW_MAXIMIZE = 3;
    

    Add it to your code here:

    if (!Ok)
    {
       Process[] p = Process.GetProcessesByName(ProductName);
       SetForegroundWindow(p[0].MainWindowHandle);
       ShowWindow(p[0].MainWindowHandle, SW_MAXIMIZE);
    }
    

    If you actually want to test whether the window is minimized first before you maximize it, you can use the old-school IsIconic function:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsIconic(IntPtr hWnd);
    
    // [...]
    
    if (!Ok)
    {
       Process[] p = Process.GetProcessesByName(ProductName);
       IntPtr hwndMain= p[0].MainWindowHandle;
       SetForegroundWindow(hwndMain);
    
       if (IsIconic(hwndMain))
       {
          ShowWindow(hwndMain, SW_MAXIMIZE);
       }
    }
    

    If you just want to activate the window (rather than maximize it), use the SW_SHOW value (5) instead of SW_MAXIMIZE. This will restore it to its previous state, before it was minimized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used jquery calendar, and I have used to create event details, now
If I have a module that has a mutex and I write the value
I have a test program that I wrote to try and debug a GMutex
I have used FCKeditor in my project. It worked fine before i add url
I have used some TCL, but this construction stumps me. When $res = Table
I have used date picker in my iPad App but the output of date
I have a class called 'subscribedQueue'. This class receives its data via its subscribed
I have a leak in my application and I've come to reduce my code
OS: Windows Language: C/C++ The design demands to use a mutex variable across process
Hello I built a hash table in C. I have unit tested it enough

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.