2011-04-19 59 views
3

如何使用用戶32 .dll處理特定窗口?我在C#工作。謝謝。有人能給我一個簡短的例子嗎?:)我會感激! (方法:getwindowtext,show window,enumwindowcallback normal)。c#如何使用user32 dll處理特定窗口

回答

6

嘗試以下,

// For Windows Mobile, replace user32.dll with coredll.dll 
[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter. 

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)] 
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

// You can also call FindWindow(default(string), lpWindowName) or FindWindow((string)null, lpWindowName) 

編輯: 您可以使用這些聲明如下

// Find window by Caption 
     public static IntPtr FindWindow(string windowName) 
     { 
      var hWnd = FindWindow(windowName, null); 
      return hWnd; 
     } 

編輯:2 下面是代碼的簡明版本:

public class WindowFinder 
    { 
     // For Windows Mobile, replace user32.dll with coredll.dll 
     [DllImport("user32.dll", SetLastError = true)] 
     static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

     public static IntPtr FindWindow(string caption) 
     { 
      return FindWindow(String.Empty, caption); 
     } 

    } 
+0

並從主要如何訪問此方法? – Alex 2011-04-19 07:27:19

+0

剛剛看到我的EDIT – crypted 2011-04-19 07:30:57

+0

,我把這個方法稱爲:FindWindow(「notepad」)例如? – Alex 2011-04-19 07:35:57

相關問題