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. Drawing on Dialog using DC

Drawing on Dialog using DC

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
6 Posts 3 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.
  • Y Offline
    Y Offline
    YaronNir
    wrote on last edited by
    #1

    Hi all, i have a dialog, i catch the OnPaint() message. here is the code

    void CMyDialog::OnPaint()
    {
    CPaintDC dc(this); // device context for painting
    GetWindowRect(m_rectDlg);
    PlaceTitleBorder(dc);
    }

    in in the PlaceTitleBorder() method i draw a bitmap:

    void CMyDialog::PlaceTitleBorder(CPaintDC &dc)
    {
    BITMAP bmp;
    memset(&bmp,0,sizeof(bmp));
    ::GetObject(m_hTitleBorder,sizeof(bmp),&bmp);
    HDC compatibleDC = ::CreateCompatibleDC(dc);
    HBITMAP hSavedObj;
    hSavedObj = (HBITMAP)::SelectObject(compatibleDC,m_hTitleBorder);
    BOOL bRes = StretchBlt(dc,
    m_rectTitleBorder->left,
    m_rectTitleBorder->top,
    m_rectTitleBorder->Width(),
    m_rectTitleBorder->Height(),
    compatibleDC,
    0,
    0,
    bmp.bmWidth,
    bmp.bmHeight,
    SRCCOPY);
    ::SelectObject(compatibleDC,hSavedObj);
    ::DeleteDC(compatibleDC);
    }

    when dialog appear i see the bitmap ok. when i move the dialog around the screen and go out of the bounds of the screen, and then return to the screen, the bitmap disappears (not redrawn???) can any1 help me?? thanks in advanced Yaron Ask not what your application can do for you, Ask what you can do for your application

    V C 2 Replies Last reply
    0
    • Y YaronNir

      Hi all, i have a dialog, i catch the OnPaint() message. here is the code

      void CMyDialog::OnPaint()
      {
      CPaintDC dc(this); // device context for painting
      GetWindowRect(m_rectDlg);
      PlaceTitleBorder(dc);
      }

      in in the PlaceTitleBorder() method i draw a bitmap:

      void CMyDialog::PlaceTitleBorder(CPaintDC &dc)
      {
      BITMAP bmp;
      memset(&bmp,0,sizeof(bmp));
      ::GetObject(m_hTitleBorder,sizeof(bmp),&bmp);
      HDC compatibleDC = ::CreateCompatibleDC(dc);
      HBITMAP hSavedObj;
      hSavedObj = (HBITMAP)::SelectObject(compatibleDC,m_hTitleBorder);
      BOOL bRes = StretchBlt(dc,
      m_rectTitleBorder->left,
      m_rectTitleBorder->top,
      m_rectTitleBorder->Width(),
      m_rectTitleBorder->Height(),
      compatibleDC,
      0,
      0,
      bmp.bmWidth,
      bmp.bmHeight,
      SRCCOPY);
      ::SelectObject(compatibleDC,hSavedObj);
      ::DeleteDC(compatibleDC);
      }

      when dialog appear i see the bitmap ok. when i move the dialog around the screen and go out of the bounds of the screen, and then return to the screen, the bitmap disappears (not redrawn???) can any1 help me?? thanks in advanced Yaron Ask not what your application can do for you, Ask what you can do for your application

      V Offline
      V Offline
      Vitali Halershtein
      wrote on last edited by
      #2

      Hi YaronNir, I think this is not good way to rewrite OnPaint in dialog (hmmm - f.e. because dialog may contain other elements). Insted this let try to put cpecial element on the dialog and draw bitmap in to it. Hope this help. Vitali http://www.creative-case.com[^]

      Y 1 Reply Last reply
      0
      • V Vitali Halershtein

        Hi YaronNir, I think this is not good way to rewrite OnPaint in dialog (hmmm - f.e. because dialog may contain other elements). Insted this let try to put cpecial element on the dialog and draw bitmap in to it. Hope this help. Vitali http://www.creative-case.com[^]

        Y Offline
        Y Offline
        YaronNir
        wrote on last edited by
        #3

        hi, thanks for the reply... What can you suggest i do ?? Ask not what your application can do for you, Ask what you can do for your application

        V 1 Reply Last reply
        0
        • Y YaronNir

          Hi all, i have a dialog, i catch the OnPaint() message. here is the code

          void CMyDialog::OnPaint()
          {
          CPaintDC dc(this); // device context for painting
          GetWindowRect(m_rectDlg);
          PlaceTitleBorder(dc);
          }

          in in the PlaceTitleBorder() method i draw a bitmap:

          void CMyDialog::PlaceTitleBorder(CPaintDC &dc)
          {
          BITMAP bmp;
          memset(&bmp,0,sizeof(bmp));
          ::GetObject(m_hTitleBorder,sizeof(bmp),&bmp);
          HDC compatibleDC = ::CreateCompatibleDC(dc);
          HBITMAP hSavedObj;
          hSavedObj = (HBITMAP)::SelectObject(compatibleDC,m_hTitleBorder);
          BOOL bRes = StretchBlt(dc,
          m_rectTitleBorder->left,
          m_rectTitleBorder->top,
          m_rectTitleBorder->Width(),
          m_rectTitleBorder->Height(),
          compatibleDC,
          0,
          0,
          bmp.bmWidth,
          bmp.bmHeight,
          SRCCOPY);
          ::SelectObject(compatibleDC,hSavedObj);
          ::DeleteDC(compatibleDC);
          }

          when dialog appear i see the bitmap ok. when i move the dialog around the screen and go out of the bounds of the screen, and then return to the screen, the bitmap disappears (not redrawn???) can any1 help me?? thanks in advanced Yaron Ask not what your application can do for you, Ask what you can do for your application

          C Offline
          C Offline
          csc
          wrote on last edited by
          #4

          I'm doing that too, but it works. Even if the dialog needs a refresh / redraw. Just to show an example, which should not look very strange from your point of view : 1) Define the handlers for InitDialog and OnPaint and add a CBitmap object in the dialogs header : class CMyDialog : public CDialog { : virtual BOOL OnInitDialog(); afx_msg void OnPaint(); : CBitmap m_bmMyBitmap; : }; 2) make sure that the OnPaint:handler are in the CPP-File : BEGIN_MESSAGE_MAP(CMyDialog, CDialog) //{{AFX_MSG_MAP(CMyDialog) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() 3) Init the Bitmap object in the OnInitDialog function BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); m_bmMyBitmap.LoadBitmap( IDB_MY_BITMAP ); } 4) Fill the OnPaint-Handler : void CDialogDruckauswahl::OnPaint() { CPaintDC dc(this); // device context for painting // fill dialog background with some color..... CRect crRect; GetClientRect( &crRect ); COLORREF crColor = RGB( 255,255,255 ); CBrush* pFillBrush = new CBrush( crColor ); CBrush* pOldBrush = dc.SelectObject( pFillBrush ); CPen* pNewPen = new CPen( PS_SOLID, 1, crColor ); CPen* pOldPen = dc.SelectObject( pNewPen ); dc.Rectangle( crRect ); dc.SelectObject( pOldPen ); delete pNewPen; pNewPen = NULL; dc.SelectObject( pOldBrush ); delete pFillBrush; pFillBrush = NULL; // in welchen Bereich ? DrawBitmap( &dc, crRect, &m_bmMyBitmap ); } 5) at least : define the bitmap drawing function : void CMyDialog::DrawBitmap( CDC* pDC, CRect crRect, CBitmap* pBM ) { // create a memory dc to prepare drawing via bitblt ... CDC* pmemDCBitBlt = new CDC(); pmemDCBitBlt->CreateCompatibleDC( pDC ); CBitmap* pOldBM = pmemDCBitBlt->SelectObject( pBM ); // blit the bitmap into our view using the bitblt-xor-mode pDC->BitBlt(crRect.TopLeft().x, crRect.TopLeft().y, crRect.Width(), crRect.Height(), pmemDCBitBlt, 0, 0, SRCCOPY ); // free the memory dc and the bitmap .... pmemDCBitBlt->SelectObject( pOldBM ); delete pmemDCBitBlt; pmemDCBitBlt = NULL; }

          Y 1 Reply Last reply
          0
          • C csc

            I'm doing that too, but it works. Even if the dialog needs a refresh / redraw. Just to show an example, which should not look very strange from your point of view : 1) Define the handlers for InitDialog and OnPaint and add a CBitmap object in the dialogs header : class CMyDialog : public CDialog { : virtual BOOL OnInitDialog(); afx_msg void OnPaint(); : CBitmap m_bmMyBitmap; : }; 2) make sure that the OnPaint:handler are in the CPP-File : BEGIN_MESSAGE_MAP(CMyDialog, CDialog) //{{AFX_MSG_MAP(CMyDialog) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() 3) Init the Bitmap object in the OnInitDialog function BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); m_bmMyBitmap.LoadBitmap( IDB_MY_BITMAP ); } 4) Fill the OnPaint-Handler : void CDialogDruckauswahl::OnPaint() { CPaintDC dc(this); // device context for painting // fill dialog background with some color..... CRect crRect; GetClientRect( &crRect ); COLORREF crColor = RGB( 255,255,255 ); CBrush* pFillBrush = new CBrush( crColor ); CBrush* pOldBrush = dc.SelectObject( pFillBrush ); CPen* pNewPen = new CPen( PS_SOLID, 1, crColor ); CPen* pOldPen = dc.SelectObject( pNewPen ); dc.Rectangle( crRect ); dc.SelectObject( pOldPen ); delete pNewPen; pNewPen = NULL; dc.SelectObject( pOldBrush ); delete pFillBrush; pFillBrush = NULL; // in welchen Bereich ? DrawBitmap( &dc, crRect, &m_bmMyBitmap ); } 5) at least : define the bitmap drawing function : void CMyDialog::DrawBitmap( CDC* pDC, CRect crRect, CBitmap* pBM ) { // create a memory dc to prepare drawing via bitblt ... CDC* pmemDCBitBlt = new CDC(); pmemDCBitBlt->CreateCompatibleDC( pDC ); CBitmap* pOldBM = pmemDCBitBlt->SelectObject( pBM ); // blit the bitmap into our view using the bitblt-xor-mode pDC->BitBlt(crRect.TopLeft().x, crRect.TopLeft().y, crRect.Width(), crRect.Height(), pmemDCBitBlt, 0, 0, SRCCOPY ); // free the memory dc and the bitmap .... pmemDCBitBlt->SelectObject( pOldBM ); delete pmemDCBitBlt; pmemDCBitBlt = NULL; }

            Y Offline
            Y Offline
            YaronNir
            wrote on last edited by
            #5

            Hi, thanks for the reply, i wrote something similar to what you've posted and it works! thanks a lot Yaron Ask not what your application can do for you, Ask what you can do for your application

            1 Reply Last reply
            0
            • Y YaronNir

              hi, thanks for the reply... What can you suggest i do ?? Ask not what your application can do for you, Ask what you can do for your application

              V Offline
              V Offline
              Vitali Halershtein
              wrote on last edited by
              #6

              I usualy put some element in the dialog such as image or button and put image in to it. Vitali http://www.creative-case.com[^]

              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