How to Move an MFC Button. [modified]
-
..... 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
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);
-
..... 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
-
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);
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..
-
Use a timer[^] for the updates and call SetWindowPos()[^] or MoveWindow()[^] to move the buttons.
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 messagemapprivate:
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 StartTimerBOOL 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
-
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 messagemapprivate:
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 StartTimerBOOL 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
-
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 messagemapprivate:
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 StartTimerBOOL 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
-
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 messagemapprivate:
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 StartTimerBOOL 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
-
how do i disable the left and right button when the window load.. and enable it back when press the center button?
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. -
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.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.
-
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++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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You