Getting the handle of a PictureBox in MFC
-
Hi, It's been a while since I've posted in here. A friend and I have a problem with MFC, We're trying to get the handle of a PictureBox in MFC, but the code we use keeps returning null. Here is a code snippet we're using
CStatic* pCStatic = (CStatic*) GetDlgItem(IDC_STATIC1);
HWND hWnd = pCStatic->m_hWnd;We're trying to get the handle of the PictureBox so we put a DirectX into the control. Many Thanks Tom
-
Hi, It's been a while since I've posted in here. A friend and I have a problem with MFC, We're trying to get the handle of a PictureBox in MFC, but the code we use keeps returning null. Here is a code snippet we're using
CStatic* pCStatic = (CStatic*) GetDlgItem(IDC_STATIC1);
HWND hWnd = pCStatic->m_hWnd;We're trying to get the handle of the PictureBox so we put a DirectX into the control. Many Thanks Tom
Tom, Make sure that you are calling GetDlgItem(int) from the parent window of the CStatic because the MFC wrapper auto-populates the first HWND parameter with this->m_hWnd. You could also use the global namespace function ::GetDlgItem(HWND,int) if you want to call this function from outside the window class. I would actually recommend that you avoid using GetDlgItem and instead use a control variable. In some cases the GetDlgItem function will return a pointer to a CTempWnd object. Another reason to void GetDlgItem is because of performance reasons... GetDlgItem causes the MFC framework to iterate through an internal handle map in an attempt at finding a permanent object. In some projects this map could contain several hundred or more handles. So if an engineer is using a function such as GetDlgItem from WM_PAINT handler or perhaps WM_TIMER they could potentially be iterating needlessly through this map dozens of times per second or more. Best Wishes, -David Delaune