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

时间:2011-09-25 06:42:23

标签: c++ mfc activex atl

我之前已经提出了两个问题,每个帖子都有一些我试过的解决方案,但问题仍然存在。

我的第一个问题是:为什么无窗口的Activex不会返回Handle。建议是“将创建设置更改为无窗口激活关闭,我已经尝试过,但m_hWnd属性仍然按GetSafeHwnd()方法返回零。

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

这是我新的隐形类源:

// moWind.cpp : implementation file
//
#include "stdafx.h"
#include "PINActive.h"
#include "moWind.h"
#include "include\xfspin.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 = %d\r\n", 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”

class CmyActivexCtrl : public COleControl, public IXFSEvents
{
...
Class definition
...
protected:
      moWind tmpWind;
 .
 .
 };

最后在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.
}

1 个答案:

答案 0 :(得分:0)

正如您所提到的,您需要一个窗口句柄才能通过它接收用户消息,您始终可以选择创建帮助窗口,例如仅消息窗口,请参阅Using CreateWindowEx to Make a Message-Only Window

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

相关问题