簡體   English   中英

無法在MFC Windowless Activex中獲得Cwnd類的句柄?

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

我之前已經問過兩個問題,對於每個帖子,我都嘗試過一些解決方案,但是問題仍然存在。

我的第一個問題是:為什么沒有窗口的Activex無法返回句柄。 建議是“更改創建設置以使無窗口激活關閉,我已經嘗試過了,但是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的創建方法中,我初始化了組件回調方法,想要獲得它的Handle,如下所示:

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制作僅消息窗口

對於無窗口控件,完全沒有任何窗口句柄是可以的,因此,除非您自己擁有一個窗口,否則您不能真正依靠句柄可用性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM