Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Moving Controls on a Dialog

Moving Controls on a Dialog

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Clump
    wrote on last edited by
    #1

    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, below void 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!

    J J J 4 Replies Last reply
    0
    • J John Clump

      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, below void 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!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      On which function of your dialog is this code inserted? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      J 1 Reply Last reply
      0
      • J John Clump

        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, below void 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!

        J Offline
        J Offline
        Josh Koppang
        wrote on last edited by
        #3

        You're calling MoveWindow before it has an h_Wnd try this; if(GetDlgItem(IDC_BTNADDITEM)->m_hWnd) MoveWindow(rect);

        1 Reply Last reply
        0
        • J John Clump

          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, below void 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!

          J Offline
          J Offline
          Josh Koppang
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            On which function of your dialog is this code inserted? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            J Offline
            J Offline
            John Clump
            wrote on last edited by
            #5

            In OnSize()

            1 Reply Last reply
            0
            • J Josh Koppang

              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

              J Offline
              J Offline
              John Clump
              wrote on last edited by
              #6

              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?

              J 1 Reply Last reply
              0
              • J John Clump

                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?

                J Offline
                J Offline
                Josh Koppang
                wrote on last edited by
                #7

                For grins, try using the other version of MoveWindow() and specify the width and height in the parameters, and seee if that works. Josh

                J 1 Reply Last reply
                0
                • J Josh Koppang

                  For grins, try using the other version of MoveWindow() and specify the width and height in the parameters, and seee if that works. Josh

                  J Offline
                  J Offline
                  John Clump
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • J John Clump

                    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, below void 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!

                    J Offline
                    J Offline
                    jagadish bharath
                    wrote on last edited by
                    #9

                    May be try this GetDlgItem(IDC_BTN_ADDITEM)->ShowWindow(SW_HIDE); GetDlgItem(IDC_BTN_ADDITEM)->MoveWindow(rect); GetDlgItem(IDC_BTN_ADDITEM)->ShowWindow(SW_SHOW);

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups