在MFC中无法获取Cwnd类的句柄无窗ActiveX? [英] Can't get the handle of a Cwnd Class in MFC Windowless Activex?

查看:210
本文介绍了在MFC中无法获取Cwnd类的句柄无窗ActiveX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的第一个问题是:为什么一个无窗ActiveX不返回句柄。建议是更改创建设置使一个无窗口激活,我已经尝试,但仍然 m_hWnd 属性已返回零为 GetSafeHwnd()方法。



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



新的不可见类源:

  // moWind.cpp:实现文件
//
#include stdafx.h
#includePINActive.h
#includemoWind.h
#includeinclude\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 presskey;
pressedkey =(LPWFSPINKEY)pResult-> lpBuffer;

reskey = log10 - > ulDigit)/ log 10((double)2);

EK.Format(%d,reskey);
xFSOnKeyEvent-> OnKeyRecieved(reskey);
}
else
{
str.Format(ExecuteEvent:ID =%d\r\\\
,pResult-> u.dwEventID);
}
MessageBox(a Execute message Receieved);
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注册(IXFSEvents * obj)
{
xfsOnKeyEvent = obj;
}
protected:
IXFSEvents * xfsOnKeyEvent;
LRESULT OnExecuteEvent(WPARAM wParam,LPARAM lParam);
DECLARE_MESSAGE_MAP()
};

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



includemoWind.h



  class CmyActivexCtrl:public COleControl,public IXFSEvents 
{
...
类定义
...
protected:
moWind tmpWind;


};

最后在myActivex的创建方法我已经初始化组件回调方法一个想要得到它的Handle这:

  CmyActivexCtrl :: CmyActivexCtrl()
{
InitializeIIDs(& IID_DmyActivex,& IID_DmyActivexEvents );
tmpWind.Register(this);
myOtherComponent.WindowsHandle = tmpWind.GetSafeHwnd(); //这里我的Cwnd派生类返回零
//我的其他组件获取句柄并调用API来注册
//给定句柄并强制API将消息发送到该句柄。
}


解决方案

窗口句柄能够通过它来接收用户消息,你总是可以选择创建一个帮助器窗口,例如消息只有窗口,参见使用CreateWindowEx创建消息窗口



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

I have asked two questions earlier about this and for each post there was some solutions i tried them, 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.

这篇关于在MFC中无法获取Cwnd类的句柄无窗ActiveX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆