The study of Programming Windows with MFC--BitMap

本文介绍了使用C++和MFC进行位图创建、绘制及转换的方法,包括DDB和Bitmap的基本操作、位图到屏幕和其他设备的绘制过程、位图资源加载方式,并展示了应用程序结构示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.DDB and CBitMap

 

   CClientDC dcScreen(this);

   CBitmap bitmap;

   bitmap.CreateCompatibleBitMap(&dcScreen,m_nWidth,m_nHeight);

 

   CDC dcMem;

   dcMem.CreateCompatibleDC(&dcScreen);

 

   CBitMap *pOldBitMap=dcMem.SelectObject(&bitmap);

   CBrush brush(RGB(255,255,0);

   dcMem.FillRect(CRect(0,0,100,100),&brush);

   dcMem.SelectObject(pOldBitmap);

 

 

2.Blit Bitmap to Screen and Other Devices

   void CMyBitMap::DrawBitmap(CDC *pDC, int x, int y)

   {

         BITMAP bm;

         GetBitmap(&bm);

         CSize size(bm.bmWidth, bm.bmHeight);

         pDC->DPtoLP(&size);    

         CPoint  Org(0,0);

         pDC->DPtoLP(&Org);

 

         CDC dcMem;

         dcMem->CreateCompatibleDC(pDC);

         CBitmap *pOldBitmap=dcMem.SelectObjec(this);

         dcMem.SetMapMode(pDC->GetMapMode ());

         pDC->BitBlit(x,y,size.x,size.y,pDC,Org.x,Org.y,SRCCOPY);

         dcMem.SelectObject(pOldBitmap);

   }

 

//When you pass CDC::DPtoLP the address of a CPoint object, the call goes straight through to the ::DPtoLP API function and the conversion is performed properly, even if one or more of the coordinates comes back negative. But when you pass CDC::DPtoLP the address of a CSize object, MFC performs the conversion itself and converts any negatives to positives.

 

3.LoadMap

   //.rc

  IDB_MYLOGO BITMAP Logo.bmp

  CBitmap bitmap;
  bitmap.LoadBitmap (IDB_MYLOGO);   //bitmap.LoadMappedBitmap (IDB_BITMAP);

 

 

4.the structure of The BitmapDemo application

   class CMainFrame:public CFrameWnd

   {

    public:

            CMainFrame();

    protected:

            DECLARE_DYNAMIC(CMainWnd);

    public:

            virtual BOOL PreCreateWindow(CREATESTRUCT &cs)

            virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,
        AFX_CMDHANDLERINFO* pHandlerInfo);

            virtual ~CMainWnd();

    protected:

             CStatusBar m_wndStatusBar;

             CChildView m_wndView;

             afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
             afx_msg void OnSetFocus(CWnd *pOldWnd);
             afx_msg BOOL OnQueryNewPalette();
             afx_msg void OnPaletteChanged(CWnd* pFocusWnd);

             DECLARE_MESSAGE_MAP()
   };

 

 

    class CChildView : public CWnd
    {

     public:

             CChildView();


    protected:
              virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

              virtual ~CChildView();

    protected:
             void DoGradientFill (CDC* pDC, LPRECT pRect);
             CPalette m_palette;
             CMaskedBitmap m_bitmap;
             BOOL m_bDrawOpaque;
             afx_msg void OnPaint();
             afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
             afx_msg BOOL OnEraseBkgnd(CDC* pDC);
             afx_msg void OnOptionsDrawOpaque();
             afx_msg void OnOptionsDrawTransparent();
             afx_msg void OnUpdateOptionsDrawOpaque(CCmdUI* pCmdUI);
             afx_msg void OnUpdateOptionsDrawTransparent(CCmdUI* pCmdUI);
             DECLARE_MESSAGE_MAP()
    };

 

   class CMaskedBitmap : public CBitmap 
   {
    public:
            void DrawTransparent (CDC* pDC, int x, int y,COLORREF clrTransparency);
            void Draw (CDC* pDC, int x, int y);
   };

   


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风水月

从心底相信自己是成功的第一步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值