Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Trouble getting device context

Trouble getting device context

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    theFrenchHornet
    wrote on last edited by
    #1

    I don't know how to correctly acquire the device context for a control. If I put similar code in the OnCtlColor routine where a pointer to the device context is provided, the code works. However, when I try setting the device context, the code doesn't work. Two samples are provided - the first is the code that doesn't work. I appreciate any help fixing it. Thank-you. This code compiles and runs without error, but does not achieve the color change. (c_navValid is a text box control)

    LOGFONT lf;
    CDC* dc = new CDC;
    dc->Attach(c_navValid.GetDC()->m_hDC);

    GetFont()->GetObject(sizeof(lf), &lf);
    font.CreateFontIndirect(&lf);

    dc->SelectObject(&font);
    dc->SetTextColor(RGB(0,100,0));
    font.DeleteObject();

    This code works.

    HBRUSH CNavDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    W_Position_Data_Type* pPositionMsg;
    pPositionMsg = (W_Position_Data_Type*)&CInMsgs::wPositionMsg;

    HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);

    if (pWnd->GetDlgCtrlID() == IDC_NAV_VALID)
    {
    if (pPositionMsg->Mode_Word.Nav_Valid)
    {
    LOGFONT lf;
    GetFont()->GetObject(sizeof(lf), &lf);
    font.CreateFontIndirect(&lf);
    pDC->SelectObject(&font);
    pDC->SetTextColor(RGB(0,100,0));
    font.DeleteObject();
    }
    }

    M 1 Reply Last reply
    0
    • T theFrenchHornet

      I don't know how to correctly acquire the device context for a control. If I put similar code in the OnCtlColor routine where a pointer to the device context is provided, the code works. However, when I try setting the device context, the code doesn't work. Two samples are provided - the first is the code that doesn't work. I appreciate any help fixing it. Thank-you. This code compiles and runs without error, but does not achieve the color change. (c_navValid is a text box control)

      LOGFONT lf;
      CDC* dc = new CDC;
      dc->Attach(c_navValid.GetDC()->m_hDC);

      GetFont()->GetObject(sizeof(lf), &lf);
      font.CreateFontIndirect(&lf);

      dc->SelectObject(&font);
      dc->SetTextColor(RGB(0,100,0));
      font.DeleteObject();

      This code works.

      HBRUSH CNavDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      {
      W_Position_Data_Type* pPositionMsg;
      pPositionMsg = (W_Position_Data_Type*)&CInMsgs::wPositionMsg;

      HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);

      if (pWnd->GetDlgCtrlID() == IDC_NAV_VALID)
      {
      if (pPositionMsg->Mode_Word.Nav_Valid)
      {
      LOGFONT lf;
      GetFont()->GetObject(sizeof(lf), &lf);
      font.CreateFontIndirect(&lf);
      pDC->SelectObject(&font);
      pDC->SetTextColor(RGB(0,100,0));
      font.DeleteObject();
      }
      }

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Use CClientDC or CWindowDC to get a DC for a window. Use the former to draw in the window's client area only, or the latter if you need to draw in the non-client area too. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ Wizard needs food, badly!

      T 1 Reply Last reply
      0
      • M Michael Dunn

        Use CClientDC or CWindowDC to get a DC for a window. Use the former to draw in the window's client area only, or the latter if you need to draw in the non-client area too. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ Wizard needs food, badly!

        T Offline
        T Offline
        theFrenchHornet
        wrote on last edited by
        #3

        Could you please show me the code? To use CClientDC I need a pointer to a window. How do I convert the static control to a window pointer? cCliendDc dc(c_navValid); I know isn't correct. What parameter do I pass to CClientDC to get a dc for the control? Thanks.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups