C#中使用FindWindow函數詳解 從標題擷取控制代碼

來源:互聯網
上載者:User

FindWindow 用來根據類名和視窗名來得到視窗控制代碼的。但是這個函數不能尋找子視窗,也不區分大小寫。

如果要從一個視窗的子視窗中尋找需要使用FindWindowEX。

1.在C#中使用方法如下:

[DllImport("User32.dll", EntryPoint = "FindWindow")]   
        private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);   
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]   
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName); 
[DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

2. 執行個體參考:

IntPtr hWnd = FindWindow(null, "test Demo");

這樣會尋找所有title是"test Demo"的視窗。

參考下面的資料解釋

3. FindWindow參數詳解:

Parameters

lpClassName
[in] Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero. 
If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.

If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.

lpWindowName
[in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match. 
Return Value

If the function succeeds, the return value is a handle to the window that has the specified class name and window name.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

If the lpWindowName parameter is not NULL, FindWindow calls the GetWindowText function to retrieve the window name for comparison. For a description of a potential problem that can arise, see the Remarks for GetWindowText.

To check if the Microsoft IntelliType version 1.x software is running, call FindWindow as follows:

Copy Code
FindWindow("MSITPro::EventQueue",NULL);   To check if the IntelliType version 2.0 software is running, call FindWindow as follows:

Copy Code
FindWindow("Type32_Main_Window", NULL);   If the IntelliType software is running, it sends WM_APPCOMMAND messages to the application. Otherwise the application must install a hook to receive WM_APPCOMMAND messages.

Microsoft Windows 95 or later: FindWindowW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example

For an example, see Retrieving the Number of Mouse Wheel Scroll Lines.

Function Information

Minimum DLL Version user32.dll 
Header Declared in Winuser.h, include Windows.h 
Import library User32.lib 
Minimum operating systems Windows 95, Windows NT 3.1 
Unicode Implemented as ANSI and Unicode versions. 
本文來自:http://blog.csdn.net/soief/archive/2009/04/20/4094476.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.