如何调用FindWindow API获取主窗体

wonderful_abc 2012-05-29 03:59:50
由于主窗体的ShowInTaskBar = false,所以不得不使用FindWindow来获取,IntPtr FindWindow(string lpClassName, string lpWindowName),但是主窗体的标题lpWindowName可能会变动,所以想通过窗体的类名lpClassName来获取,而我的主窗体是C#写的程序,这个lpClassName应该不会是主窗体的类全名(Namespace.ClassName)字符串....可是百度和谷歌了大半天,没有相关的例子.....苦闷中,哪位大侠能拔刀相助,先谢了!
...全文
443 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wonderful_abc 2012-05-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
1、通过程序名获取该进程Process.GetProcess();
2、获取主界面窗口句柄Process.MainWindowHandle
[/Quote]
看清楚我说的“ShowInTaskBar=false”,所以采用System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle永远都是IntPtr=IntPtr.Zero,是无法获得主窗口的句柄的!
showlie 2012-05-30
  • 打赏
  • 举报
回复
1、通过程序名获取该进程Process.GetProcess();
2、获取主界面窗口句柄Process.MainWindowHandle
wonderful_abc 2012-05-30
  • 打赏
  • 举报
回复
我其实就是想知道FindWindow中的类名lpClassName如何得到?
大狗狗 2012-05-29
  • 打赏
  • 举报
回复
下面是用来查找IE窗口句柄的代码,希望对你有帮助:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace HtmlShow
{
/**/
/// <summary>
/// This class is to find the given window's child window accroding to the given child window's name.
/// The useage: FindWindow fw = new FindWindow(wndHandle, "ChildwndClassName"); IntPtr ip = fw.FoundHandle;
/// I adapt the code from Paul DiLascia,who is the MSDN Magazine's writer.
/// The original class is named CFindWnd which is written in C++, and you could get it on Internet.
/// www.pinvoke.net is a great website.It includes almost all the API fuctoin to be used in C#.
/// </summary>
class FindWindow
{
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
//IMPORTANT : LPARAM must be a pointer (InterPtr) in VS2005, otherwise an exception will be thrown
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
//the callback function for the EnumChildWindows
private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

//if found return the handle , otherwise return IntPtr.Zero
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

private string m_classname; // class name to look for

private IntPtr m_hWnd; // HWND if found
public IntPtr FoundHandle
{
get { return m_hWnd; }
}
// ctor does the work--just instantiate and go
public FindWindow(IntPtr hwndParent, string classname)
{
m_hWnd = IntPtr.Zero;
m_classname = classname;
FindChildClassHwnd(hwndParent, IntPtr.Zero);
}

//EnumChildWindows是API函数,能够遍历主窗口下所有子窗口。不过它的遍历过程是通过
//第二个参数即回调函数与程序员交互的。EnumChildWindows每找到一个窗口。就调用回调
//函数。回调函数如果返回false。遍历就会结束。

/**/
/// <summary>
/// Find the child window, if found m_classname will be assigned
/// </summary>
/// <param name="hwndParent">parent's handle</param>
/// <param name="lParam">the application value, nonuse</param>
/// <returns>found or not found</returns>
//The C++ code is that lParam is the instance of FindWindow class , if found assign the instance's m_hWnd
private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
{
EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd);
IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, this.m_classname, string.Empty);
if (hwnd != IntPtr.Zero)
{
this.m_hWnd = hwnd; // found: save it
return false; // stop enumerating
}
EnumChildWindows(hwndParent, childProc, IntPtr.Zero); // recurse redo FindChildClassHwnd
return true;// keep looking
}
}
}

//this.Handle是要查找窗口的父窗口,因为我使用的是webBrowser控件
FindWindow fw = new FindWindow(this.Handle, "Internet Explorer_Server");
hIE = fw.FoundHandle;
glglgl2015 2012-05-29
  • 打赏
  • 举报
回复
this.dgvCustomer(这是方法是某控件名称).FindForm(); // 这个方法可以获取某控件所在窗体上的 的From ,不知道是否能回答你的问题

glglgl2015 2012-05-29
  • 打赏
  • 举报
回复
你找到某个控件的主窗体??
wonderful_abc 2012-05-29
  • 打赏
  • 举报
回复
主窗体是C#写的,可不一定是我写的,而且还不一定在同一个程序集中呢....
ParanoidKing 2012-05-29
  • 打赏
  • 举报
回复
为什么不能呢?你都说的主窗体是C#写的了。
wonderful_abc 2012-05-29
  • 打赏
  • 举报
回复
2楼和3楼无法理解偶的痛苦....
如果能够用“this.Handle”,就说明已经得到窗体的对象了,还需要这么麻烦么?
ParanoidKing 2012-05-29
  • 打赏
  • 举报
回复
为什么一定要用FindWindow呢?this.Handle不就能拿到窗体的句柄了吗?lpClassName不是那个名字。
请叫我卷福 2012-05-29
  • 打赏
  • 举报
回复
用Spy++就可以查看到了
类似 WindowsForms10.Window.8.app.0.378734a

110,534

社区成员

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

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

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