zoukankan      html  css  js  c++  java
  • 实例1.1:通过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) 先查找由用户定义的内核对象对象的封装类, 如果找到直接返回,没有找到构造一个临时对象返回.

  • 相关阅读:
    论信息系统项目的范围管理
    Windows服务/Linux守护创建及安装
    C#开源网络通信库PESocketde的使用
    Unity本地持久化类PlayerPrefs使用详解
    记录一个闪烁,跳动,光圈的动画
    一、Linux驱动学习-了解系统
    用c#自己实现一个简单的JSON解析器
    highcharts的yAxis标题过长 分行显示
    Mysql binlog恢复数据
    Xamarin.Forms实例旅行日志
  • 原文地址:https://www.cnblogs.com/mars9/p/2327941.html
Copyright © 2011-2022 走看看