VC 通过进程ID获得主窗口句柄

VC 通过进程ID获得主窗口句柄,第1张

通过进程名称结束 进程

BOOL FindProcessHandleAndKill(const CString &cs_WndName)

{

HWND hWnd;

DWORD ProcessID;

HANDLE hProcess;

hWnd=::FindWindow(NULL, cs_WndName);

if(hWnd==NULL)//テサモミユメオスエーソレ

{

return FALSE;

}

else

{

if(GetWindowThreadProcessId(hWnd, &ProcessID)==0)//ハァーワ

{

return FALSE;

}

else

{

hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,ProcessID);

if(hProcess==NULL)

{

return FALSE;

}

else

{

if(TerminateProcess(hProcess, 0))

{

return TRUE;

}

else

{

return FALSE;

}

}

}

}

}

下面是MFC中获取各种指针的方式, 自己找去吧

在CWinApp中

AfxGetMainWnd()

m_pMainWnd

AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()

SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()

SDI:AfxGetMainWnd()->GetActiveView()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()

在CMainFrame中 AfxGetApp()

theApp

MDIGetActive()

GetActiveFrame()

SDI:GetActiveView()->GetDocument()

MDI:MDIGetActive()->GetActiveView()->GetDocument() SDI:GetActiveView()

MDI:MDIGetActive()->GetActiveView()

在CChildFrame中 AfxGetApp()

theApp

GetParentFrame()

GetActiveView()->GetDocument() GetActiveView()

在CDocument中 AfxGetApp()

theApp

AfxGetMainWnd() AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()

POSITION pos = GetFirstViewPosition();GetNextView(pos)

在CView中 AfxGetApp()

theApp

AfxGetMainWnd() GetParentFrame() GetDocument()

在其他类中 AfxGetApp()

AfxGetMainWnd() AfxGetMainWnd()->MDIGetActive()

AfxGetMainWnd()->GetActiveFrame()

SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()

SDI:AfxGetMainWnd()->GetActiveView()

MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()

强制转换是肯定不行的,我想了个笨方法。在获得某进程的Id后,获取桌面上每个窗口的进程Id,再与先获得的进程Id进行比较,然后就能知道哪个窗口属于哪个进程的了。

另外网上找的一篇文章:>

包含图标的窗口实际上是DesktopWindow的一个字窗口。确切地讲,Desktop Window包含一个无标题的、类名为“SHELLDLL_DefView”的子窗口,这个字窗口又包含一个无标题的、类名为“SysListView32”的子窗口——这才是那个真正包含桌面图标的窗口。 // HWN[VC++]如何得到桌面窗口的句柄(桌面图标)

[DllImport("user32dll", EntryPoint = "FindWindow", CharSet = CharSetAuto)]

static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

[DllImport("user32dll", EntryPoint = "FindWindowEx", CharSet = CharSetAuto)]

extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[STAThread]

static void Main(string[] args)

{

string path = "\\\\\\AUT3\\bin\\Debug\\AUT3exe";

Process p = ProcessStart(path);

if (p==null)

ConsoleWriteLine("Warning:process may already exist");

ConsoleWriteLine("Finding main window handle");

IntPtr mwh = FindMainWindowHandle("Form1", 100, 25);

ConsoleWriteLine("Handle to main window is " + mwh);

//有名字控件句柄

ConsoleWriteLine("Findding handle to textbox1");

IntPtr tb = FindWindowEx(mwh, IntPtrZero, null, "<enter color>");

if (tb == IntPtrZero)

throw new Exception("Unable to find textbox1");

else

ConsoleWriteLine("Handle to textbox1 is " + tb);

ConsoleWriteLine("Findding handle to button1");

IntPtr butt = FindWindowEx(mwh, IntPtrZero, null, "button1");

if (butt == IntPtrZero)

throw new Exception("Unable to find button1");

else

ConsoleWriteLine("Handle to button1 is " + butt);

//没有名字或者重名控件

ConsoleWriteLine("Findding handle to listbox1");

IntPtr lb = FindWindowByIndex(mwh,3);

if (lb == IntPtrZero)

throw new Exception("Unable to find listbox1");

else

ConsoleWriteLine("Handle to listbox1 is " + lb);

}

方法:

//通过索引查找相应控件句柄

static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)

{

if (index == 0)

{

return hwndParent;

}

else

{

int ct = 0;

IntPtr result = IntPtrZero;

do

{

result = FindWindowEx(hwndParent,result,null,null);

if (result != IntPtrZero)

{

++ct;

}

} while (ct<index&&result!=IntPtrZero);

return result;

}

}

//获得待测程序主窗体句柄

private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)

{

IntPtr mwh = IntPtrZero;

bool formFound = false;

int attempts = 0;

while (!formFound && attempts < maxTries)

{

if (mwh == IntPtrZero)

{

ConsoleWriteLine("Form not yet found");

ThreadSleep(delay);

++attempts;

mwh = FindWindow(null, caption);

}

else

{

ConsoleWriteLine("Form has been found");

formFound = true;

}

}

if (mwh == IntPtrZero)

throw new Exception("Could not find main window");

else

return mwh;

}

CWnd有个成员函数

HWND GetSafeHwnd( ) const;

在对话框类的成员函数里用

HWND hwnd = GetSafeHwnd();

得到的hwnd就是句柄了

以上就是关于VC 通过进程ID获得主窗口句柄全部的内容,包括:VC 通过进程ID获得主窗口句柄、VC如何获取打开窗口的句柄、VC怎么通过进程ID得到窗口句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9542294.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存