日期:2014-05-18  浏览次数:21011 次

C# 怎么通过句柄获取窗体对象。求求大家了。求了很久,帮忙吧
我想通过句柄获取窗体对象,从而获取里面的值,或更改。
网上说 [DllImport("user32.dll", EntryPoint = "FindWindow")]
  public static extern IntPtr FindWindow(string a, string b);
  [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
  public static extern IntPtr FindWindowEx(IntPtr aa, IntPtr bb, string a, string b);
  [DllImport("user32.dll", EntryPoint = "SendMessage")]
  public static extern int SendMessage(IntPtr aa, int bb, IntPtr a, string b);


这几个可以。我主要对FindWindowEx的第三个参数不太明白,请解释下,最好能帮我写个代码,谢谢了。

------解决方案--------------------
类名,windows api 程序创建窗口前,要调用RegisterClassEx注册窗口类,用于处理窗口的消息,这时候指定的类名。

纤细解释

lpszClass [in, optional]
Type: LPCTSTR
The class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be placed in the low-order word of lpszClass; the high-order word must be zero.
If lpszClass is 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, or it can be MAKEINTATOM(0x8000). In this latter case, 0x8000 is the atom for a menu class. For more information, see the Remarks section of this topic.
------解决方案--------------------
例子:

private IntPtr GetHandleToHorizontalScrollBar(Control parent)
{
// Locals
IntPtr childHandle;
string appDomainHexedHash;

// Get the hexadecimal value of AppDomain hash code.
// This value is dynamically appended to the window class name of the child window
// for .NET Windows Forms. This name is viewable via the Spy++ tool.
appDomainHexedHash = AppDomain.CurrentDomain.GetHashCode().ToString("x");

// Find window handle
childHandle = FindWindowEx(
parent.Handle, // Parent handle
IntPtr.Zero, // Child window after which to seek
"WindowsForms10.SCROLLBAR.app.0." + appDomainHexedHash, // Class name to seek (viewable in the Spy++ tool)
IntPtr.Zero); // Window title to seek

// Return handle
return childHandle;
}

来自:
http://pinvoke.net/default.aspx/user32/FindWindowEx.html