2011-09-25 79 views
1

我之前就這個問題提出了兩個問題,每個帖子都有我嘗試過的一些解決方案,但問題仍然存在。在MFC Windowless Activex中無法獲取Cwnd類的句柄?

我的第一個問題是:爲什麼無窗口的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的創建方法

終於想獲得它的句柄,因爲這:

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

這裏有聽到我的故事嗎? :D –

+0

對不起,又是什麼問題? – djeidot

+0

那麼,如果它是一個無窗口的ActiveX控件,它自然不會有任何窗口句柄。你需要什麼手柄?有很多COM接口可以讓你在沒有窗口句柄的情況下實現。請明確說明。 – aquaherd

回答

0

正如你所說,你需要一個窗口句柄可以通過它接收用戶消息,你總是可以選擇創建一個幫助窗口,比如只有消息窗口,參見Using CreateWindowEx to Make a Message-Only Window

對於無窗控制,根本沒有任何窗口句柄是可以的,所以除非你自己擁有一個窗口,否則你不能真正依賴句柄的可用性。

+0

所以我如何通過創建的窗口以這種方式處理消息? –

+0

與任何窗口一樣 - 定義處理程序並讓它們調用。第一次偶然的谷歌結果給了我這一個:[如何使一個消息窗口](http://www.codeproject.com/KB/dialog/messageonly.aspx),它顯示了一個窗口類「CMyMessageOnlyWindowClass」這是關於你需要什麼,在MFC中。 ATL代碼可能會更簡單。 –

+0

非常感謝你Roman Roman R. –