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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Adding items to the title bar

Adding items to the title bar

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsquestion
6 Posts 3 Posters 9 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.
  • H Offline
    H Offline
    hans sch
    wrote on last edited by
    #1

    Is it possible to add icons to a dialog window's title bar? I'd like to add some LED-style status indicators to a dialog-based MFC application which should be shown next to the close/maximize/minimize icons. I imagine it's to do with non-client area painting but I couldn't find sample code. I don't want to create the whole title bar - there are examples around for that - but just add an icon or a bitmap.

    K H 2 Replies Last reply
    0
    • H hans sch

      Is it possible to add icons to a dialog window's title bar? I'd like to add some LED-style status indicators to a dialog-based MFC application which should be shown next to the close/maximize/minimize icons. I imagine it's to do with non-client area painting but I couldn't find sample code. I don't want to create the whole title bar - there are examples around for that - but just add an icon or a bitmap.

      K Offline
      K Offline
      KingsGambit
      wrote on last edited by
      #2

      Check if WM_NCPAINT message helps: http://msdn.microsoft.com/en-us/library/dd145212(VS.85).aspx[^] http://www.codeguru.com/cpp/w-d/dislog/titlebar/article.php/c1987[^]

      H 1 Reply Last reply
      0
      • K KingsGambit

        Check if WM_NCPAINT message helps: http://msdn.microsoft.com/en-us/library/dd145212(VS.85).aspx[^] http://www.codeguru.com/cpp/w-d/dislog/titlebar/article.php/c1987[^]

        H Offline
        H Offline
        hans sch
        wrote on last edited by
        #3

        It didn't really help. I assume the sample code should go into an override of CWnd::DefWindowProc, so I tried this:

        /*virtual*/ LRESULT CMyDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
        {
        if (message == WM_NCPAINT)
        {
        LRESULT lRes = CDialog::DefWindowProc(message, wParam, lParam);
        HDC hdc = ::GetDCEx(GetSafeHwnd(), (HRGN)wParam, DCX_WINDOW|DCX_INTERSECTRGN);
        // Paint into this DC
        if (hdc)
        {
        CDC dc;
        dc.Attach(hdc);
        CBrush brush(HS_FDIAGONAL, RGB(255, 255, 255));
        dc.FillRgn(CRgn::FromHandle((HRGN)wParam), &brush);
        }
        ::ReleaseDC(GetSafeHwnd(), hdc);
        return lRes;
        }
        return CDialog::DefWindowProc(message, wParam, lParam);
        }

        A breakpoint on the 'if (hdc)' line would be hit but a breakpoint on the 'CDC dc' line wouldn't, so hdc seems to be 0 all the time :-( Anyway, thanks for the reply!

        modified on Saturday, April 24, 2010 3:52 PM

        E 1 Reply Last reply
        0
        • H hans sch

          It didn't really help. I assume the sample code should go into an override of CWnd::DefWindowProc, so I tried this:

          /*virtual*/ LRESULT CMyDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
          {
          if (message == WM_NCPAINT)
          {
          LRESULT lRes = CDialog::DefWindowProc(message, wParam, lParam);
          HDC hdc = ::GetDCEx(GetSafeHwnd(), (HRGN)wParam, DCX_WINDOW|DCX_INTERSECTRGN);
          // Paint into this DC
          if (hdc)
          {
          CDC dc;
          dc.Attach(hdc);
          CBrush brush(HS_FDIAGONAL, RGB(255, 255, 255));
          dc.FillRgn(CRgn::FromHandle((HRGN)wParam), &brush);
          }
          ::ReleaseDC(GetSafeHwnd(), hdc);
          return lRes;
          }
          return CDialog::DefWindowProc(message, wParam, lParam);
          }

          A breakpoint on the 'if (hdc)' line would be hit but a breakpoint on the 'CDC dc' line wouldn't, so hdc seems to be 0 all the time :-( Anyway, thanks for the reply!

          modified on Saturday, April 24, 2010 3:52 PM

          E Offline
          E Offline
          Emilio Garavaglia
          wrote on last edited by
          #4

          hummm .... see the comments at http://msdn.microsoft.com/en-us/library/dd145212%28VS.85%29.aspx#CommunityContent[^] Looks like MSDN didn't tell the whole story: Just copied from the user commnt

          Getting the device context when handling WM_NCPAINT does not work as described. Additional undocumented flag 0x10000 has to be used:

          case WM_NCPAINT:
          {
          HDC hdc;
          hdc = GetDCEx(hwnd, (HRGN)wParam, DCX_WINDOW | DCX_INTERSECTRGN | 0x10000);
          // Paint into this DC
          ReleaseDC(hwnd, hdc);
          }

          (Verified on Windows XP)

          2 bugs found. > recompile ... 65534 bugs found. :doh:

          H 1 Reply Last reply
          0
          • E Emilio Garavaglia

            hummm .... see the comments at http://msdn.microsoft.com/en-us/library/dd145212%28VS.85%29.aspx#CommunityContent[^] Looks like MSDN didn't tell the whole story: Just copied from the user commnt

            Getting the device context when handling WM_NCPAINT does not work as described. Additional undocumented flag 0x10000 has to be used:

            case WM_NCPAINT:
            {
            HDC hdc;
            hdc = GetDCEx(hwnd, (HRGN)wParam, DCX_WINDOW | DCX_INTERSECTRGN | 0x10000);
            // Paint into this DC
            ReleaseDC(hwnd, hdc);
            }

            (Verified on Windows XP)

            2 bugs found. > recompile ... 65534 bugs found. :doh:

            H Offline
            H Offline
            hans sch
            wrote on last edited by
            #5

            Thanks emilio_grv, that does make a change. Now the hdc isn't always 0. However, just calling GetDCEx and ReleaseDC (with no code between them) makes the window update so slow that most of the time, parts of the window are not properly redrawn...

            1 Reply Last reply
            0
            • H hans sch

              Is it possible to add icons to a dialog window's title bar? I'd like to add some LED-style status indicators to a dialog-based MFC application which should be shown next to the close/maximize/minimize icons. I imagine it's to do with non-client area painting but I couldn't find sample code. I don't want to create the whole title bar - there are examples around for that - but just add an icon or a bitmap.

              H Offline
              H Offline
              hans sch
              wrote on last edited by
              #6

              Thanks to all who helped me. I finally found a solution. 1. Insert the ON_WM_NCPAINT() macro into the dialog's message map, and create an overwritten OnNcPaint method. 2. In OnNcPaint, first call the derived OnNcPaint method, then get the paint DC by calling GetWindowDC(), and paint into the DC whatever you want to paint. That's it. It is a good idea to exit the function before painting if IsWindowVisible() returns false, or if IsIconic() returns true, because there is no visible title bar to paint on. If you don't want to paint over the close, minimize, etc. icons, you can retrieve their positions by calling GetTitleBarInfoEx. This function is unfortunately not defined in CWnd, so you must define it as

              BOOL GetTitleBarInfoEx(HWND hWnd, PTITLEBARINFOEX pstInfo)
              {
              return ::SendMessage(hWnd, WM_GETTITLEBARINFOEX, 0, (LPARAM)pstInfo);
              }

              GetTitleBarInfoEx will return FALSE unless running Vista and up, so you must check its return value and call GetTitleBarInfo if the call to GetTitleBarInfoEx is not successful. GetTitleBarInfo is a member of CWnd :-) If you want to put a flashlight on the title bar, create a timer event which changes a flag (which indicates whether the light is on or off) and then calls RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE);. This will post the WM_NCPAINT message. What I wrote is for CDialog derived classes. I read (but I haven't tried it) that for MDI applications, you must insert this functionality into the main frame.

              modified on Friday, June 18, 2010 3:50 AM

              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