2010-02-27 53 views
0

我创建了一个ATL窗口较少的控制和类的定义是这样的:如何获得无窗口ATL控件的HWND?

class ATL_NO_VTABLE CRSPClient : 
    public IObjectSafetyImpl<CRSPClient, INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA>, 
    public CComObjectRootEx<CComSingleThreadModel>, 
    public IDispatchImpl<IRSPClient, &IID_IRSPClient, &LIBID_axBanckleRSPClientLib, /*wMajor =*/ 1, /*wMinor =*/ 0>, 
    public IPersistStreamInitImpl<CRSPClient>, 
    public IOleControlImpl<CRSPClient>, 
    public IOleObjectImpl<CRSPClient>, 
    public IOleInPlaceActiveObjectImpl<CRSPClient>, 
    public IQuickActivateImpl<CRSPClient>, 
    public IViewObjectExImpl<CRSPClient>, 
    public IOleInPlaceObjectWindowlessImpl<CRSPClient>, 
#ifdef _WIN32_WCE // IObjectSafety is required on Windows CE for the control to be loaded correctly 
    public IObjectSafetyImpl<CRSPClient, INTERFACESAFE_FOR_UNTRUSTED_CALLER>, 
#endif 
    public CComCoClass<CRSPClient, &CLSID_RSPClient>, 
    public CComControl<CRSPClient> 

那么对于一些目的,我需要发布消息的窗口。我试图让窗口的句柄在相当多的方面,他们都失败了:

HWND CRSPClient::GetHwnd() 
{ 
    HWND hwndRet = NULL; 
    // hwndRet = m_hWnd; 
    //IOleInPlaceActiveObjectImpl<CRSPClient>::GetWindow(&hwndRet); 
    //IOleWindow<CRSPClient>::GetWindow(&hwndRet); 
    //this->m_spInPlaceSite->GetWindow(&hwndRet); 
    //CComQIPtr<IOleInPlaceSite> spSite = this->m_spClientSite; 
    //spSite->GetWindow(&hwndRet); 
    //hwndRet = ::WindowFromDC(GetDC()); 
    return hwndRet; 
} 

任何人都知道是有什么办法让HWND?

OMG我完全沮丧的微软伟大的ATL框架!

回答

3

无窗控制的要点在于它没有窗口句柄。如果您想要使用窗口手柄以防万一,它会存在并且控件会回到窗口模式,那么很容易:m_hWndCD

如果不是的话,你必须有一个窗口,那么你必须m_bWindowOnly来标记在构造函数和指示,你将需要一个HWND:即使容器支持窗户

标志指示控制应开窗,控制。

如果你还希望它窗口的,需要一个窗口只是有时候,某些条件下,拿出上运行下,你总是有一个选项,通过它来创建一个私有message only window和发送信息。

3

这里是一些代码取自微软的Direct3D ATL示例。我还没有测试过。

// Get the window we need to use. This will either be the window that has already 
// been created if we are window. If we are windowless the HWND of the client 
// will be retrieved from the HDC. 
void GetOurWindow() 
{ 
    // If we're windowless we still need an HWND for Direct3d 
    if (m_bWndLess) 
    { 
     HDC hDC; 

     // Get the HDC from the client 
     m_spInPlaceSite->GetDC(NULL, OLEDC_NODRAW, &hDC); 
     m_hOurWnd = WindowFromDC(hDC); 
    #if 1 
     // Code to make VB5 paint properly now it has clipped it's own hDC 
     RECT rect; 
     ::GetClientRect(m_hOurWnd,&rect); 
     HRGN hRegion = CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom); 
     SelectClipRgn(hDC,hRegion); 
    #endif 
     m_spInPlaceSite->ReleaseDC(hDC); 
    } 
    else 
     m_hOurWnd = m_hWnd; 
} 
相关问题