Thread: Using HDC to initialize data structure

  1. #1
    Registered User
    Join Date
    Jul 2011

    Using HDC to initialize data structure

    I need to create a compatible DC and compute a font height. Both of these initialization steps require a handle to the device context called HDC.

    HDC hdc = GetDC(NULL);
    dcImage = CreateCompatibleDC(hdc);
    lfHeight = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    ReleaseDC(NULL, hdc);
    Should I create them in InitInstance or WM_CREATE? In my case I create the dcImage and lfHeight in the InitInstance routine.
    On the WM_CREATE I finish the initialization procedure by using the SelectObject to finish the initilizaition of the device context called dcImage. It seems a bit artbitary because I could it either way.

  2. #2
    train spotter
    Join Date
    Aug 2001
    near a computer
    InitInstance is MFC and runs at the application level, not the window level.
    Your issue is that the app has not yet created a window and failing to return TRUE or init the main app HWND pointer will cause serious issues.

    OnCreate (WM_CREATE or WM_INITDIALOG) is run after the CWnd has created a window/dialog but not yet shown it.

    So IMO put your window/dlg specific code in the OnCreate (WM_CREATE for windows or WM_INITDIALOG for dlgs).
    If you have multiple windows/dlgs it becomes more important to track which DCs belong to which (especially as the DCs should be members of the individual CWnd object not the CWinApp object).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jul 2011
    Quote Originally Posted by novacain View Post
    InitInstance is MFC and runs at the application level, not the window level.
    I don't know what this means. I used Visual C++ 2011 expression to create a new C++ project.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You can always consult the people who made the thing: InitInstance Member Function

    Quoting linked page:
    Quote Originally Posted by MSDN
    WinMain calls InitInstance every time a new instance of the application starts.
    So InitInstance runs once per application, not once per window.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between data object and data structure?
    By c_lady in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2011, 12:30 PM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. using a class as a data member before you initialize the class?
    By class question in forum C++ Programming
    Replies: 6
    Last Post: 01-05-2003, 04:25 PM
  5. using constructor to initialize data
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2002, 08:55 PM