hello,

google, daniweb, and microsoft searches didn't help me with this so i hope that someone here will know.

i'm trying to have a method called at a fixed interval of time from the time an application starts until it ends ( RedrawWindow() ). i know how to do it in Java (with ScheduledThreadPoolExecutor), but i can only find how to do single execution of a method of a thread in MFC C++ (afxBeginThread).

i thought about just making a new thread to call RedrawWindow() from inside RedrawWindow() but i thought(/think) that that would be a very unsafe operation, and would be quite hard to stop.

see SetTimer(). With SetTimer() you can have windows call a function of your choicel at set time intervals.

that seems perfect, but i need to figure out how to call RedrawWindow() from inside the non-member callback, how can i take the HWND and make it into something that i can call the view's function from?

it's not doing anything, i am probably missing something, here's the relevant parts of the code:

void CALLBACK EXPORT TimerProc( HWND hWnd, UINT nMsg, UINT_PTR nIDEvent, DWORD dwTime );

BEGIN_MESSAGE_MAP(CGDI1View, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_BN_CLICKED(1,&OnButton1Clicked)
END_MESSAGE_MAP()


void CGDI1View::OnInitialUpdate(){
	button1Rect.SetRect(250,50,400,100);
	button1.Create(_T("HI"),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,button1Rect,this,1);
	m_Timer1 = SetTimer(1,33,&(TimerProc));
}

void CGDI1View::OnDraw(CDC* pDC){
	CGDI1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CRect *dims = new CRect;
	pDC->GetClipBox(dims);
	button1.MoveWindow(dims->right-150,0,150,50,0);
	//AfxMessageBox(_T("") + ,MB_OK)
	pDC->MoveTo(pDoc->x,pDoc->y);
	pDC->LineTo(pDoc->x+10,pDoc->y+10);
	pDC->LineTo(pDoc->x+10,pDoc->y);
	pDC->LineTo(pDoc->x,pDoc->y);
	pDC->SetPixel(pDoc->x,pDoc->y,RGB(0,255,0));
	pDC->SetPixel(pDoc->x+10,pDoc->y,RGB(0,255,0));
	pDC->SetPixel(pDoc->x+10,pDoc->y+10,RGB(0,255,0));
	pDoc->step();
}

void CGDI1View::OnButton1Clicked(){
	//AfxMessageBox(_T("you Clicked button1"),MB_OK);
	CGDI1Doc* pDoc = GetDocument();
	pDoc->x=10;
	pDoc->y=10;
	KillTimer(m_Timer1);
}

void CALLBACK EXPORT TimerProc( HWND hWnd, UINT nMsg, UINT_PTR nIDEvent, DWORD dwTime ){
RedrawWindow(hWnd,NULL,NULL,RDW_ALLCHILDREN);
}
void CGDI1Doc::step(){
	x+=1;
	y+=1;
}

is there a message i am supposed to add...

it's not doing anything, i am probably missing something, here's the relevant parts of the code:

void CALLBACK EXPORT TimerProc( HWND hWnd, UINT nMsg, UINT_PTR nIDEvent, DWORD dwTime );

BEGIN_MESSAGE_MAP(CGDI1View, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_BN_CLICKED(1,&OnButton1Clicked)
END_MESSAGE_MAP()


void CGDI1View::OnInitialUpdate(){
	button1Rect.SetRect(250,50,400,100);
	button1.Create(_T("HI"),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,button1Rect,this,1);
	m_Timer1 = SetTimer(1,33,&(TimerProc));
}

void CGDI1View::OnDraw(CDC* pDC){
	CGDI1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CRect *dims = new CRect;
	pDC->GetClipBox(dims);
	button1.MoveWindow(dims->right-150,0,150,50,0);
	//AfxMessageBox(_T("") + ,MB_OK)
	pDC->MoveTo(pDoc->x,pDoc->y);
	pDC->LineTo(pDoc->x+10,pDoc->y+10);
	pDC->LineTo(pDoc->x+10,pDoc->y);
	pDC->LineTo(pDoc->x,pDoc->y);
	pDC->SetPixel(pDoc->x,pDoc->y,RGB(0,255,0));
	pDC->SetPixel(pDoc->x+10,pDoc->y,RGB(0,255,0));
	pDC->SetPixel(pDoc->x+10,pDoc->y+10,RGB(0,255,0));
	pDoc->step();
}

void CGDI1View::OnButton1Clicked(){
	//AfxMessageBox(_T("you Clicked button1"),MB_OK);
	CGDI1Doc* pDoc = GetDocument();
	pDoc->x=10;
	pDoc->y=10;
	KillTimer(m_Timer1);
}

void CALLBACK EXPORT TimerProc( HWND hWnd, UINT nMsg, UINT_PTR nIDEvent, DWORD dwTime ){
RedrawWindow(hWnd,NULL,NULL,RDW_ALLCHILDREN);
}
void CGDI1Doc::step(){
	x+=1;
	y+=1;
}

is there a message i am supposed to add...

I'm not quite sure what you actually expect to happen, but I think you maybe want to add the RDW_INVALIDATE flag to the RedrawWindow() call.

oh, that worked, what exactly does it mean though?

oh, that worked, what exactly does it mean though?

Simply put, it invalidates the view so that its OnDraw() function gets called.

By the way, the OnDraw() leaks memory since you are not deleting the CRect that you allocate there.

oh, ok, i'll fix that

CRect is only 4 integers, so why bother allocating it at all. Just declare it without allocation and save yourself the bother of finding memory leeks.

i thought that

CRect *dims = new CRect;
pDC->GetClipBox(dims);

in order for GetClipBox to change it it had to be a pointer, could i have just passed &dims? i messed around a lot with it until it got the button to stick to the right side of the screen, the code i showed is the code that worked, once i got it to work i didn't think of looking for easier ways to go about the stuff i'm still new to VC++/C++/MFC

>> could i have just passed &dims?
Yes. Don't dynamically allocate things unless you absolutely have to.

ok, thanks for the advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.