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. how to move button when the dialog is resized?

how to move button when the dialog is resized?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
17 Posts 6 Posters 1 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.
  • A antonio343

    Hi, I have a modeless dialog with a edit control, and two button. I show the dialog maximized, I got maximized the edit control with

    void CEjemplo::OnSize(UINT nType, int cx, int cy)
    {
    CDialogEx::OnSize(nType, cx, cy);

    	if (GetDlgItem(IDC\_EDIT1))
    	{
    		  GetDlgItem(IDC\_EDIT1)->MoveWindow(5,5,cx-1,cy-1); 
    	}
    

    }

    But I dont know how to move the button at the botton of the dialog. Now, when I maximized the dialog the button dont move itself and it is at back of edit control I tried to do

    if (GetDlgItem(IDC_EDIT1))
    {
    GetDlgItem(IDC_EDIT1)->MoveWindow(5,5,cx-1,cy-1);
    GetDlgItem(IDC_button1)->MoveWindow(5,5,cx-1,cy-1);

        }
    

    But not run.

    M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #2

    antonio343 wrote:

    But not run

    Crash or not moving the controls in the good position ? When doing something like that, moving controls around, I play dumb and use the dialog client area rectangle (GetClientRect). Play around with the values to move to the bottom of the rect returned by GetClientRect; Note: be careful with the coordinate being in either relative to the client or window .

    Watched code never compiles.

    A 1 Reply Last reply
    0
    • M Maximilien

      antonio343 wrote:

      But not run

      Crash or not moving the controls in the good position ? When doing something like that, moving controls around, I play dumb and use the dialog client area rectangle (GetClientRect). Play around with the values to move to the bottom of the rect returned by GetClientRect; Note: be careful with the coordinate being in either relative to the client or window .

      Watched code never compiles.

      A Offline
      A Offline
      antonio343
      wrote on last edited by
      #3

      Not moving the controls in the good position. I dont know how to use this function. Maybe on this way:

      void CEjemplo::OnSize(UINT nType, int cx, int cy)
      {
      CDialogEx::OnSize(nType, cx, cy);
      CRect myRect;
      GetClientRect(&myRect);

          if (GetDlgItem(IDC\_EDIT1))
          {
               m\_Edit.MoveWindow(5,5,cx-55,cy-55); //puedes jugar con estos valores
                m\_Button1.MoveWindow(myRect.right, myRect.bottom,
        myRect.Width(), myRect.Height());
      
          }
      

      }

      But I dont want to change the size, only the position. I'd like to move at the right botton corner.

      M C 2 Replies Last reply
      0
      • A antonio343

        Not moving the controls in the good position. I dont know how to use this function. Maybe on this way:

        void CEjemplo::OnSize(UINT nType, int cx, int cy)
        {
        CDialogEx::OnSize(nType, cx, cy);
        CRect myRect;
        GetClientRect(&myRect);

            if (GetDlgItem(IDC\_EDIT1))
            {
                 m\_Edit.MoveWindow(5,5,cx-55,cy-55); //puedes jugar con estos valores
                  m\_Button1.MoveWindow(myRect.right, myRect.bottom,
          myRect.Width(), myRect.Height());
        
            }
        

        }

        But I dont want to change the size, only the position. I'd like to move at the right botton corner.

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #4

        use SetWindowPos (with the SWP_NOSIZE option, check the documentation ) function instead of MoveWindow. M.

        Watched code never compiles.

        A 1 Reply Last reply
        0
        • M Maximilien

          use SetWindowPos (with the SWP_NOSIZE option, check the documentation ) function instead of MoveWindow. M.

          Watched code never compiles.

          A Offline
          A Offline
          antonio343
          wrote on last edited by
          #5

          I've tried with this:

          void CEjemplo::OnSize(UINT nType, int cx, int cy)
          {
          CDialogEx::OnSize(nType, cx, cy);
          CRect myRect;
          const CWnd &p=wndBottom;
          GetClientRect(&myRect);

              if (GetDlgItem(IDC\_EDIT1))
              {
                  GetDlgItem(IDC\_EDIT1)->MoveWindow(5,5,cx-50,cy-80);  //puedes jugar con estos valores
                  m\_Button1.SetWindowPos(&p,5,5,cx-50,cy-75,SWP\_NOSIZE);
              }
          

          }

          But when I run the application it crash X|

          D T 2 Replies Last reply
          0
          • A antonio343

            I've tried with this:

            void CEjemplo::OnSize(UINT nType, int cx, int cy)
            {
            CDialogEx::OnSize(nType, cx, cy);
            CRect myRect;
            const CWnd &p=wndBottom;
            GetClientRect(&myRect);

                if (GetDlgItem(IDC\_EDIT1))
                {
                    GetDlgItem(IDC\_EDIT1)->MoveWindow(5,5,cx-50,cy-80);  //puedes jugar con estos valores
                    m\_Button1.SetWindowPos(&p,5,5,cx-50,cy-75,SWP\_NOSIZE);
                }
            

            }

            But when I run the application it crash X|

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #6

            antonio343 wrote:

            But when I run the application it crash

            Exception thrown? Assertion fired? Have you stepped into the code to see what is actually going on?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

            A 1 Reply Last reply
            0
            • A antonio343

              I've tried with this:

              void CEjemplo::OnSize(UINT nType, int cx, int cy)
              {
              CDialogEx::OnSize(nType, cx, cy);
              CRect myRect;
              const CWnd &p=wndBottom;
              GetClientRect(&myRect);

                  if (GetDlgItem(IDC\_EDIT1))
                  {
                      GetDlgItem(IDC\_EDIT1)->MoveWindow(5,5,cx-50,cy-80);  //puedes jugar con estos valores
                      m\_Button1.SetWindowPos(&p,5,5,cx-50,cy-75,SWP\_NOSIZE);
                  }
              

              }

              But when I run the application it crash X|

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #7

              antonio343 wrote:

              m_Button1.SetWindowPos(&p,5,5,cx-50,cy-75,SWP_NOSIZE)

              change this to read:

              m_Button1.SetWindowPos(&wndTop, cx - m_Button1.Width - 5, cy - m_Button1.Height - 5, 0, 0, SWP_NOSIZE);

              If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
              You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

              A 1 Reply Last reply
              0
              • A antonio343

                Hi, I have a modeless dialog with a edit control, and two button. I show the dialog maximized, I got maximized the edit control with

                void CEjemplo::OnSize(UINT nType, int cx, int cy)
                {
                CDialogEx::OnSize(nType, cx, cy);

                	if (GetDlgItem(IDC\_EDIT1))
                	{
                		  GetDlgItem(IDC\_EDIT1)->MoveWindow(5,5,cx-1,cy-1); 
                	}
                

                }

                But I dont know how to move the button at the botton of the dialog. Now, when I maximized the dialog the button dont move itself and it is at back of edit control I tried to do

                if (GetDlgItem(IDC_EDIT1))
                {
                GetDlgItem(IDC_EDIT1)->MoveWindow(5,5,cx-1,cy-1);
                GetDlgItem(IDC_button1)->MoveWindow(5,5,cx-1,cy-1);

                    }
                

                But not run.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                antonio343 wrote:

                Now, when I maximized the dialog the button dont move itself...

                See if the Extras section of this article is of any help. I've updated the code in a newer project but the premise is the same.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                T 1 Reply Last reply
                0
                • D David Crow

                  antonio343 wrote:

                  But when I run the application it crash

                  Exception thrown? Assertion fired? Have you stepped into the code to see what is actually going on?

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  A Offline
                  A Offline
                  antonio343
                  wrote on last edited by
                  #9

                  I'm sorry , the error wasn't due to the setwindowspos. I solved the error. It run well, but I need know the coordinates of the right bottom corner.

                  D 1 Reply Last reply
                  0
                  • A antonio343

                    I'm sorry , the error wasn't due to the setwindowspos. I solved the error. It run well, but I need know the coordinates of the right bottom corner.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    antonio343 wrote:

                    It run well, but I need know the coordinates of the right bottom corner.

                    Relative to what?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                    1 Reply Last reply
                    0
                    • D David Crow

                      antonio343 wrote:

                      Now, when I maximized the dialog the button dont move itself...

                      See if the Extras section of this article is of any help. I've updated the code in a newer project but the premise is the same.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                      T Offline
                      T Offline
                      TheGreatAndPowerfulOz
                      wrote on last edited by
                      #11

                      +5. Good bit of code.

                      If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                      You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                      1 Reply Last reply
                      0
                      • T TheGreatAndPowerfulOz

                        antonio343 wrote:

                        m_Button1.SetWindowPos(&p,5,5,cx-50,cy-75,SWP_NOSIZE)

                        change this to read:

                        m_Button1.SetWindowPos(&wndTop, cx - m_Button1.Width - 5, cy - m_Button1.Height - 5, 0, 0, SWP_NOSIZE);

                        If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                        You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                        A Offline
                        A Offline
                        antonio343
                        wrote on last edited by
                        #12

                        I cant change this, becouse CButton don't have some member "width" and "Height"

                        C T 2 Replies Last reply
                        0
                        • A antonio343

                          Hi, I have a modeless dialog with a edit control, and two button. I show the dialog maximized, I got maximized the edit control with

                          void CEjemplo::OnSize(UINT nType, int cx, int cy)
                          {
                          CDialogEx::OnSize(nType, cx, cy);

                          	if (GetDlgItem(IDC\_EDIT1))
                          	{
                          		  GetDlgItem(IDC\_EDIT1)->MoveWindow(5,5,cx-1,cy-1); 
                          	}
                          

                          }

                          But I dont know how to move the button at the botton of the dialog. Now, when I maximized the dialog the button dont move itself and it is at back of edit control I tried to do

                          if (GetDlgItem(IDC_EDIT1))
                          {
                          GetDlgItem(IDC_EDIT1)->MoveWindow(5,5,cx-1,cy-1);
                          GetDlgItem(IDC_button1)->MoveWindow(5,5,cx-1,cy-1);

                              }
                          

                          But not run.

                          C Offline
                          C Offline
                          Chuck OToole
                          wrote on last edited by
                          #13

                          The first two arguments to MoveWindow are the new X and Y coordinates of the upper left of the window. You are putting all your things at [5,5], on top of one another. No wonder it doesn't seem to work.

                          1 Reply Last reply
                          0
                          • A antonio343

                            I cant change this, becouse CButton don't have some member "width" and "Height"

                            C Offline
                            C Offline
                            Chuck OToole
                            wrote on last edited by
                            #14

                            A CButton is a Window, WIndows have Rect (GetWindowRect() function). Rect gives you height and width. So it's not a straightforward as posted but easily obtainable.

                            1 Reply Last reply
                            0
                            • A antonio343

                              I cant change this, becouse CButton don't have some member "width" and "Height"

                              T Offline
                              T Offline
                              TheGreatAndPowerfulOz
                              wrote on last edited by
                              #15

                              Sorry, the other poster is right. Use

                              CRect rect;
                              m_Button1.GetWindowRect(&rect)
                              rect.Width()
                              rect.Height()

                              If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                              You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                              1 Reply Last reply
                              0
                              • A antonio343

                                Not moving the controls in the good position. I dont know how to use this function. Maybe on this way:

                                void CEjemplo::OnSize(UINT nType, int cx, int cy)
                                {
                                CDialogEx::OnSize(nType, cx, cy);
                                CRect myRect;
                                GetClientRect(&myRect);

                                    if (GetDlgItem(IDC\_EDIT1))
                                    {
                                         m\_Edit.MoveWindow(5,5,cx-55,cy-55); //puedes jugar con estos valores
                                          m\_Button1.MoveWindow(myRect.right, myRect.bottom,
                                  myRect.Width(), myRect.Height());
                                
                                    }
                                

                                }

                                But I dont want to change the size, only the position. I'd like to move at the right botton corner.

                                C Offline
                                C Offline
                                Chuck OToole
                                wrote on last edited by
                                #16

                                Assuming the coordinate system is correct (Client View, Control View, Screen View), which is a big assumption at this point, the line

                                m_Button1.MoveWindow(myRect.right, myRect.bottom,

                                seems to want to put the left edge of the button at the right edge of the main window, therefore, it's not visible since it's off the right side of the main window. Similarly for the y coordinate, you're putting the top of the button starting at the bottom of the window, therefore it's outside the viewing area

                                1 Reply Last reply
                                0
                                • A antonio343

                                  Hi, I have a modeless dialog with a edit control, and two button. I show the dialog maximized, I got maximized the edit control with

                                  void CEjemplo::OnSize(UINT nType, int cx, int cy)
                                  {
                                  CDialogEx::OnSize(nType, cx, cy);

                                  	if (GetDlgItem(IDC\_EDIT1))
                                  	{
                                  		  GetDlgItem(IDC\_EDIT1)->MoveWindow(5,5,cx-1,cy-1); 
                                  	}
                                  

                                  }

                                  But I dont know how to move the button at the botton of the dialog. Now, when I maximized the dialog the button dont move itself and it is at back of edit control I tried to do

                                  if (GetDlgItem(IDC_EDIT1))
                                  {
                                  GetDlgItem(IDC_EDIT1)->MoveWindow(5,5,cx-1,cy-1);
                                  GetDlgItem(IDC_button1)->MoveWindow(5,5,cx-1,cy-1);

                                      }
                                  

                                  But not run.

                                  R Offline
                                  R Offline
                                  Rolf Kristensen
                                  wrote on last edited by
                                  #17

                                  Easy dialog control resizer[^]

                                  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