What does 'handle' mean ?
-
HI I’m confused between pointer to CWnd and handle also to CWnd .What’s the difference between handle and pointer in CWnd object .For example when I want to chat text color in text box control : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINTCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); ////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd==pWnd) // using pointers { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////// return hbr; } this code doesn’t chang text color .But when I use handles like this example : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd->m_hWnd==pWnd->m_hWnd) // using handles { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////////// return hbr; } now this code do what I want to chang text color . Can any one tell me what difference between pointers and handles in CWnd objects ? Thank you … AHMAD ALWASHALI
-
HI I’m confused between pointer to CWnd and handle also to CWnd .What’s the difference between handle and pointer in CWnd object .For example when I want to chat text color in text box control : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINTCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); ////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd==pWnd) // using pointers { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////// return hbr; } this code doesn’t chang text color .But when I use handles like this example : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd->m_hWnd==pWnd->m_hWnd) // using handles { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////////// return hbr; } now this code do what I want to chang text color . Can any one tell me what difference between pointers and handles in CWnd objects ? Thank you … AHMAD ALWASHALI
I believe that a pointer is an actual object (CWnd for example) and a handle is the location in memory of that object (or object referenced by the pointer). So say you have a window in memory and you want to access the methods/properties of that window...you would have a pointer/object that "attaches" itself to the window at that memory location thereby giving you access to its "structure and methods". Hope that helps. Bret Faller Odyssey Computing, Inc.
-
HI I’m confused between pointer to CWnd and handle also to CWnd .What’s the difference between handle and pointer in CWnd object .For example when I want to chat text color in text box control : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINTCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); ////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd==pWnd) // using pointers { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////// return hbr; } this code doesn’t chang text color .But when I use handles like this example : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd->m_hWnd==pWnd->m_hWnd) // using handles { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////////// return hbr; } now this code do what I want to chang text color . Can any one tell me what difference between pointers and handles in CWnd objects ? Thank you … AHMAD ALWASHALI
Handle to a window (HWND) is a Win32 animal - it's a magic cookie which is used to identify windows in the system. Ok, it's not exactly magic: HWND is just a 32-bit number without meaning outside of running Windows system. MFC is a C++ library, and represents windows as C++ objects derived from class CWnd. Each CWnd-derived object has a data member, m_hWnd, which holds the corresponding Win32 HWND. But the connection between HWNDs and CWnd pointers doesn't stop here: MFC also uses handle maps to provide C++ pointers to CWnd-derived objects when it's needed. In your example, OnCtlColor function receives C++ pointer to a CWnd. Before calling your handler, MFC checks if there's already some CWnd object, if not it creates temporary one and passes its address instead of HWND. Actually, the mapping is somewhat more complicated: there are 'permanent' and 'temporary' maps, I believe this could be the source of your problems. Tomasz Sowinski -- http://www.shooltz.com
-
HI I’m confused between pointer to CWnd and handle also to CWnd .What’s the difference between handle and pointer in CWnd object .For example when I want to chat text color in text box control : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINTCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); ////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd==pWnd) // using pointers { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////// return hbr; } this code doesn’t chang text color .But when I use handles like this example : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd->m_hWnd==pWnd->m_hWnd) // using handles { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////////// return hbr; } now this code do what I want to chang text color . Can any one tell me what difference between pointers and handles in CWnd objects ? Thank you … AHMAD ALWASHALI
The issue is related to permanent / temporary handle mapping in MFC's CWnd class. This is a complex subject and so it's easy to get confused. Here is a MSJ/97 article on this subject that you may find useful: http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0997/c++0997.htm&nav=/msj/0997/newnav.htm Hope this helps. // Fazlul
Get RadVC today! Play RAD in VC++ http://www.capitolsoft.com
-
HI I’m confused between pointer to CWnd and handle also to CWnd .What’s the difference between handle and pointer in CWnd object .For example when I want to chat text color in text box control : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINTCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); ////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd==pWnd) // using pointers { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////// return hbr; } this code doesn’t chang text color .But when I use handles like this example : HBRUSH CMyDlg::OnCtlColor(CDC*pDC,CWnd*pWnd,UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //////////////////////////////////////////////// CWnd* wnd=GetDlgItem(IDC_EDIT1); if(wnd->m_hWnd==pWnd->m_hWnd) // using handles { pDC->SetTextColor(RGB(255,0,0)); } /////////////////////////////////////////////////// return hbr; } now this code do what I want to chang text color . Can any one tell me what difference between pointers and handles in CWnd objects ? Thank you … AHMAD ALWASHALI
In general, a handle is just a 32-bit key to a data structure maintained by Windows. Some common handles are:
- HACCEL - Handle to an accelerator table.
- HANDLE - Handle to an object.
- HBITMAP - Handle to a bitmap.
- HBRUSH - Handle to a brush.
- HCOLORSPACE - Handle to a logical color space.
- HCONV - Handle to a dynamic data exchange (DDE) conversation.
- HCONVLIST - Handle to a DDE conversation list.
- HCURSOR - Handle to a cursor.
- HDC - Handle to a device context (DC).
- HDDEDATA - Handle to DDE data.
- HDESK - Handle to a desktop.
- HDROP - Handle to an internal drop structure.
- HDWP - Handle to a deferred window position structure.
- HENHMETAFILE - Handle to an enhanced metafile.
- HFILE - Handle to a file opened byOpenFile, notCreateFile.
- HFONT - Handle to a font.
- HGDIOBJ - Handle to a GDI object.
- HGLOBAL - Handle to a global memory block.
- HHOOK - Handle to a hook.
- HICON - Handle to an icon.
- HIMAGELIST - Handle to an image list.
- HIMC - Handle to input context.
- HINSTANCE - Handle to an instance.
- HKEY - Handle to a registry key.
- HKL - Handle to a keyboard layout.
- HLOCAL - Handle to a local memory block.
- HMENU - Handle to a menu.
- HMETAFILE - Handle to a metafile.
- HMODULE - Handle to a module.
- HMONITOR - Handle to a display monitor.
- HOOKPROC - Pointer to an application-defined hook function specified toSetWindowsHookEx.
- HPALETTE - Handle to a palette.
- HPEN - Handle to a pen.
- HRGN - Handle to a region.
- HRSRC - Handle to a resource.
- HSZ - Handle to a DDE string.
- HTREEITEM - Handle to an item in a tree-view control.
- HWINSTA - Handle to a window station.
- HWND - Handle to a window.
/ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com