分享

同时只有一个窗体实例,再次打开激活已打开窗体的范例

 goodwangLib 2014-03-25
[c-sharp] view plaincopy
  1. using System;  
  2. using System.Diagnostics;  
  3. using System.Runtime.InteropServices;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace CSharpWin08  
  7. {  
  8.     static class Program  
  9.     {  
  10.         [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]  
  11.         public static extern int SetForegroundWindow(IntPtr hwnd);  
  12.         [DllImport("user32.dll", EntryPoint = "SendMessage")]  
  13.         public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);  
  14.         public const int WM_SYSCOMMAND = 0x112;  
  15.         public const int SC_RESTORE = 0xF120;  
  16.         /// <summary>  
  17.         /// 应用程序的主入口点。  
  18.         /// </summary>  
  19.         [STAThread]  
  20.         static void Main()  
  21.         {  
  22.             Process cur = Process.GetCurrentProcess();  
  23.             foreach (Process p in Process.GetProcesses())  
  24.             {  
  25.                 if (p.Id == cur.Id) continue;  
  26.                 if (p.ProcessName == cur.ProcessName)  
  27.                 {  
  28.                     SetForegroundWindow(p.MainWindowHandle);  
  29.                     SendMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0);  
  30.                     return;  
  31.                 }  
  32.             }  
  33.             Application.Run(new Form2());  
  34.         }  
  35.     }  
  36. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多