无法在 MFC Windowless Activex 中获取 Cwnd 类的句柄?

Can't get the handle of a Cwnd Class in MFC Windowless Activex?

本文关键字:Cwnd 句柄 获取 MFC Windowless Activex      更新时间:2023-10-16

我之前问过两个问题,每个帖子都有一些解决方案,但问题仍然存在。

我的第一个问题是:为什么没有窗口的Activex不返回句柄。建议是"更改创建设置并使无窗口激活关闭,我已经尝试过了,但m_hWnd属性仍然返回零,因为GetSafeHwnd()方法已经完成了。

第二个问题是同样的问题,这个问题关注的是COleControl类和它的祖先CWnd。解决方案如下"在控件初始化代码的某个地方创建不可见窗口。处理发送到此窗口的消息,并直接调用控件方法"。所以我这样做了,但创建的类仍然返回零句柄。

这是我新的不可见类源:

// moWind.cpp : implementation file
//
#include "stdafx.h"
#include "PINActive.h"
#include "moWind.h"
#include "includexfspin.h"
#include <math.h>
// moWind
IMPLEMENT_DYNAMIC(moWind, CWnd)
moWind::moWind(){}
moWind::~moWind(){}
//=============================================================
LRESULT moWind::OnExecuteEvent (WPARAM wParam, LPARAM lParam)
{
    WFSRESULT *pResult = (WFSRESULT *)lParam;
    CString EK=_T("");
    CString str;
    int reskey=0;
    if (pResult->u.dwEventID=WFS_EXEE_PIN_KEY)
    {       
        LPWFSPINKEY pressedkey;
        pressedkey=(LPWFSPINKEY)pResult->lpBuffer;
    reskey = log10((double)pressedkey->ulDigit) / log10((double)2);
        EK.Format("%d",reskey);
        xfsOnKeyEvent->OnKeyRecieved(reskey);
    }
    else
    {
        str.Format("ExecuteEvent:  ID = %drn", pResult->u.dwEventID);
    }
    MessageBox("a Execute message Recieved");
    return 0;
}
    
BEGIN_MESSAGE_MAP(moWind, CWnd)
        
    ON_MESSAGE(WFS_EXECUTE_EVENT,OnExecuteEvent)
    
END_MESSAGE_MAP()

这是类的。h文件:

// moWind.h
class IXFSEvents
{
protected:
    IXFSEvents(){};
    virtual ~IXFSEvents(){};
public:
    virtual void OnKeyRecieved(int key)=0;
};
class moWind : public CWnd
{
    DECLARE_DYNAMIC(moWind)
public:
    moWind();
    virtual ~moWind();
    void Register(IXFSEvents* obj)
    {
        xfsOnKeyEvent= obj;
    }
protected:
    IXFSEvents* xfsOnKeyEvent;
    LRESULT OnExecuteEvent (WPARAM wParam, LPARAM lParam);
    DECLARE_MESSAGE_MAP()
};

在这里结束,这是我在我的Activex中使用这个类的方式:在myActivex.h文件中:

<标题>包括"moWind.h"h1> 后在myActivex的创建方法中,我已经初始化了组件回调方法,并希望获得它的句柄如下:
CmyActivexCtrl::CmyActivexCtrl()
{
    InitializeIIDs(&IID_DmyActivex, &IID_DmyActivexEvents);
    tmpWind.Register(this);
    myOtherComponent.WindowsHandle=tmpWind.GetSafeHwnd(); //here my Cwnd derived class returns zero
        //my other component gets the handle and call an API with it to register 
        //the given handle and force the API to send the messages to that handle.
}

正如您所提到的,您需要一个窗口句柄来通过它接收用户消息,您总是可以选择创建一个帮助窗口,例如消息窗口,请参阅使用CreateWindowEx来创建一个消息窗口。

对于你的无窗口控件,没有任何窗口句柄是可以的,所以你不能真正依赖句柄的可用性,除非你自己拥有一个窗口。