VC中句柄、指针、ID之间的转换
转自http://blog.csdn.net/cy757/article/details/4423756
win32直接操作的是句柄HANDLE,每个句柄就对应windows窗口,而vc对HANDLE进行类封装,间接操作的都是HANDLE,现在句柄只是类的一个成员变量。
从句柄到指针
CWnd* pWnd=CWnd::FromHandle(hWnd); //a temporary CWnd object is created //and attached.
pWnd->Attach(hWnd); //Attaches a Windows window to a CWnd object
从指针到句柄
HWND hWnd=pWnd->GetSafeHandle();
hWnd=pWnd->m_hWnd;
SDK编程中窗口ID,句柄,指针三者相互转换函数
ID--HANDLE--HWND三者之间的互相转换
id->句柄-----------hWnd = ::GetDlgItem(hParentWnd,id);
id->指针-----------CWnd::GetDlgItem();
句柄->id-----------id = GetWindowLong(hWnd,GWL_ID);
句柄->指针--------CWnd *pWnd=CWnd::FromHandle(hWnd);
指针->ID----------id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID);
GetDlgCtrlID();
指针->句柄--------hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;
HICON->ID--------HICON hIcon = AfxGetApp()->LoadIcon(nIconID);
HICON hIcon = LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(nIconID));
附注:获得窗口句柄三种方法
1.HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName)
HWND FindWindowEx(HWND hwndParent, HWND hwndChildAfter,LPCTSTR lpClassName, LPCTSTR lpWindowName)
2.HWND WindowFromPoint(POINT& Point)//获得当前鼠标光标位置的窗口HWND
转自http://blog.csdn.net/cy757/article/details/4423756
win32直接操作的是句柄HANDLE,每个句柄就对应windows窗口,而vc对HANDLE进行类封装,间接操作的都是HANDLE,现在句柄只是类的一个成员变量。
从句柄到指针
CWnd* pWnd=CWnd::FromHandle(hWnd); //a temporary CWnd object is created //and attached.
pWnd->Attach(hWnd); //Attaches a Windows window to a CWnd object
从指针到句柄
HWND hWnd=pWnd->GetSafeHandle();
hWnd=pWnd->m_hWnd;
SDK编程中窗口ID,句柄,指针三者相互转换函数
ID--HANDLE--HWND三者之间的互相转换
id->句柄-----------hWnd = ::GetDlgItem(hParentWnd,id);
id->指针-----------CWnd::GetDlgItem();
句柄->id-----------id = GetWindowLong(hWnd,GWL_ID);
句柄->指针--------CWnd *pWnd=CWnd::FromHandle(hWnd);
指针->ID----------id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID);
GetDlgCtrlID();
指针->句柄--------hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;
HICON->ID--------HICON hIcon = AfxGetApp()->LoadIcon(nIconID);
HICON hIcon = LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(nIconID));
附注:获得窗口句柄三种方法
1.HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName)
HWND FindWindowEx(HWND hwndParent, HWND hwndChildAfter,LPCTSTR lpClassName, LPCTSTR lpWindowName)
2.HWND WindowFromPoint(POINT& Point)//获得当前鼠标光标位置的窗口HWND