如何注册 Windows 类并使用注册的类查找窗口

发布于 2024-10-02 22:09:22 字数 1121 浏览 0 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2024-10-09 22:09:22

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文