开发者

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

开发者 https://www.devze.com 2023-04-08 10:01 出处:网络
I have asked two questions earlier about thisand for each post there was some solutions i tried them,开发者_如何学C but the problem still exist.

I have asked two questions earlier about this and for each post there was some solutions i tried them,开发者_如何学C but the problem still exist.

My first question was : why a windowless Activex does not return the Handle. the suggestion was "change the creation setting an make windowless activate off, i have tried it but still m_hWnd property has returned zero as GetSafeHwnd() method has did.

the second one was the same question this one focused on COleControl class and it's ancestor CWnd. the solution was as this "Create invisible window somewhere in your control initialization code. Handle the messages sent to this window, and call controls methods directly". so i did that but the created class still returns zero handle.

here is my new invisible class source:

// 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()

and this is .h file of the class:

// 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()
};

and at the end here this the way I've used this class in my Activex: in the myActivex.h file:

include "moWind.h"

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

finally in the creation method of myActivex i have initialized the component callback method an wanted to get it's Handle as this:

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


As you mentioned you need a window handle to be able to receive user messages through it, you always have an option of creating a helper window, such as message only window, see Using CreateWindowEx to Make a Message-Only Window.

For your windowless control it is okay to not have any window handle at all, so you cannot really rely on handle availability unless you own a window yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号