展开全部
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 获取有名字窗体句柄
/// </summary>
/// <param name="caption">窗体名</param>
/// <param name="delay">循环查询次数</param>
/// <param name="maxTries">停顿时间</param>
/// <returns>句柄 IntPtr</returns>
static IntPtr FindMainWindowHandle(String caption,int delay,int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool foundWindow = false;
int attempts = 0;
do
{
mwh = FindWindow(null, caption);
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found\n");
Thread.Sleep(maxTries);
++attempts;
}
else
{
Console.WriteLine("Form has been found");
foundWindow = true;
}
}
while(!foundWindow && attempts<delay);
if(mwh != IntPtr.Zero)
return mwh;
else
throw new Exception("Coule not find Main Window");
//调用FindMainWindowHandle()方法以获取窗体句柄,例:
IntPtr wHandle= FindMainWindowHandle("窗体名",10,100);
Console.WriteLine("窗体句柄: " + wHandle);
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 获取有名字窗体句柄
/// </summary>
/// <param name="caption">窗体名</param>
/// <param name="delay">循环查询次数</param>
/// <param name="maxTries">停顿时间</param>
/// <returns>句柄 IntPtr</returns>
static IntPtr FindMainWindowHandle(String caption,int delay,int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool foundWindow = false;
int attempts = 0;
do
{
mwh = FindWindow(null, caption);
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found\n");
Thread.Sleep(maxTries);
++attempts;
}
else
{
Console.WriteLine("Form has been found");
foundWindow = true;
}
}
while(!foundWindow && attempts<delay);
if(mwh != IntPtr.Zero)
return mwh;
else
throw new Exception("Coule not find Main Window");
//调用FindMainWindowHandle()方法以获取窗体句柄,例:
IntPtr wHandle= FindMainWindowHandle("窗体名",10,100);
Console.WriteLine("窗体句柄: " + wHandle);
展开全部
每一个从Cwnd类派生出来的类都有一个成员变量m_hWnd,即指向当前窗口的句柄,直接使用它即可。
其中从CWnd类派生出来的类有CFrameWnd、CDialog、CView、CControlBar等等。
其中从CWnd类派生出来的类有CFrameWnd、CDialog、CView、CControlBar等等。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
HWND hWnd;
hWnd = ::FindWindow("窗口类名如:Dialog" , "要得到窗口的标题");
hWnd = ::FindWindow("窗口类名如:Dialog" , "要得到窗口的标题");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
GetSafeHwnd();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询