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 an MFC Button. [modified]

How to Move an MFC Button. [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studiocomdesign
22 Posts 6 Posters 5 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.
  • L lgmanuel

    and how do i get the current coordinates of the three buttons?

    N Offline
    N Offline
    Niklas L
    wrote on last edited by
    #8

    Using GetWindowRect()[^] And then convert to client coords using ScreenToClient()[^]

    home

    L 1 Reply Last reply
    0
    • C Cedric Moonen

      You should really buy a book about VC++. It is nearly impossible to help someone who has almost no knowledge of VC++ or C++ through a forum. It will take ages to explain some basic concepts. You will be much more efficient by reading a book first.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      L Offline
      L Offline
      lgmanuel
      wrote on last edited by
      #9

      by the way, here is the app. that i was trying create. you can download the project and check it, it's just a design and no codes yet, i just named the controls to their ID's.. what i am tring to do is move the three buttons until they are totally out the the app window.. thanks for the help guys, appreciate it. here is the URL to download the project. http://h1.ripway.com/lgmanuel/VC MFC/MoveTheButton.rar

      1 Reply Last reply
      0
      • N Niklas L

        Using GetWindowRect()[^] And then convert to client coords using ScreenToClient()[^]

        home

        L Offline
        L Offline
        lgmanuel
        wrote on last edited by
        #10

        ..... update by the way, here is the app. that i was trying create. you can download the project and check it, it's just a design and no codes yet, i just named the controls to their ID's.. what i am tring to do is move the three buttons until they are totally out the the app window.. thanks for the help guys, appreciate it. here is the URL to download the project. http://h1.ripway.com/lgmanuel/VC MFC/MoveTheButton.rar

        B M 2 Replies Last reply
        0
        • L lgmanuel

          i think that's the best thing to do for now?.. read a book and understand the concepts and terminology used in VC++. anyway, thanks for the advice.

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #11

          How did you end up in this situation anyway? Did you spice up your CV?

          home

          1 Reply Last reply
          0
          • L lgmanuel

            ..... update by the way, here is the app. that i was trying create. you can download the project and check it, it's just a design and no codes yet, i just named the controls to their ID's.. what i am tring to do is move the three buttons until they are totally out the the app window.. thanks for the help guys, appreciate it. here is the URL to download the project. http://h1.ripway.com/lgmanuel/VC MFC/MoveTheButton.rar

            B Offline
            B Offline
            bolivar123
            wrote on last edited by
            #12

            Why not leave the buttons where they are and simply call ShowWindow(SW_HIDE) on each of the buttons? Such as: GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_HIDE); GetDlgItem(IDC_BUTTON2)->ShowWindow(SW_HIDE); GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_HIDE); Then somewhere else in your program if you want to show the buttons again: GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_SHOW); GetDlgItem(IDC_BUTTON2)->ShowWindow(SW_SHOW); GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_SHOW);

            L 1 Reply Last reply
            0
            • L lgmanuel

              ..... update by the way, here is the app. that i was trying create. you can download the project and check it, it's just a design and no codes yet, i just named the controls to their ID's.. what i am tring to do is move the three buttons until they are totally out the the app window.. thanks for the help guys, appreciate it. here is the URL to download the project. http://h1.ripway.com/lgmanuel/VC MFC/MoveTheButton.rar

              M Offline
              M Offline
              merano
              wrote on last edited by
              #13

              The link does not work. Do you have a Solution ?

              L 1 Reply Last reply
              0
              • B bolivar123

                Why not leave the buttons where they are and simply call ShowWindow(SW_HIDE) on each of the buttons? Such as: GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_HIDE); GetDlgItem(IDC_BUTTON2)->ShowWindow(SW_HIDE); GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_HIDE); Then somewhere else in your program if you want to show the buttons again: GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_SHOW); GetDlgItem(IDC_BUTTON2)->ShowWindow(SW_SHOW); GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_SHOW);

                L Offline
                L Offline
                lgmanuel
                wrote on last edited by
                #14

                there is no need for hiding the controls., i got an idea that if the LEFT button is clicked, it scrolls all the way to the left until the three buttons are gone, then it shows up on the right side of the app window, likewise with the right button. then it should stop to where the three buttons are if the center button is prssed. Like the tag in HTML, it just scrolls to the direction, then shows up to the other side.. i think but am not sure if i need to get the coordinates? need help guys. thanks..

                1 Reply Last reply
                0
                • M merano

                  The link does not work. Do you have a Solution ?

                  L Offline
                  L Offline
                  lgmanuel
                  wrote on last edited by
                  #15

                  http://h1.ripway.com/lgmanuel/VisualCPP/MoveTheButton.rar[^] here is the link, sorry if the previous link did not work.. i renamed the directory..

                  1 Reply Last reply
                  0
                  • N Niklas L

                    Use a timer[^] for the updates and call SetWindowPos()[^] or MoveWindow()[^] to move the buttons.

                    home

                    M Offline
                    M Offline
                    merano
                    wrote on last edited by
                    #16

                    Yes that will do the job. Found an easy TODO for the Timer-Part here ("Using Timers in MFC Applications") In our Sample it would look like this: First update your class in MoveTheButtonDlg.h (Some work is already done by VisualStudio when clicking the Button)

                    enum {DIR_STOP, DIR_LEFT, DIR_RIGHT};

                    // CMoveTheButtonDlg dialog
                    class CMoveTheButtonDlg : public CDialog
                    {

                    ...

                    DECLARE\_MESSAGE\_MAP()
                    

                    public:
                    afx_msg void OnBnClickedMoveright();
                    afx_msg void OnBnClickedStop();
                    afx_msg void OnBnClickedMoveleft();
                    afx_msg void OnTimer(UINT TimerVal); // TI: Add timer handler to messagemap

                    private:
                    BOOL StartTimer (UINT TimerDuration);
                    BOOL StopTimer(void);

                    UINT\_PTR m\_nTimer;
                    UINT  m\_Direction;
                    DWORD m\_winpos;
                    

                    };

                    After that you have to declare Message-Map:

                    BEGIN_MESSAGE_MAP(CMoveTheButtonDlg, CDialog)
                    ON_WM_SYSCOMMAND()
                    ON_WM_PAINT()
                    ON_WM_QUERYDRAGICON()
                    //}}AFX_MSG_MAP
                    ON_BN_CLICKED(IDC_MoveRight, &CMoveTheButtonDlg::OnBnClickedMoveright)
                    ON_BN_CLICKED(IDC_Stop, &CMoveTheButtonDlg::OnBnClickedStop)
                    ON_BN_CLICKED(IDC_MoveLeft, &CMoveTheButtonDlg::OnBnClickedMoveleft)
                    ON_WM_TIMER ( ) // TI: timer implementation
                    END_MESSAGE_MAP()

                    At last the new members have do be defined:

                    void CMoveTheButtonDlg::OnBnClickedStop()
                    {
                    m_Direction = DIR_STOP;
                    StopTimer();
                    }

                    void CMoveTheButtonDlg::OnBnClickedMoveright()
                    {
                    m_Direction = DIR_RIGHT;
                    StartTimer(1000);
                    }

                    void CMoveTheButtonDlg::OnBnClickedMoveleft()
                    {
                    m_Direction = DIR_LEFT;
                    StartTimer(1000);
                    }

                    BOOL CMoveTheButtonDlg::StartTimer (UINT TimerDuration)
                    {
                    m_nTimer = SetTimer (IDT_TIMER_0, TimerDuration, NULL);

                    if ( m_nTimer == 0) {
                    MessageBox (_T("Unable to obtain timer"), _T("IDT_TIMER_0"), MB_OK|MB_SYSTEMMODAL);
                    return FALSE;
                    }

                    return TRUE;
                    }// end StartTimer

                    BOOL CMoveTheButtonDlg::StopTimer(void)
                    {
                    if(m_nTimer) {
                    // Stop the timer
                    if (!KillTimer (m_nTimer)) {
                    return FALSE;
                    m_nTimer = 0;
                    }
                    }

                    return TRUE;
                    

                    } // end StopTimer

                    void CMoveTheButtonDlg::OnTimer (UINT TimerVal)
                    {
                    if(TimerVal != m_nTimer) // TI: check what timer
                    return;

                    switch(m\_Direction) {
                    case DIR\_LEFT:
                    	// TI: move to left here
                    	m\_winpos-=5;
                    	break;
                    	
                    case DIR\_RIGHT:
                    	// TI: move to right here
                    	m\_winpos+=5;
                    	break;
                    }
                    

                    }

                    I

                    L 3 Replies Last reply
                    0
                    • M merano

                      Yes that will do the job. Found an easy TODO for the Timer-Part here ("Using Timers in MFC Applications") In our Sample it would look like this: First update your class in MoveTheButtonDlg.h (Some work is already done by VisualStudio when clicking the Button)

                      enum {DIR_STOP, DIR_LEFT, DIR_RIGHT};

                      // CMoveTheButtonDlg dialog
                      class CMoveTheButtonDlg : public CDialog
                      {

                      ...

                      DECLARE\_MESSAGE\_MAP()
                      

                      public:
                      afx_msg void OnBnClickedMoveright();
                      afx_msg void OnBnClickedStop();
                      afx_msg void OnBnClickedMoveleft();
                      afx_msg void OnTimer(UINT TimerVal); // TI: Add timer handler to messagemap

                      private:
                      BOOL StartTimer (UINT TimerDuration);
                      BOOL StopTimer(void);

                      UINT\_PTR m\_nTimer;
                      UINT  m\_Direction;
                      DWORD m\_winpos;
                      

                      };

                      After that you have to declare Message-Map:

                      BEGIN_MESSAGE_MAP(CMoveTheButtonDlg, CDialog)
                      ON_WM_SYSCOMMAND()
                      ON_WM_PAINT()
                      ON_WM_QUERYDRAGICON()
                      //}}AFX_MSG_MAP
                      ON_BN_CLICKED(IDC_MoveRight, &CMoveTheButtonDlg::OnBnClickedMoveright)
                      ON_BN_CLICKED(IDC_Stop, &CMoveTheButtonDlg::OnBnClickedStop)
                      ON_BN_CLICKED(IDC_MoveLeft, &CMoveTheButtonDlg::OnBnClickedMoveleft)
                      ON_WM_TIMER ( ) // TI: timer implementation
                      END_MESSAGE_MAP()

                      At last the new members have do be defined:

                      void CMoveTheButtonDlg::OnBnClickedStop()
                      {
                      m_Direction = DIR_STOP;
                      StopTimer();
                      }

                      void CMoveTheButtonDlg::OnBnClickedMoveright()
                      {
                      m_Direction = DIR_RIGHT;
                      StartTimer(1000);
                      }

                      void CMoveTheButtonDlg::OnBnClickedMoveleft()
                      {
                      m_Direction = DIR_LEFT;
                      StartTimer(1000);
                      }

                      BOOL CMoveTheButtonDlg::StartTimer (UINT TimerDuration)
                      {
                      m_nTimer = SetTimer (IDT_TIMER_0, TimerDuration, NULL);

                      if ( m_nTimer == 0) {
                      MessageBox (_T("Unable to obtain timer"), _T("IDT_TIMER_0"), MB_OK|MB_SYSTEMMODAL);
                      return FALSE;
                      }

                      return TRUE;
                      }// end StartTimer

                      BOOL CMoveTheButtonDlg::StopTimer(void)
                      {
                      if(m_nTimer) {
                      // Stop the timer
                      if (!KillTimer (m_nTimer)) {
                      return FALSE;
                      m_nTimer = 0;
                      }
                      }

                      return TRUE;
                      

                      } // end StopTimer

                      void CMoveTheButtonDlg::OnTimer (UINT TimerVal)
                      {
                      if(TimerVal != m_nTimer) // TI: check what timer
                      return;

                      switch(m\_Direction) {
                      case DIR\_LEFT:
                      	// TI: move to left here
                      	m\_winpos-=5;
                      	break;
                      	
                      case DIR\_RIGHT:
                      	// TI: move to right here
                      	m\_winpos+=5;
                      	break;
                      }
                      

                      }

                      I

                      L Offline
                      L Offline
                      lgmanuel
                      wrote on last edited by
                      #17

                      this is a step forward.. thanks.. i just thought maybe i needed timers, then poof.. a sample code.. thanks.. just have to modify the code to fit my needs.. but am still having trouble moving the buttons., a trying to work how to move i now?

                      1 Reply Last reply
                      0
                      • M merano

                        Yes that will do the job. Found an easy TODO for the Timer-Part here ("Using Timers in MFC Applications") In our Sample it would look like this: First update your class in MoveTheButtonDlg.h (Some work is already done by VisualStudio when clicking the Button)

                        enum {DIR_STOP, DIR_LEFT, DIR_RIGHT};

                        // CMoveTheButtonDlg dialog
                        class CMoveTheButtonDlg : public CDialog
                        {

                        ...

                        DECLARE\_MESSAGE\_MAP()
                        

                        public:
                        afx_msg void OnBnClickedMoveright();
                        afx_msg void OnBnClickedStop();
                        afx_msg void OnBnClickedMoveleft();
                        afx_msg void OnTimer(UINT TimerVal); // TI: Add timer handler to messagemap

                        private:
                        BOOL StartTimer (UINT TimerDuration);
                        BOOL StopTimer(void);

                        UINT\_PTR m\_nTimer;
                        UINT  m\_Direction;
                        DWORD m\_winpos;
                        

                        };

                        After that you have to declare Message-Map:

                        BEGIN_MESSAGE_MAP(CMoveTheButtonDlg, CDialog)
                        ON_WM_SYSCOMMAND()
                        ON_WM_PAINT()
                        ON_WM_QUERYDRAGICON()
                        //}}AFX_MSG_MAP
                        ON_BN_CLICKED(IDC_MoveRight, &CMoveTheButtonDlg::OnBnClickedMoveright)
                        ON_BN_CLICKED(IDC_Stop, &CMoveTheButtonDlg::OnBnClickedStop)
                        ON_BN_CLICKED(IDC_MoveLeft, &CMoveTheButtonDlg::OnBnClickedMoveleft)
                        ON_WM_TIMER ( ) // TI: timer implementation
                        END_MESSAGE_MAP()

                        At last the new members have do be defined:

                        void CMoveTheButtonDlg::OnBnClickedStop()
                        {
                        m_Direction = DIR_STOP;
                        StopTimer();
                        }

                        void CMoveTheButtonDlg::OnBnClickedMoveright()
                        {
                        m_Direction = DIR_RIGHT;
                        StartTimer(1000);
                        }

                        void CMoveTheButtonDlg::OnBnClickedMoveleft()
                        {
                        m_Direction = DIR_LEFT;
                        StartTimer(1000);
                        }

                        BOOL CMoveTheButtonDlg::StartTimer (UINT TimerDuration)
                        {
                        m_nTimer = SetTimer (IDT_TIMER_0, TimerDuration, NULL);

                        if ( m_nTimer == 0) {
                        MessageBox (_T("Unable to obtain timer"), _T("IDT_TIMER_0"), MB_OK|MB_SYSTEMMODAL);
                        return FALSE;
                        }

                        return TRUE;
                        }// end StartTimer

                        BOOL CMoveTheButtonDlg::StopTimer(void)
                        {
                        if(m_nTimer) {
                        // Stop the timer
                        if (!KillTimer (m_nTimer)) {
                        return FALSE;
                        m_nTimer = 0;
                        }
                        }

                        return TRUE;
                        

                        } // end StopTimer

                        void CMoveTheButtonDlg::OnTimer (UINT TimerVal)
                        {
                        if(TimerVal != m_nTimer) // TI: check what timer
                        return;

                        switch(m\_Direction) {
                        case DIR\_LEFT:
                        	// TI: move to left here
                        	m\_winpos-=5;
                        	break;
                        	
                        case DIR\_RIGHT:
                        	// TI: move to right here
                        	m\_winpos+=5;
                        	break;
                        }
                        

                        }

                        I

                        L Offline
                        L Offline
                        lgmanuel
                        wrote on last edited by
                        #18

                        hi, i have another question? am trying to connect two computers on the network whit the same App that i created, how do i connect these 2? it should prompt the user that the connection is successful? help guys. thanks

                        1 Reply Last reply
                        0
                        • M merano

                          Yes that will do the job. Found an easy TODO for the Timer-Part here ("Using Timers in MFC Applications") In our Sample it would look like this: First update your class in MoveTheButtonDlg.h (Some work is already done by VisualStudio when clicking the Button)

                          enum {DIR_STOP, DIR_LEFT, DIR_RIGHT};

                          // CMoveTheButtonDlg dialog
                          class CMoveTheButtonDlg : public CDialog
                          {

                          ...

                          DECLARE\_MESSAGE\_MAP()
                          

                          public:
                          afx_msg void OnBnClickedMoveright();
                          afx_msg void OnBnClickedStop();
                          afx_msg void OnBnClickedMoveleft();
                          afx_msg void OnTimer(UINT TimerVal); // TI: Add timer handler to messagemap

                          private:
                          BOOL StartTimer (UINT TimerDuration);
                          BOOL StopTimer(void);

                          UINT\_PTR m\_nTimer;
                          UINT  m\_Direction;
                          DWORD m\_winpos;
                          

                          };

                          After that you have to declare Message-Map:

                          BEGIN_MESSAGE_MAP(CMoveTheButtonDlg, CDialog)
                          ON_WM_SYSCOMMAND()
                          ON_WM_PAINT()
                          ON_WM_QUERYDRAGICON()
                          //}}AFX_MSG_MAP
                          ON_BN_CLICKED(IDC_MoveRight, &CMoveTheButtonDlg::OnBnClickedMoveright)
                          ON_BN_CLICKED(IDC_Stop, &CMoveTheButtonDlg::OnBnClickedStop)
                          ON_BN_CLICKED(IDC_MoveLeft, &CMoveTheButtonDlg::OnBnClickedMoveleft)
                          ON_WM_TIMER ( ) // TI: timer implementation
                          END_MESSAGE_MAP()

                          At last the new members have do be defined:

                          void CMoveTheButtonDlg::OnBnClickedStop()
                          {
                          m_Direction = DIR_STOP;
                          StopTimer();
                          }

                          void CMoveTheButtonDlg::OnBnClickedMoveright()
                          {
                          m_Direction = DIR_RIGHT;
                          StartTimer(1000);
                          }

                          void CMoveTheButtonDlg::OnBnClickedMoveleft()
                          {
                          m_Direction = DIR_LEFT;
                          StartTimer(1000);
                          }

                          BOOL CMoveTheButtonDlg::StartTimer (UINT TimerDuration)
                          {
                          m_nTimer = SetTimer (IDT_TIMER_0, TimerDuration, NULL);

                          if ( m_nTimer == 0) {
                          MessageBox (_T("Unable to obtain timer"), _T("IDT_TIMER_0"), MB_OK|MB_SYSTEMMODAL);
                          return FALSE;
                          }

                          return TRUE;
                          }// end StartTimer

                          BOOL CMoveTheButtonDlg::StopTimer(void)
                          {
                          if(m_nTimer) {
                          // Stop the timer
                          if (!KillTimer (m_nTimer)) {
                          return FALSE;
                          m_nTimer = 0;
                          }
                          }

                          return TRUE;
                          

                          } // end StopTimer

                          void CMoveTheButtonDlg::OnTimer (UINT TimerVal)
                          {
                          if(TimerVal != m_nTimer) // TI: check what timer
                          return;

                          switch(m\_Direction) {
                          case DIR\_LEFT:
                          	// TI: move to left here
                          	m\_winpos-=5;
                          	break;
                          	
                          case DIR\_RIGHT:
                          	// TI: move to right here
                          	m\_winpos+=5;
                          	break;
                          }
                          

                          }

                          I

                          L Offline
                          L Offline
                          lgmanuel
                          wrote on last edited by
                          #19

                          how do i disable the left and right button when the window load.. and enable it back when press the center button?

                          M 1 Reply Last reply
                          0
                          • L lgmanuel

                            how do i disable the left and right button when the window load.. and enable it back when press the center button?

                            M Offline
                            M Offline
                            merano
                            wrote on last edited by
                            #20

                            You already have the very good hint from Niklas Lindquist 14Jul'10. SetWindowPos()[^] or MoveWindow()[^] to move the buttons Every Button is based on a Window and SetWindowPos() can do everything with the button (even hide it) if you are able to get the CWnd-handle. See Example: void CWinApp::HideApplication() On the MSDN Online-Page (linked also by Niklas Lindquist) you really should start to read MFC doku (See advice of Cedric Moonen). You can get hints from the Forum, but nobody will do the complete work for you. What is the complete Funktion of your Application ? -If you want networkfunktions you will probably get Timing an Sync Problems. You can send every Button a message to get the same result as if you pressed it, but over Network you will not see the monitor. You have to find a way to send the results back to every client and display it.

                            L 1 Reply Last reply
                            0
                            • M merano

                              You already have the very good hint from Niklas Lindquist 14Jul'10. SetWindowPos()[^] or MoveWindow()[^] to move the buttons Every Button is based on a Window and SetWindowPos() can do everything with the button (even hide it) if you are able to get the CWnd-handle. See Example: void CWinApp::HideApplication() On the MSDN Online-Page (linked also by Niklas Lindquist) you really should start to read MFC doku (See advice of Cedric Moonen). You can get hints from the Forum, but nobody will do the complete work for you. What is the complete Funktion of your Application ? -If you want networkfunktions you will probably get Timing an Sync Problems. You can send every Button a message to get the same result as if you pressed it, but over Network you will not see the monitor. You have to find a way to send the results back to every client and display it.

                              L Offline
                              L Offline
                              lgmanuel
                              wrote on last edited by
                              #21

                              yup., one of my nerd friends is helping on the networking side of the app.. needs to read about Socket Buffer and Listening.. anyways., got a lot of hints from you guys on how to start my C++ adventure, hope it works for me., am trying to change my programming path from VB to C++., i wish myself good luck. Thank you again.

                              1 Reply Last reply
                              0
                              • C Cedric Moonen

                                You should really buy a book about VC++. It is nearly impossible to help someone who has almost no knowledge of VC++ or C++ through a forum. It will take ages to explain some basic concepts. You will be much more efficient by reading a book first.

                                Cédric Moonen Software developer
                                Charting control [v3.0] OpenGL game tutorial in C++

                                T Offline
                                T Offline
                                ThatsAlok
                                wrote on last edited by
                                #22

                                who the f*** voted you down here! let me even it

                                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                                Never mind - my own stupidity is the source of every "problem" - Mixture

                                cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                                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