[MFC] Cum punem o imagine in statusbar?

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 statusbar?

Post by Ovidiu Cucu » 18 Jul 2011, 08:58

Intrebare
Cum punem o imagine in statusbar-ul unei aplicatii SDI/MDI?
Statusbar Application LOGO.gif
Statusbar Application LOGO.gif (2.7 KiB) Viewed 2249 times
Solutie
  1. Derivam din CStatusBar o clasa, sa-i zicem CAppStatusBar.
  2. Includem AppStatusBar.h si inlocuim CStatusBar cu CMyStatusBar pentru membrul CMainFrame::m_wndStatusBar.

    Code: Select all

    // MainFrm.h
    // ...
    #include "MyStatusBar.h"
    
    class CMainFrame : public CMDIFrameWnd
    {
       //...
    protected:  // control bar embedded members
       CMyStatusBar  m_wndStatusBar;
       //...
    };
    
  3. In fisierul de implementare al clasei CMainFrame adaugam un element la indicators[], de exemplu ID_INDICATOR_LOGO.

    Code: Select all

    // MainFrm.cpp
    //...
    static UINT indicators[] =
    {
       ID_SEPARATOR,       // status line indicator
       ID_INDICATOR_CAPS,
       ID_INDICATOR_NUM,
       ID_INDICATOR_SCRL,
       ID_INDICATOR_LOGO,  // image pane ID
    };
  4. In editorul de resurse adaugam un string cu id-ul ID_INDICATOR_LOGO. Framework-ul MFC va dimensioana pane-ul in functie de lungimea acestui string.
  5. In CMainFrame::OnCreate, facem ownerdraw pane-ul pentru imagine.

    Code: Select all

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
       // ...
       // set ownerdraw statusbar's pane
       m_wndStatusBar.SetPaneStyle(4, SBT_OWNERDRAW); 
       // Note: magic number '4' is the index in 'indicators' array
          
       return 0;
    }
    
  6. In sfarsit, in CAppStatusBar suprascriem functia virtuala CStatusBar::DrawItem si desenam imaginea.

    Code: Select all

    // AppStatusBar.h
    //...
    class CAppStatusBar : public CStatusBar
    {
       CBitmap m_bmpLogo;
    //...
    // Overrides
       virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
    //...
    };

    Code: Select all

    // AppStatusBar.cpp
    //...
    CAppStatusBar::CAppStatusBar()
    {
       m_bmpLogo.LoadBitmap(IDB_LOGO);
    }
    //...
    void CMyStatusBar::DrawItem(LPDRAWITEMSTRUCT lpDIS)
    {
       CDC dcMem;
       CDC* pdcItem = CDC::FromHandle(lpDIS->hDC);
       dcMem.CreateCompatibleDC(pdcItem);
       CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(&m_bmpLogo);
       BITMAP bmp;
       m_bmpLogo.GetBitmap(&bmp);
    
    
       CRect rcItem(lpDIS->rcItem);
       const int nOldMode = pdcItem->SetStretchBltMode(HALFTONE);
       pdcItem->StretchBlt(rcItem.left, rcItem.top, rcItem.Width(), rcItem.Height(),
          &dcMem, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
    
       pdcItem->SetStretchBltMode(nOldMode);
       dcMem.SelectObject(pOldBitmap);
    }
<< Back to MFC index



Post Reply