Sign in to follow this  
lucky6969b

Drawing Grid Lines on MFC

Recommended Posts

 
void CPinterView::InitializeGrid()
{
DrawGrid();
}

void CPinterView::DrawGrid()
{
int Spacing = gEnd / gGridNum;
int i;
CDC *dc = GetDC();
CPen newPen(PS_SOLID, 5, RGB(0,0,255));
CPen *oldPen = dc->SelectObject(&newPen);
for (i = 0; i < gEnd; i+=Spacing);
{

dc->MoveTo(i,0);
dc->LineTo(i,gEnd);
}
for (int j = 0; j < gEnd; j+=Spacing)
{
dc->MoveTo(0,j);
dc->LineTo(gEnd,j);
}

dc->SelectObject(oldPen);
Invalidate(true);

}



I have initialized gEnd(5000),Spacing and gGridNum(30)
I have called the function from OnCreate of CView
But I can't see anything from the view.
Any ideas?
Thanks
Jack

Share this post


Link to post
Share on other sites
The painting in a view should be called inside OnDraw. You should NOT draw onto its DC outside of a paint call.

Right now your grid drawing is done, and then you call InvalidateRect. MFC in turn will call the OnDraw function drawing over your grid lines again.

Put your DrawGrid call inside OnDraw near the end.

Share this post


Link to post
Share on other sites
Quote:
You should NOT draw onto its DC outside of a paint call.
Not always strictly true, but that's certainly the OP's problem in this case.

I have to ask the question though ... why on earth use MFC for a game? Using it for anything at all these days is bad enough, but for a game it's even more particularly ill-suited. Have you considered XNA, or even just a pure Win32 program?

Share this post


Link to post
Share on other sites
Quote:
Original post by Echkard
Quote:
You should NOT draw onto its DC outside of a paint call.
Not always strictly true, but that's certainly the OP's problem in this case.

I have to ask the question though ... why on earth use MFC for a game? Using it for anything at all these days is bad enough, but for a game it's even more particularly ill-suited. Have you considered XNA, or even just a pure Win32 program?


Just testing some pathfinding stuff...
Been fiddling around with Pinter's code. Because he has lost part of the source code, tried to rebuild that. So that's the story.
Thanks
Jack

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this