2012-03-01 44 views
0

我需要回到前臺窗口,這是我的應用程序窗口前積極,我tryed使用user32.dll中這一點,但我無法找到一個窗口hendle。C#焦點返回到前一個窗口

[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] 
    internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount); 

    [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
    public static extern IntPtr GetWindow(IntPtr hwnd, uint wFlag); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
    public static extern IntPtr GetFocus(); 

    [DllImport("user32.dll")] 
    public static extern IntPtr GetForegroundWindow(); 

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

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    static extern bool PostMessage(IntPtr hWnd, int Msg, char wParam, int lParam); 

    [DllImport("user32")] 
    public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); 

    [DllImport("user32")] 
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern uint GetCurrentThreadId(); 
    ... 
     hMe = GetForegroundWindow(); 
     hNext = GetNextWindow(hMe, hw_next); 
     System.Text.StringBuilder window = new StringBuilder(32); 
     GetWindowText(hNext, window, 32); 

和我得到的只是「默認輸入法」,或在接下來的窗口中「M」,我怎麼能找到真正的應用程序的窗口?

我環路thrue窗口,找到我的記事本窗口:

0: D:\univer\C# 
1: 
2: 
3: 
4: 
5: 
6: Главное меню 
7: 
8: 
9: M 
10: Default IME 
11: 
12: 
13: 
14: CiceroUIWndFrame 
15: 
16: 
17: 
18: SysFader 
19: SysFader 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: SysFader 
36: 
37: 
38: HDMI Settings 
39: S/PDIF IN/OUT Settings 
40: Set Device Type 
41: Mixer ToolBox 
42: Параметры разъёма 
43: CiceroUIWndFrame 
44: TF_FloatingLangBar_WndTitle 
45: Syn Zoom Window 
46: Syn Visual Window 
47: 
48: 
49: Начать отладку (F5) 
50: M 
51: Default IME 
52: 
53: 
54: 
55: 
56: 
57: 
58: *new 2 - Notepad++ 

我有帶鉤的解決方案:

delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); 

    [DllImport("user32.dll")] 
    static extern bool UnhookWinEvent(IntPtr hWinEventHook); 

    [DllImport("user32.dll")] 
    static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); 


private static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) 
    { 
     uint id = 0; 
     if (eventType == EVENT_SYSTEM_FOREGROUND) 
     { 
      if (hwnd != _this.hKeyboard && hwnd != _this.hLast && hwnd != IntPtr.Zero) 
      { 
       _this.hLast = hwnd; 

      } 
     } 
    } 
... 
Form1_load(){ 
    _WinEvent = new WinEventDelegate(WinEventProc); 
    mHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _WinEvent, 0, 0, WINEVENT_OUTOFCONTEXT); 
} 
+0

http://stackoverflow.com/questions/621998/restoring-window-focus-back-to-previous-owner – zmbq 2012-03-01 15:21:33

+0

Parametri razbita的可能的複製! – 2012-03-01 15:57:00

+0

El:我看到你現在有了另一種解決方案,你能否把它作爲答案張貼並回復這個問題的改變?謝謝。 – Deanna 2013-03-19 14:49:51

回答

2

窗口的Z順序沒有直接關係到何時被激活或切換到/從。 得到以前的活動窗口的hWnd正確的方法是處理WM_ACTIVATE消息。之前的hWnd將以lParam的值傳遞。