通过HWND获得CWnd指针/通过CWnd获取HWND


较常用的下面两个方法:(MFC实现机制是使用映射机制,两个之间互相MAP)

HWND hWnd = GetSafeHwnd();

CWnd *pWnd = CWnd::FromHandle(hWnd);

 

下面的转载地址:http://www.cnblogs.com/mars9/archive/2012/01/20/2327941.html

 

通过HWND获得CWnd指针

//获得当前窗口的句柄 
HWND hWnd = GetSafeHwnd(); 
//通过HWND获得CWnd指针 
CWnd *pWnd = CWnd::FromHandle(hWnd); 
CString strText = _T(""); 
strText.Format("pWnd = 0x%X\nthis = 0x%X\n", pWnd, this); 
AfxMessageBox(strText); 

1、GetSafeHwnd方法:该函数获得窗口对象的窗口句柄。

Returns the window handle for a window. Returns NULL if the CWnd is not attached to a window or if it is used with a NULL CWnd pointer。

这个函数用途很广,很多函数的参数要求是HWND,这时就需要使用GetSafeHwnd函数了。如:

  HWND hwnd;  
  CWnd* pWnd;  
  pWnd=((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();  
  hwnd=pWnd->GetDlgItem(IDC_EDIT2)->GetSafeHwnd();  
  ::SetWindowText(hwnd,m_strResult); 


其它的一些情况:

AfxWinInit         被WinMain调用的一个函数,用做MFC GUI程序初始化的一部份

AfxBeginThread     开始一个新的执行线程

AfxEndThread       结束一个旧的执行线程

AfxMessageBox      类似Windows API 函数MessageBox

AfxGetApp          取得application object(CWinApp衍生对象)的指针

AfxGetMainWnd      取得程序主窗口的指针

AfxGetInstance     取得程序的instance handle

AfxRegisterClass   以自定的WNDCLASS 注册窗口类别

2、FromHandle方法:FromHandle(HANDLE h) 先查找由用户定义的内核对象对象的封装类, 如果找到直接返回,没有找到构造一个临时对象返回.

Life is like a box of chocolate, you never know what you are going to get.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值