如何注册 Windows 类并使用注册的类查找窗口
我正在创建一个 MFC 应用程序,该应用程序将在单击资源管理器上下文(右键单击)菜单时启动。
但我只需要启动应用程序的单个实例。为此,我必须使用 FindWindow 和 < a href="http://msdn.microsoft.com/en-us/library/kcb1w44w%28VS.80%29.aspx" rel="nofollow">AfxRegisterClass
我尝试在 MFC 中注册该类app如下:
BOOL CNDSClientDlg::InitInstance()
{
//Register Window Updated on 16th Nov 2010, @Subhen
// Register our unique class name that we wish to use
WNDCLASS wndcls;
memset(&wndcls, 0, sizeof(WNDCLASS));
//Class name for using FindWindow later
wndcls.lpszClassName = _T("NDSApp");
// Register new class and exit if it fails
if(!AfxRegisterClass(&wndcls)) // [C]
{
return FALSE;
}
}
并调用MFC类的构造函数中的方法。我在启动应用程序时验证了该课程正在注册。
现在,在我的 shell 扩展中,我试图找到在 MFC 中注册的类,如下所示:
CWnd *pWndPrev = NULL;
pWndPrev = CWnd::FindWindow(_T("NDSApp"),NULL);
if(pWndPrev != NULL)
pWndPrev->BringWindowToTop();
但我无法将 CWnd 获取到 Window。无法弄清楚。如果我遗漏了什么或做错了什么,请告诉我。
I am creating an MFC application which will be launched on click on Explorer Context (Rightclick) menu.
But I need to launch only single instance of the application. For that I have to use FindWindow and AfxRegisterClass
I tried to register the class in my MFC app as below:
BOOL CNDSClientDlg::InitInstance()
{
//Register Window Updated on 16th Nov 2010, @Subhen
// Register our unique class name that we wish to use
WNDCLASS wndcls;
memset(&wndcls, 0, sizeof(WNDCLASS));
//Class name for using FindWindow later
wndcls.lpszClassName = _T("NDSApp");
// Register new class and exit if it fails
if(!AfxRegisterClass(&wndcls)) // [C]
{
return FALSE;
}
}
and called the method in the constructor of the MFC class. I verified that the class is being registered while I am starting the application.
Now in my shell Extension I am trying to find the Class registered in my MFC as below:
CWnd *pWndPrev = NULL;
pWndPrev = CWnd::FindWindow(_T("NDSApp"),NULL);
if(pWndPrev != NULL)
pWndPrev->BringWindowToTop();
But I am not able to get the CWnd to Window. Not able to figure it out. Please let me know if I am missing something or doing something wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FindWindow 查找窗口实例而不是窗口类。在注册类的应用程序中,您需要实际创建一个窗口,以便扩展可以找到该窗口。
(通过类名查找窗口很好;问题是您实际上没有创建任何要查找的内容。)
另外,我怀疑如果您尝试根据您注册的窗口类创建一个窗口,它将会失败,因为您已将 WNDCLASS 结构的大部分保留为空。请参阅您链接到的示例以获得更好的默认值。 (例如,您必须提供 wndproc 和实例。)
FindWindow finds window instances not window classes. In your app which registers the class you need to actually create a window so that the extension can find that window.
(Finding the window by class name is fine; the problem is you haven't actually created anything to find.)
Also, I suspect if you try to create a window based on the window-class you've registered it will fail because you've left most of the WNDCLASS structure null. See the example you linked to for better default values. (e.g. You must provide a wndproc and hinstance.)