Click here to Skip to main content
15,892,161 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to Move an MFC Button. Pin
lgmanuel13-Jul-10 23:16
lgmanuel13-Jul-10 23:16 
GeneralRe: How to Move an MFC Button. Pin
Niklas L13-Jul-10 23:35
Niklas L13-Jul-10 23:35 
GeneralRe: How to Move an MFC Button. Pin
lgmanuel13-Jul-10 23:43
lgmanuel13-Jul-10 23:43 
GeneralRe: How to Move an MFC Button. Pin
bolivar12314-Jul-10 3:21
bolivar12314-Jul-10 3:21 
GeneralRe: How to Move an MFC Button. Pin
lgmanuel18-Jul-10 17:50
lgmanuel18-Jul-10 17:50 
GeneralRe: How to Move an MFC Button. Pin
merano14-Jul-10 12:54
merano14-Jul-10 12:54 
GeneralRe: How to Move an MFC Button. Pin
lgmanuel18-Jul-10 17:52
lgmanuel18-Jul-10 17:52 
GeneralRe: How to Move an MFC Button. Pin
merano19-Jul-10 2:06
merano19-Jul-10 2:06 
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 hope you can do the rest at "TI: move to ... " at your own
GeneralRe: How to Move an MFC Button. Pin
lgmanuel19-Jul-10 19:13
lgmanuel19-Jul-10 19:13 
GeneralRe: How to Move an MFC Button. Pin
lgmanuel19-Jul-10 19:18
lgmanuel19-Jul-10 19:18 
GeneralRe: How to Move an MFC Button. Pin
lgmanuel19-Jul-10 20:41
lgmanuel19-Jul-10 20:41 
AnswerRe: How to Move an MFC Button. Pin
merano19-Jul-10 22:11
merano19-Jul-10 22:11 
GeneralRe: How to Move an MFC Button. Pin
lgmanuel19-Jul-10 22:40
lgmanuel19-Jul-10 22:40 
Questionregular expressins performance Pin
yftah198913-Jul-10 22:20
yftah198913-Jul-10 22:20 
AnswerRe: regular expressins performance Pin
Niklas L13-Jul-10 22:32
Niklas L13-Jul-10 22:32 
AnswerRe: regular expressins performance Pin
CPallini13-Jul-10 23:20
mveCPallini13-Jul-10 23:20 
QuestionHow to use progress bar in Property Sheet. Pin
cancerion13-Jul-10 21:23
cancerion13-Jul-10 21:23 
QuestionCan i set Float value to CLabelControl::SetCaption() Pin
raju_shiva13-Jul-10 19:24
raju_shiva13-Jul-10 19:24 
AnswerRe: Can i set Float value to CLabelControl::SetCaption() Pin
chandu00413-Jul-10 20:06
chandu00413-Jul-10 20:06 
QuestionListbox Selection Pin
T.RATHA KRISHNAN13-Jul-10 18:48
T.RATHA KRISHNAN13-Jul-10 18:48 
AnswerRe: Listbox Selection Pin
CPallini13-Jul-10 20:33
mveCPallini13-Jul-10 20:33 
GeneralRe: Listbox Selection Pin
T.RATHA KRISHNAN13-Jul-10 20:46
T.RATHA KRISHNAN13-Jul-10 20:46 
GeneralRe: Listbox Selection Pin
CPallini13-Jul-10 21:03
mveCPallini13-Jul-10 21:03 
GeneralRe: Listbox Selection Pin
T.RATHA KRISHNAN13-Jul-10 21:07
T.RATHA KRISHNAN13-Jul-10 21:07 
QuestionRe: Listbox Selection Pin
CPallini13-Jul-10 21:48
mveCPallini13-Jul-10 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.