[MFC] Cum punem o imagine in bara de titlu?

Despre MFC, ATL si alte biblioteci C++ de la Microsoft (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3778
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

[MFC] Cum punem o imagine in bara de titlu?

Post by Ovidiu Cucu » 01 Aug 2011, 12:06

Intrebare
Cum punem o imagine in bara de titlu (caption) a unei aplicatii SDI/MDI?
Image in caption.gif
Image in caption.gif (11.65 KiB) Viewed 2283 times
Raspuns
In principiu e simplu: in handlerul mesajului WM_NCPAINT, chemam mai intai functia Default apoi desenam imaginea dorita. In practica, exista o mica problema atunci cand se redimensioneaza o aplicatie MDI cu ferestrele child maximizate. O solutie ar fi ca, in loc sa desenam direct din OnNcPaint, sa postam un mesaj custom pentru a ne asigura ca desenarea imaginii se face la urma.

Exemplu

Code: Select all

// MainFrm.h
// ...
#define WM_APP_CUSTOMCAPTIONDRAW (WM_APP+1)

class CMainFrame : public CMDIFrameWnd
{
// ...
   afx_msg void OnNcPaint();
   afx_msg LRESULT OnAppCustomCaptionDraw(WPARAM wParam, LPARAM lParam);
private:
   void _DrawCaptionImage();
};

Code: Select all

// MainFrm.cpp
// ...
   ON_WM_NCPAINT()
   //}}AFX_MSG_MAP
   ON_MESSAGE(WM_APP_CUSTOMCAPTIONDRAW, OnAppCustomCaptionDraw)
END_MESSAGE_MAP()

void CMainFrame::OnNcPaint() 
{
   // perform default painting
   Default();

   // post a message for custom drawing
   PostMessage(WM_APP_CUSTOMCAPTIONDRAW);
}

LRESULT CMainFrame::OnAppCustomCaptionDraw(WPARAM wParam, LPARAM lParam)
{
   _DrawCaptionImage();
   // other drawing function calls, if any 
   return 0;
}

void CMainFrame::_DrawCaptionImage()
{
   // window and memory DC for drawing
   CWindowDC dc(this);
   CDC dcMem;
   dcMem.CreateCompatibleDC(&dc);

   // load bitmap image and select it in the memory DC
   CBitmap bmpImage;
   bmpImage.LoadBitmap(IDB_LOGO);
   CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(&bmpImage);

   // get the bitmap size
   BITMAP bmp;
   bmpImage.GetBitmap(&bmp);
   const int nBmpWidth = bmp.bmWidth;
   const int nBmpHeight = bmp.bmHeight;

   //get info for positioning image in the caption area
   const int nYCaption = ::GetSystemMetrics(SM_CYCAPTION);
   const int nXFrame = ::GetSystemMetrics(SM_CXFRAME);
   const int nYFrame = ::GetSystemMetrics(SM_CYFRAME);
   const int nXButton = ::GetSystemMetrics(SM_CXSMSIZE);

   // calculate drawing image size and position
   const int nImgHeight = nYCaption - 1;
   const int nImgWidth = (int)(((double)nImgHeight / nBmpHeight) * nBmpWidth);
   CRect rc;
   GetWindowRect(rc);
   const int nImgXPos = rc.Width() - (nXFrame + 3 * nXButton + nImgWidth + 20); 
   const int nImgYPos = nYFrame;
   
   // blit the bitmap in the window DC 
   const int nOldMode = dc.SetStretchBltMode(HALFTONE);
   dc.StretchBlt(nImgXPos, nImgYPos, nImgWidth, nImgHeight, 
      &dcMem, 0, 0, nBmpWidth, nBmpHeight, SRCCOPY);

   // restore DC and free GDI objects
   dc.SetStretchBltMode(nOldMode);
   dcMem.SelectObject(pOldBitmap);
}
Vezi si << Back to MFC index



Post Reply