Method of Realizing Singleton Pattern in C Window

  • 2021-07-18 08:52:15
  • OfStack

Mainly to meet this demand: the software is only allowed to start once.

Translate this problem into 1, which can be described as follows: For 1 software, after starting 1 process, it is not allowed to start other processes. If the program is opened for the second time, the window of the process that has been started will be displayed at the front end.

The C # winfrom application will first execute the code in program. cs after startup, so you need to start here. After starting, check whether there is a process with the same process name. If there is, mention the window of that process to the front end, and then close yourself.

Usage: Modify your program. cs to look like this:


 static class Program
  {
    //windows api, Used to display code 
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      // Core code 
      Process currProc = Process.GetCurrentProcess();
 
      Process[] runningProc = Process.GetProcesses();
      // Check the rules to see if the process names are the same. You can customize it flexibly, such as checking the user name. 
      var searchedProc=from a in runningProc
               where a.ProcessName == currProc.ProcessName
               select a;
 
      if (searchedProc.Count() > 1)
      {
        // Choose the same process name as the current process, but id The different process 
        Process firstProc = searchedProc.FirstOrDefault(a => a.Id != currProc.Id);
        IntPtr firstProcWindow = firstProc.MainWindowHandle;
        SetForegroundWindow(firstProcWindow);
        currProc.Kill();
      }
      //-------end---------
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }
  }

====================================================


Regarding the front-end display and top setting of the window, 1 involves three API of windows
//Display window
ShowWindow(hWnd, SW_NORMAL);

//Front-end display
SetForegroundWindow(hWnd);

//Top window
SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

Among them, this SetWindowPos is the most commonly used to set the position of the window. The most common use is to set the window to the top, which is equivalent to this. TopMost = true in winform;

Usage:

WinAPI: SetWindowPos-Changing the position and state of the window

SetWindowPos(
hWnd: HWND; {Window handle}
hWndInsertAfter: HWND; {Z order of windows}
X, Y: Integer; {Location}
cx, cy: Integer; {Size}
uFlags: UINT {Option}
): BOOL;

//hWndInsertAfter parameter optional values:
HWND_TOP = 0; {In front}
HWND_BOTTOM = 1; {At the back}
HWND_TOPMOST = HWND (-1); {In front, in front of any top window}
HWND_NOTOPMOST = HWND (-2); {In front, behind other top windows}

//uFlags parameter optional values:
SWP_NOSIZE = 1; {Ignore cx, cy, keep size}
SWP_NOMOVE = 2; {Ignore X, Y, do not change position}
SWP_NOZORDER = 4; {Ignore hWndInsertAfter, keep Z order}
SWP_NOREDRAW = 8; {Do not redraw}
SWP_NOACTIVATE = $10; {Not activated}
SWP_FRAMECHANGED = $20; {Force the WM_NCCALCSIZE message to be sent, 1 usually only when the size is changed}
SWP_SHOWWINDOW = $40; {Show Window}
SWP_HIDEWINDOW = $80; {Hide Window}

The above is the learning content of this article, and I hope everyone can like it.


Related articles: