Moving Controls on a Dialog
-
Hi everyone. I have a few buttons on a dialog, and when the window resizes, I want the buttons to stay at a fixed distance from the bottom of the dialog window's client area.
CRect rect; GetClientRect(rect); // put 40 pixels up from bottom of screen rect.bottom -= 40 GetDlgItem(IDC_BTN_ADDITEM)->MoveWindow(rect);
I even tried making the button a variable, and tried doing m_btnAddItem.MoveWindow(rect), and it also caused the same assertion. When the code executes, I get an assertion error in winocc.cpp, line 279, belowvoid CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint) { ASSERT(::IsWindow(m_hWnd)); ... } Can anyone tell me the proper way to move the control, so it does not assert? Thanks in advance!
-
Hi everyone. I have a few buttons on a dialog, and when the window resizes, I want the buttons to stay at a fixed distance from the bottom of the dialog window's client area.
CRect rect; GetClientRect(rect); // put 40 pixels up from bottom of screen rect.bottom -= 40 GetDlgItem(IDC_BTN_ADDITEM)->MoveWindow(rect);
I even tried making the button a variable, and tried doing m_btnAddItem.MoveWindow(rect), and it also caused the same assertion. When the code executes, I get an assertion error in winocc.cpp, line 279, belowvoid CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint) { ASSERT(::IsWindow(m_hWnd)); ... } Can anyone tell me the proper way to move the control, so it does not assert? Thanks in advance!
On which function of your dialog is this code inserted? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Hi everyone. I have a few buttons on a dialog, and when the window resizes, I want the buttons to stay at a fixed distance from the bottom of the dialog window's client area.
CRect rect; GetClientRect(rect); // put 40 pixels up from bottom of screen rect.bottom -= 40 GetDlgItem(IDC_BTN_ADDITEM)->MoveWindow(rect);
I even tried making the button a variable, and tried doing m_btnAddItem.MoveWindow(rect), and it also caused the same assertion. When the code executes, I get an assertion error in winocc.cpp, line 279, belowvoid CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint) { ASSERT(::IsWindow(m_hWnd)); ... } Can anyone tell me the proper way to move the control, so it does not assert? Thanks in advance!
You're calling MoveWindow before it has an h_Wnd try this;
if(GetDlgItem(IDC_BTNADDITEM)->m_hWnd) MoveWindow(rect);
-
Hi everyone. I have a few buttons on a dialog, and when the window resizes, I want the buttons to stay at a fixed distance from the bottom of the dialog window's client area.
CRect rect; GetClientRect(rect); // put 40 pixels up from bottom of screen rect.bottom -= 40 GetDlgItem(IDC_BTN_ADDITEM)->MoveWindow(rect);
I even tried making the button a variable, and tried doing m_btnAddItem.MoveWindow(rect), and it also caused the same assertion. When the code executes, I get an assertion error in winocc.cpp, line 279, belowvoid CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint) { ASSERT(::IsWindow(m_hWnd)); ... } Can anyone tell me the proper way to move the control, so it does not assert? Thanks in advance!
Sorry, I meant for it to be this: if(::IsWindow(GetDlgItem(IDC_BTNADDITEM)->m_hWnd)) MoveWindow(rect, TRUE); Are you calling it from OnSize? I did this in my own code, and when it is resized when it is initially displaying it, I guess it doesn't have an m_hWnd. If it still doesn't work, use class wizard to create a class for it (control class) then use that to get the m_hWnd Josh
-
On which function of your dialog is this code inserted? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
In OnSize()
-
Sorry, I meant for it to be this: if(::IsWindow(GetDlgItem(IDC_BTNADDITEM)->m_hWnd)) MoveWindow(rect, TRUE); Are you calling it from OnSize? I did this in my own code, and when it is resized when it is initially displaying it, I guess it doesn't have an m_hWnd. If it still doesn't work, use class wizard to create a class for it (control class) then use that to get the m_hWnd Josh
That worked, thanks! I just had to change m_hWnd to GetSafeHwnd(), it causes an access violation if you use m_hWnd. This brings up another question. I am resizing in the code below, which does center the button horozontally and keeps the button at the same place vertically.
CRect rect; rect.left = cx - (415 + 102); rect.right = rect.left + 40; rect.bottom = cy - 40; rect.top = rect.bottom - 14; if(::IsWindow(GetDlgItem(IDC_BTN_ADD)->GetSafeHwnd())) GetDlgItem(IDC_BTN_ADD)->MoveWindow(rect, TRUE);
In the code above, I specify 40 to be the width of the button, and the 14 to be the height. But when the button is drawn, the button appears to be 10 in height, if it were drawn in the resource editor, and the width appears to be about 25. Is there way to adjust this, so the button appears like it would in the resource editor, that is, with a height of 10 and a width of 40? -
That worked, thanks! I just had to change m_hWnd to GetSafeHwnd(), it causes an access violation if you use m_hWnd. This brings up another question. I am resizing in the code below, which does center the button horozontally and keeps the button at the same place vertically.
CRect rect; rect.left = cx - (415 + 102); rect.right = rect.left + 40; rect.bottom = cy - 40; rect.top = rect.bottom - 14; if(::IsWindow(GetDlgItem(IDC_BTN_ADD)->GetSafeHwnd())) GetDlgItem(IDC_BTN_ADD)->MoveWindow(rect, TRUE);
In the code above, I specify 40 to be the width of the button, and the 14 to be the height. But when the button is drawn, the button appears to be 10 in height, if it were drawn in the resource editor, and the width appears to be about 25. Is there way to adjust this, so the button appears like it would in the resource editor, that is, with a height of 10 and a width of 40?For grins, try using the other version of MoveWindow() and specify the width and height in the parameters, and seee if that works. Josh
-
For grins, try using the other version of MoveWindow() and specify the width and height in the parameters, and seee if that works. Josh
I tried it, and it still produced buttons which are too small. At the risk fo sounding dumb (which isn't uncommon for me :)) I always assumed that the resource editor and code functions all used the same units (pixels). If they are not the same, is there a formula or ratio to convert resource editor units to code units, so to speak?
-
Hi everyone. I have a few buttons on a dialog, and when the window resizes, I want the buttons to stay at a fixed distance from the bottom of the dialog window's client area.
CRect rect; GetClientRect(rect); // put 40 pixels up from bottom of screen rect.bottom -= 40 GetDlgItem(IDC_BTN_ADDITEM)->MoveWindow(rect);
I even tried making the button a variable, and tried doing m_btnAddItem.MoveWindow(rect), and it also caused the same assertion. When the code executes, I get an assertion error in winocc.cpp, line 279, belowvoid CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint) { ASSERT(::IsWindow(m_hWnd)); ... } Can anyone tell me the proper way to move the control, so it does not assert? Thanks in advance!
May be try this GetDlgItem(IDC_BTN_ADDITEM)->ShowWindow(SW_HIDE); GetDlgItem(IDC_BTN_ADDITEM)->MoveWindow(rect); GetDlgItem(IDC_BTN_ADDITEM)->ShowWindow(SW_SHOW);