ä»å¤©éå°ä¸ä¸ªéæ±ï¼å°±åMSNæ示信æ¯ä¸æ ·ï¼å¦æçªå£æªå½åï¼åééªçæéç¨æ·
å¥½ä¹ æ²¡å¨ç¬äºï¼åä¸åè®°ä¸æ¥å§ï¼å¿å¿
å¼ç¨ï¼http://www.codeproject.com/KB/dialog/Is_My_Application_on_Top_.aspx
以ä¸å¾æ¯ä»copeproject copyä¸æ¥çï¼ä»æ¤å¾å¯ä»¥çåºä¸ä¸ªæµç¨å§ï¼ä½æ¯ä½è ç¨çæ¯ä¸ä¸ªGetTopWindowçå½æ°ï¼æè§å¹¶ä¸æ£ç¡®ï¼å°½ç®¡ååºæ¥çæææ¯è·è®²çä¸æ ·ï¼ä½æ¯ããããæå ´è¶£çæåå¯ä»¥çä¸ä¸ï¼ä»¥ä¸æ¯ä»å¤ææ¯å¦å½åçªå£ä¸ºæ顶å±çå½æ°ï¼VBãNET代ç
Â
Private Function IsOnTop(ByVal hwnd As Integer) As Boolean
   Dim i As Integer = GetTopWindow(0)   'Initialize
   Dim x As Integer = 1
   Dim s As String
Â
   ' Enumeration
   Do  Â
        i = GetNextWindow(i, 2) ' Find next window in Z-order
       If i = hwnd Then
           Exit Do
       Else
           If i = 0 Then       ' Never find any window match the input handle
               Return False
           End If
       End If
Â
       If IsWindowVisible(i) = True Then
           s = Space(256)
           If GetWindowText(i, s, 255) <> 0 Then
           ' Very important to prevent confusing
           ' of BalloonTips and ContextMenuStrips
               x += 1
           End If
       End If
   Loop
Â
   ' x is Z-order number
Â
   If x = 1 Then
       Return True
   Else
       Return False
   End If
End Function
Â
ä½éè¿consoleæå°åºèªå·²å½åå¤äºæ顶å±çªå£å¥ææ¶ï¼ä¹æå°åºGetTopWindowè·å¾çå¥æï¼åç¨SPY++æ¥çï¼å¹¶ä¸ä¸è´ï¼é£éåºéå¢ï¼å¯¹è¿å½æ°çç解åºéäºä¹ï¼
Â
幸好忽éªè¿ä¸ä¸ªå½æ°ï¼
[DllImport("user32.dll")]
  private static extern IntPtr GetForegroundWindow();
åä¸å½åèªå·²å¤äºé¡¶å±ççªå£å¥ææ¯è¾äºä¸ä¸ï¼æ¯ä¸è´çï¼å°±ç´æ¥éç¨äº
Â
Â
æ¥äºä¸ä¸MSDN
GetForegroundWindow Function
The GetForegroundWindow function returns a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.
Â
GetTopWindow Function
The GetTopWindow function examines the Z order of the child windows associated with the specified parent window and retrieves a handle to the child window at the top of the Z order.
èªå·²è±è¯å¾èï¼ä½è¿gettopwindowçæææ¯è·ååçªå£ç顶å±çªå£çå¥æ,ä½ç½ä¸å«äººè¯´ï¼ä¼ 0è¿å»æ¯è·å¾WINDOWSå½å顶å±çªå£ï¼
Â
èéªçåæ¯éè¿flashwindowå½æ°ï¼è¿å¤§å¤C++ coderé½æ¸ æ¥çï¼å¿å¿ï¼å°±è½è·MSNä¸æ ·ï¼å½ç¨æ·å¹¶æªæä½å½åæ¶æ¯çªå£æ¶è½æ示
Â
顺便ååè¿ä¸ªä¾ä¸è¿ç¨ä¸æ¾ç¨è¿çå 个API
[DllImport("user32", EntryPoint = "GetTopWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetTopWindow(int hwnd);
[DllImport("user32", EntryPoint = "GetWindow", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetNextWindow(int hwnd, int wFlag);
[DllImport("user32", EntryPoint = "IsWindowVisible", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool IsWindowVisible(int hwnd);
[DllImport("user32", EntryPoint = "GetWindowTextA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GetWindowText(int hwnd, string lpString, int cch);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
Â
Â
ä¹è®°ä¸ä¸ä¸ç¯æç« ï¼å¾å¥½çä»ç»äºWINDOWSçªå£æºå¶ï¼æ¹å¤©å¥½å¥½éªè¯ä¸ä¸æ¯ä¸ªå½æ°
http://blog.csdn.net/chenshaoying/archive/2008/04/24/2324304.aspx
Â
å ¶å®ä¸é¢ä¸»è¦æ¶åå°çï¼è¿æ¯ä¸ä¸ªZè½´çé®é¢ï¼èçWIN32 coderæ¯å¾çå¦ï¼éè¿Zè½´ï¼è½ååºä¸äºå¾æ¼äº®çææå¦ï¼æææ¾çå°±æ¯ä¸ä¸ªOUTLOOKçæ§ä»¶ï¼codeprojectä¸æ好å 个ä»ç»çï¼ä¸ä¸ªæ¯ä½è èªå·²åçï¼å¥½é¿å¥½é¿ç代ç åï¼å¦ä¸ä¸ªï¼åæ¯ä½è éè¿è®¾ç½®Zè½´è¾¾å°çææï¼å¾å¥½å¾å¼ºå¤§
Â
Â
以ä¸æ¯å ¶è®¾ç½®Zè½´çä¸æ®µä»£ç
        void ButtonClick(object sender, System.EventArgs e)
        {
             // Get the clicked button...
             Button clickedButton = (Button)sender;  Â
             // ... and it's tabindex
             int clickedButtonTabIndex = clickedButton.TabIndex;   Â
            Â
             // Send each button to top or bottom as appropriate
             foreach (Control ctl in panel1.Controls)
             {
                  if (ctl is Button)
                  {
                      Button btn = (Button)ctl;
                      if (btn.TabIndex > clickedButtonTabIndex)
                      {
                           if (btn.Dock != DockStyle.Bottom)
                           {
                                btn.Dock = DockStyle.Bottom;
                                // This is vital to preserve the correct order
                                btn.BringToFront();            Â
                                                 }
                      }
                      else
                      {
                           if (btn.Dock != DockStyle.Top)
                           {
                                btn.Dock = DockStyle.Top;
                                // This is vital to preserve the correct order
                                btn.BringToFront();
                                                 }
                      }
                  }
             }
            Â
             // Determine which button was clicked.
             switch (clickedButton.Text)
             {
                  case "Cars":
                      CreateCarList();
                      break;
                     Â
                  case "Outlook Shortcuts":
                      CreateOutlookList();
                      break;
                     Â
                  case "Zip Files":
                      CreateZipList();
                      break;
             }
                    listView1.BringToFront(); // Without this, the buttons will hide the items.
            Â
        }
Â
å¾ç®åå°è®¾ç½®ä¸ä¸ªZ轴就达å°OUTLookææäºï¼ç»èªå·²æ å¿ä¸ä¸ï¼å æ²¹
Â
å«åè¯æé£ç§VSæææ¯æä¹ååºæ¥çå¦ï¼é£è¦è¯´çè¯å¯ä»¥å好é¿å¥½é¿çä¸ç¯æç« äºï¼CODEPROJECTä¸å°±æä¸ä¸ªå¼æºçæ§ä»¶åï¼å¿å¿ï¼å¾ä½©æä½è çç²¾ç¥ï¼åæä¸å»ããããããããã
Â
转载äº:https://www.cnblogs.com/yellowyu/archive/2008/09/03/1282990.html