C# 获得窗口句柄??

juso2011 2012-01-20 03:12:07

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//获取主窗口句柄
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "D:\\uu.exe"
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();

IntPtr hWnd1 = FindWindow("Afx:00400000:8:00010011:00000000:03F10A01", "μVision4");

我先打开需要控制的软件,用spy获得窗体类名和窗口名,然后再关闭,运行程序,为什么调试下来,句柄hwnd1总为0??求高手,而且发现窗体类名每次都不一样
...全文
802 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
事理 2012-01-28
  • 打赏
  • 举报
回复
其实有这种写法,不过有窗口
IntPtr hWnd1 = Process.Start("D:\\uu.exe");

IntPtr hWnd1 = FindWindow("Afx:00400000:8:00010011:00000000:03F10A01", "μVision4");
如果μVision4是子窗口,不是主窗口用FindWindowEx

还有种可能是程序有自我保护,像qq那样不能获取某些窗口句柄
feixiang993 2012-01-28
  • 打赏
  • 举报
回复
窗体类内部:this.Handle
窗体类外部:窗体对象.Handle
wy811007 2012-01-27
  • 打赏
  • 举报
回复
可以只用类名 或这用标题做查找的 另一个null 就好了
threenewbee 2012-01-27
  • 打赏
  • 举报
回复
可能是那个 u 特殊字符造成的。

你可以用 EnumWindows API 枚举所有窗口,再找出你要的那个。
wangyue4 2012-01-23
  • 打赏
  • 举报
回复
窗体类内部:this.Handle
窗体类外部:窗体对象.Handle
aa6103848252 2012-01-21
  • 打赏
  • 举报
回复
myProcess.Start();
启动后窗体不一定已经创建好,所以需要Sleep一会
"Afx:00400000:8:00010011:00000000:03F10A01", "μVision4"
你确定没错?
mrsupersky 2012-01-21
  • 打赏
  • 举报
回复
调用API的时候建议只保留窗体名

,还有
myProcess.StartInfo.CreateNoWindow = true;
为啥是true 是不是应该是False
蔡袅 2012-01-20
  • 打赏
  • 举报
回复
Returns the top-level CWnd whose window class is given by lpszClassName and whose window name, or title, is given by lpszWindowName.


static CWnd* PASCAL FindWindow(
LPCTSTR lpszClassName,
LPCTSTR lpszWindowName
);


Parameters
lpszClassName
Points to a null-terminated string that specifies the window's class name (a WNDCLASS structure). If lpClassName is NULL, all class names match.

lpszWindowName
Points to a null-terminated string that specifies the window name (the window's title). If lpWindowName is NULL, all window names match.

Return Value
Identifies the window that has the specified class name and window name. It is NULL if no such window is found.

The CWnd* may be temporary and should not be stored for later use.

110,476

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧