首页 > 编程 > C# > 正文

C#窗口实现单例模式的方法

2020-01-24 01:34:54
字体:
来源:转载
供稿:网友

主要是应对这种需求:软件只允许启动一次。

将这个问题转化一下,可以这样描述:对于一个软件,在启动一个进程之后,不允许启动其它进程,如果第二次打开程序,就把已经启动的那个进程的窗口放到最前端显示。

C# winfrom应用在启动之后会首先执行program.cs里的代码,所以需要在这里下手。启动后,检测是否有相同进程名的进程,如果有,就把那个进程的窗口提到前端,然后关闭自己。

用法:把你的program.cs改造成这个样子:

 static class Program  {    //windows api,用于显示代码    [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()    {      //核心代码      Process currProc = Process.GetCurrentProcess();       Process[] runningProc = Process.GetProcesses();      //检查规则,看进程名是否相同。可以自己灵活定制,比如需要检查用户名之类的。      var searchedProc=from a in runningProc               where a.ProcessName == currProc.ProcessName               select a;       if (searchedProc.Count() > 1)      {        //选出和当前进程进程名相同,但是id不同的那个进程        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());    }  }

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


关于窗口前端显示和置顶,一共涉及到3个windows的API
   //显示窗口
   ShowWindow(hWnd, SW_NORMAL);

   //前端显示
   SetForegroundWindow(hWnd);

   //窗口置顶
   SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

其中这个SetWindowPos最常用,用于设定窗口的位置,最常见用途是给窗口设定为置顶,等同于winform里的this.TopMost=true;

用法:

WinAPI: SetWindowPos - 改变窗口的位置与状态

SetWindowPos(
hWnd: HWND; {窗口句柄}
hWndInsertAfter: HWND; {窗口的 Z 顺序}
X, Y: Integer; {位置}
cx, cy: Integer; {大小}
uFlags: UINT {选项}
): BOOL;

//hWndInsertAfter 参数可选值:
HWND_TOP = 0; {在前面}
HWND_BOTTOM = 1; {在后面}
HWND_TOPMOST = HWND(-1); {在前面, 位于任何顶部窗口的前面}
HWND_NOTOPMOST = HWND(-2); {在前面, 位于其他顶部窗口的后面}

//uFlags 参数可选值:
SWP_NOSIZE = 1; {忽略 cx、cy, 保持大小}
SWP_NOMOVE = 2; {忽略 X、Y, 不改变位置}
SWP_NOZORDER = 4; {忽略 hWndInsertAfter, 保持 Z 顺序}
SWP_NOREDRAW = 8; {不重绘}
SWP_NOACTIVATE = $10; {不激活}
SWP_FRAMECHANGED = $20; {强制发送 WM_NCCALCSIZE 消息, 一般只是在改变大小时才发送此消息}
SWP_SHOWWINDOW = $40; {显示窗口}
SWP_HIDEWINDOW = $80; {隐藏窗口}

以上就是本文的学习内容,希望大家可以喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表