很多API函数都要提供窗口句柄(HWND),我怎样获得相应的HWND,能否解释一下HWND?

解决方案 »

  1.   

    通俗地说,如果把一个到处跑的人当作指针的话,那么HWND就是该人的身份证。
    CWnd *pp;
    HWND hWnd=pp->GetSafeHwnd();
      

  2.   

    up 
    hwnd is only id in the windows program and control of windows
      

  3.   

    Relationship Between a C++ Window Object and an HWND
    Home |  Overview |  How Do I |  TutorialThe window object is an object of the C++ CWnd class (or a derived class) that your program creates directly. It comes and goes in response to your program’s constructor and destructor calls. The Windows window, on the other hand, is an opaque handle to an internal Windows data structure that corresponds to a window and consumes system resources when present. A Windows window is identified by a “window handle” (HWND) and is created after the CWnd object is created by a call to the Create member function of class CWnd. The window may be destroyed either by a program call or by a user’s action. The window handle is stored in the window object’s m_hWnd member variable. The following figure shows the relationship between the C++ window object and the Windows window. Creating windows is discussed in Creating Windows. Destroying windows is discussed in Destroying Window Objects. 
    Detaching a CWnd from Its HWND
    Home |  Overview |  How Do I |  TutorialIf you need to circumvent the object-HWND relationship, MFC provides another CWnd member function, Detach, which disconnects the C++ window object from the Windows window. This prevents the destructor from destroying the Windows window when the object is destroyed. What do you want to know more about?
    Creating windows
    Window destruction sequence
    Allocating and deallocating window memory 
      

  4.   

    u'd see more details via MSDN :)
      

  5.   

    通俗地说,如果把一个到处跑的人当作指针的话,那么HWND就是该人的身份证----我想应该是身份证号码
      

  6.   

    如果说窗口是电视机,那么HWnd就是这个电视机的遥控器
      

  7.   

    我的经验~
    这需要看具体函数~
    比如
    Messagebox();中的HWND hWnd,可以用NULL~
    有的函数中的用this代替就可以了~
    有的是Windows中自己定义的全局变量……
    如:
    SetWindowPos(m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
    m_hWnd就是基于对话框程序的的指针~
    有的需要你去获得~
    CWnd* pCAPwnd=GetDlgItem(IDC_EDIT_CAPTION);//取得编辑框的类指针
    SendMessage(pCAPwnd->GetSafeHwnd(),WM_SETTEXT,0,(LPARAM)(LPCTSTR)m_EditPos);有时你无法获得HWND~这就需要你去进行强制类型转换~
    如:HWND pCAPwnd=(HWND)GetDlgItem(IDC_EDIT_CAPTION);
    //GetDlgItem得到的是CWnd* ~我们需要把它进行转换成HWND~
      

  8.   

    得到主窗口句柄用下面的语句
    HWND h=AfxGetApp()->m_pMainWnd->m_hWnd;