I have an app with a floating / docking toolbar. This is a pretty big program. Recently I've added a new function that comes off of the toolbar. This involves some screen draws and screen captures. However, when I do a

pDC = GetDC();

and then

pDC->MoveTo(p1.x, p1.y);
pDC->LineTo(p2.x, p2.y);

the drawing is off, on the Y axis by the height of the toolbar! In other words, the offset of the toolbar to the screen is not being calculated. This is only happening in the 'new function'.

Anybody have any ideas (other than adding 60 to the Y of every call)?

Thanks!

August 29th, 2011, 10:11 AM

VladimirF

Re: GetDC not calculating toolbar offset

Quote:

Originally Posted by zetar

Anybody have any ideas (other than adding 60 to the Y of every call)?

What window are you drawing on? What is its relationship with toolbar window?
My guess is - you are drawing on the wrong window.

August 29th, 2011, 10:25 AM

zetar

Re: GetDC not calculating toolbar offset

Quote:

Originally Posted by VladimirF

What window are you drawing on? What is its relationship with toolbar window?
My guess is - you are drawing on the wrong window.

I'm pretty sure I'm drawing to the right window. I've also tried

pDC = GetWindowDC();

with the same problems.

How would I know if I'm drawing to the wrong window? It seems to be the same window. I can move it around and stuff. Also, if I move the tool bar from the top of the window to the bottom of the window the drawing matches exactly. All my 'new' draw calls are off by exactly the height of the toolbar.

August 29th, 2011, 11:31 AM

Skizmo

Re: GetDC not calculating toolbar offset

Why do you need to get a DC ? You can only draw in the OnPaint, and in it you need to declare

Code:

CPaintDC dc(this);

to create a DC. Drawing outside the OnPaint is simply the wrong way to do it.

August 29th, 2011, 11:43 AM

zetar

Re: GetDC not calculating toolbar offset

Quote:

Originally Posted by Skizmo

Why do you need to get a DC ? You can only draw in the OnPaint, and in it you need to declare

Code:

CPaintDC dc(this);

to create a DC. Drawing outside the OnPaint is simply the wrong way to do it.

That seems to be the case.

In essence, then, the 'correct' way to draw in MFC is ALWAYS on Invalidate(TRUE), yes?

Though, my OnDraw just calls UpdateDialog(pDC);

Never draw outside of UpdateDialog?

August 29th, 2011, 11:59 AM

Skizmo

Re: GetDC not calculating toolbar offset

OnDraw for views, instead of OnPaint for dialogs. The OnDraw function gives you the drawing DC as a parameter of the function, so my question stands, why the GetDC ().

Quote:

In essence, then, the 'correct' way to draw in MFC is ALWAYS on Invalidate(TRUE), yes?

Yes. A window (or view) has only 1 paint function that is doing all the work. If you draw something in another function the OnPaint/OnDraw function automatically overrules that when the window is refreshed.

I don't use view that much so I don't know the UpdateDialog call, but your own drawing stuff can be done in the OnDraw itself.

August 29th, 2011, 12:05 PM

zetar

Re: GetDC not calculating toolbar offset

Quote:

Originally Posted by Skizmo

OnDraw for views, instead of OnPaint for dialogs. The OnDraw function gives you the drawing DC as a parameter of the function, so my question stands, why the GetDC ().

Yes. A window (or view) has only 1 paint function that is doing all the work. If you draw something in another function the OnPaint/OnDraw function automatically overrules that by refreshing the window.

I don't use view that much so I don't know the UpdateDialog call, but your own drawing stuff can be done in the OnDraw itself.

Okay, so I'm beginning to get this. I was drawing outside of OnDraw / UpdateDialog. It just seemed like I was creating (and setting) a ton of global flags for what should be drawn in UpdateDialog. But, that's the only way to do it.