GetDC not calculating toolbar offset
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: GetDC not calculating toolbar offset

  1. #1
    Join Date
    Aug 2011
    Posts
    8

    GetDC not calculating toolbar offset

    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!

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,546

    Re: GetDC not calculating toolbar offset

    Quote Originally Posted by zetar View Post
    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.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  3. #3
    Join Date
    Aug 2011
    Posts
    8

    Re: GetDC not calculating toolbar offset

    Quote Originally Posted by VladimirF View Post
    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.

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    Join Date
    Aug 2011
    Posts
    8

    Re: GetDC not calculating toolbar offset

    Quote Originally Posted by Skizmo View Post
    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?

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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 ().

    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.

    Though, my OnDraw just calls UpdateDialog(pDC);
    Never draw outside of UpdateDialog?
    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.
    Last edited by Skizmo; August 29th, 2011 at 01:02 PM.

  7. #7
    Join Date
    Aug 2011
    Posts
    8

    Re: GetDC not calculating toolbar offset

    Quote Originally Posted by Skizmo View Post
    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.

    Thanks for setting me straight!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center