关于CWnd::Attach( HWND hWndNew )摘录;

来源:互联网 发布:sql 当前时间减一天 编辑:程序博客网 时间:2024/06/08 20:01

// Using Attach and Detach to map to the MDI client window
class CMainFrame : public CMDIFrameWnd
{
...
public:
   CWnd  m_wndMDIClient;
}

CMainFrame::~CMainFrame()
{
   // detach MDI client window
   m_wndMDIClient.Detach();
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
   if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
      return -1;

   // attach MDI client window
   if (m_wndMDIClient.Attach(m_hWndMDIClient) == 0)
   {
      TRACE0("Failed to attach MDIClient./n");
      return -1;      // fail to create
   }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////


CWnd是一个窗口对象,普通情况下是生成实例后用Create创健去一个窗口实例,但也可以将这个窗口对象绑定到一个已经生成的窗口实例上,也就是用Attach,绑定之后你便可以用这个CWnd对象的成员函数和成员变量很方便的操纵"一个已经生成的窗口实例"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1.not use CWnd
Fun(HWND  hwnd)
{
  
MoveWindow(hwnd,0,0,100,100,true);
}
2.use CWnd

Fun (HWND hwnd)
{
CWnd Wnd;
Wnd.Attach(hwnd);
Wnd.MoveWindow(0,0,100,100,true);
Wnd.Detach();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

window使用句柄来把持资源,窗体是一种资源,CWnd是这种资源的MFC的包装类,在Win32API中,
HWnd是LONG型的,可以通过CWnd的ATTECH把一个HWND资源就附加到一个CWnd类里了,就是这样!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
原创粉丝点击