Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bitmap (Chapter 15).

Similar presentations


Presentation on theme: "Bitmap (Chapter 15)."— Presentation transcript:

1 Bitmap (Chapter 15)

2 CBrush with bitmap A Brush defines how to fill a region
Different kinds of the brush (by using different constructor) Various Brushes Example Solid brush: filling with a color CBrush brush(RGB(255, 0, 0)); Hatch brush: filling with a pattern CBrush brush(HS_DIAGCROSS, RGB(255, 0, 0)); Bitmap brush: filling with an image CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CBrush brush(&bitmap);

3 Bitmap? Represent an image as a set of dots(pixels)

4 How to use Bitmap in Visual studio
Adding a bitmap in the resource view Include the header file “resource.h” before using #include "resource.h"

5 CBitmap CBitmap: a class for storing a bitmap Example: CBitmap bitmap;
Resource ID CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CBrush brush(&bitmap); dc.SelectObject(&brush); dc.Rectangle(0,0,200,200);

6 CBitmap::GetBitmap( )
How to get the information about the bitmap: int CBitmap::GetBitmap (BITMAP* pBitMap) ; struct BITMAP { int bmType; int bmWidth; // width of the bitmap (pixel) int bmHeight; // height of the bitmap (pixel) int bmWidthBytes; BYTE bmPlanes; BYTE bmBitsPixel; LPVOID bmBits; };

7 CBitmap::GetBitmap( )
Example: CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); BITMAP bmpinfo; bitmap.GetBitmap(&bmpinfo); CString str; str.Format(_T(“width = %d, height = %d\n”), bmpinfo.bmWidth, bmpinfo.bmHeight); dc.TextOut(100,100,str);

8 Display the bitmap directly
Copy the image directly into the screen  Block Transfer Process: Original Device Context Preparing one more device context Draw image at the new DC Copy the new DC into the original DC

9 Display the bitmap directly
Copy the image directly into the screen  Block Transfer Process: Original Device Context CPaintDC dc; Preparing one more device context CDC memDc; CDC::CreateCompatibleDC(..); Draw image at the new DC CDC::SelectObject(…); Copy the new DC into the original DC dc.BitBlt(…); Bit Block Transfer (BitBlt)

10 Example CPaintDC dc(this); CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1); BITMAP bmpInfo; bitmap.GetBitmap(&bmpInfo); CDC memDc; memDc.CreateCompatibleDC(&dc); memDc.SelectObject(&bitmap); dc.BitBlt(100, 100, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, 0, 0, SRCCOPY);

11 (original device context) (new memory device context)
Bit Block Transfer BitBlt Parameter for copy: SRCCOPY BOOL BitBlt (int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop) ; (original device context) pSrcDC (new memory device context) x y nWidth nHeight xSrc ySrc nWidth nHeight

12 Bit Block Transfer with stretching
StretchBlt: BOOL StretchBlt (int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop) ; pSrcDC (new memory device context) ySrc nSrcWidth xSrc nSrcHeight (original device context) y nWidth x nHeight

13 More Advanced Drawing: Double Buffering

14 The Problems: Image displays while drawing
Especially when using a slow computer It is blinking when redrawing When invalidating, it first erases the scene.

15 Double Buffering Using two Device Context
1. one for painting (Back Buffer) 2. the other for displaying (Front Screen Buffer)

16 Draw on Memory and Copy at once
Process: 1. making a memory dc (CreateCompatibleDC) 2. preparing a bitmap for storing the image (CreateCompatibleBitmap) 3. Selecting bitmap (SelectObject) 4. Drawing 5. Copy the image into the original DC (BitBlt)  Draw on memory and Copy it at once: Double Buffering

17 Draw on Memory and Copy at once
CPaintDC dc(this); CRect rect; GetClientRect(&rect); CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap bitmap; bitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height()); memDC.SelectObject(&bitmap); memDC.Rectangle(0,0,100,100); dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);

18 Double Buffering using bitmap
Drawing is fast. Still blinking  Because it always erases when redrawing  Do nothing at the message handler “Erase background” Solution: Change the WM_ERASEBKGND message handler

19 WM_ERASEBKGND WM_ERASEBKGND: when erasing the screen

20 WM_ERASEBKGND handler
WM_ERASEBKGND: when erasing the screen Do nothing here but return true BOOL CMainWindow::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default // return CWnd::OnEraseBkgnd(pDC); return true; }

21 Announcement Announcement about Homework #2 will be available at 9:00 PM TODAY Check the our homepage Due Date will be 2012/04/12 (Thursday)

22 Chapter 3 The mouse and the Keyboard

23 Getting input from the Mouse

24 Client-Area Mouse Messages
Sent When WM_LBUTTONDOWN The left mouse button is pressed WM_LBUTTONUP The left mouse button is released WM_LBUTTONDBLCLK The left mouse button is double-clicked WM_MBUTTONDOWN The middle mouse button is pressed WM_MBUTTONUP The middle mouse button is released WM_MBUTTONDBLCLK The middle mouse button is double-clicked WM_RBUTTONDOWN The right mouse button is pressed WM_RBUTTONUP The right mouse button is released WM_RBUTTONDBLCLK The right mouse button is double-clicked WM_MOUSEMOVE The cursor is moved over the client area

25 Client-Area Mouse Messages
Messages for Mouse Events When clicking Left button Clicking and Dragging When moving Double clicking WM_MOUSEMOVE WM_LBUTTONDOWN WM_LBUTTONDOWN WM_LBUTTONDOWN WM_MOUSEMOVE WM_LBUTTONUP WM_MOUSEMOVE WM_LBUTTONUP WM_MOUSEMOVE WM_MOUSEMOVE WM_LBUTTONDBLCLK WM_MOUSEMOVE WM_MOUSEMOVE WM_LBUTTONUP WM_MOUSEMOVE WM_LBUTTONUP

26 Client-Area Mouse Messages
Message-map macros and Message handlers Message WM_LBUTTONDOWN WM_LBUTTONUP WM_LBUTTONDBLCLK WM_MBUTTONDOWN WM_MBUTTONUP WM_MBUTTONDBLCLK WM_RBUTTONDOWN WM_RBUTTONUP WM_RBUTTONDBLCLK WM_MOUSEMOVE Message-Map Macro ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_LBUTTONDBLCLK() ON_WM_MBUTTONDOWN() ON_WM_MBUTTONUP() ON_WM_MBUTTONDBLCLK() ON_WM_RBUTTONDOWN() ON_WM_RBUTTONUP() ON_WM_RBUTTONDBLCLK() ON_WM_MOUSEMOVE() Handling function OnLButtonDown OnLButtonUp OnLButtonDblClk OnMButtonDown OnMButtonUp OnMButtonDblClk OnRButtonDown OnRButtonUp OnRButtonDblClk OnMouseMove

27 How to add the event handler
Using “Properties” window of CMainWindow

28 Mouse Message Handler Prototype of the Handler function: nFlags
Status of the mouse buttons and the Shift and Ct기 key at the time when the message was generated afx_msg void On##### (UINT nFlags, CPoint point) ; Bit Mask Meaning MK_CONTROL Ctrl key is pressed MK_SHIFT Shift key is pressed MK_LBUTTON Mouse left button is pressed MK_MBUTTON Mouse middle button is pressed MK_RBUTTON Mouse right button is pressed

29 Mouse Message Handler Prototype of the Handler function: nFlags
afx_msg void On##### (UINT nFlags, CPoint point) ; void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { if(nFlags & MK_SHIFT){ // if shift key is pressed } if(nFlags & MK_RBUTTON){ // if right button is pressed at the same time CWnd ::OnLButtonDown(nFlags, point);

30 Mouse Message Handler Prototype of the Handler function: point
Location of the cursor afx_msg void On##### (UINT nFlags, CPoint point) ; void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { CClientDC dc(this); CPoint pt = point; dc.Rectangle(pt.x-100, pt.y+100, pt.x+100, pt.y-100); CWnd ::OnLButtonDown(nFlags, point); }

31 Coding practice: Line drawing 1
Draw lines by using mouse Key Idea: Remembering the beginning and ending position When the mouse left button is down Set the position as the beginning point When the mouse left button is released Set the position as the ending point Drawing the line

32 Coding practice: Line drawing 2
Draw Lines by using Mouse Show the lines even when dragging the mouse (rubber band effect) Key Idea: Remembering the beginning and ending position When the mouse left button is down Set the position as the beginning point When dragging the mouse Set the position as the ending point Drawing the line When the mouse left button is released

33 Any problem? What happen when moving the mouse outside the window’s client area while dragging?

34 Capturing the mouse Mouse Capture: Related function:
Receiving mouse messages no matter where the mouse goes on the screen while dragging Related function: Function Meaning SetCapture() Starting Mouse Capturing ReleaseCapture() Ending mouse Capturing GetCapture() Returns CWnd pointer that owns the capture

35 Nonclient-Area Mouse Messages
When the mouse is clicked or moved in a window’s nonclient area: Message Sent When WM_NCLBUTTONDOWN The left mouse button is pressed. WM_NCLBUTTONUP The left mouse button is released. WM_NCLBUTTONDBLCLK The left mouse button is double-clicked. WM_NCMBUTTONDOWN The middle mouse button is pressed. WM_NCMBUTTONUP The middle mouse button is released. WM_NCMBUTTONDBLCLK The middle mouse button is double-clicked. WM_NCRBUTTONDOWN The right mouse button is pressed. WM_NCRBUTTONUP The right mouse button is released. WM_NCRBUTTONDBLCLK The right mouse button is double-clicked. WM_NCMOUSEMOVE The cursor is moved over the window's nonclient area.

36 Nonclient-Area Mouse handlers
Massage-map Macros and Handlers Message Message-Map Macro Handling Function WM_NCLBUTTONDOWN ON_WM_NCLBUTTONDOWN OnNcLButtonDown WM_NCLBUTTONUP ON_WM_NCLBUTTONUP OnNcLButtonUp WM_NCLBUTTONDBLCLK ON_WM_NCLBUTTONDBLCLK OnNcLButtonDblClk WM_NCMBUTTONDOWN ON_WM_NCMBUTTONDOWN OnNcMButtonDown WM_NCMBUTTONUP ON_WM_NCMBUTTONUP OnNcMButtonUp WM_NCMBUTTONDBLCLK ON_WM_NCMBUTTONDBLCLK OnNcMButtonDblClk WM_NCRBUTTONDOWN ON_WM_NCRBUTTONDOWN OnNcRButtonDown WM_NCRBUTTONUP ON_WM_NCRBUTTONUP OnNcRButtonUp WM_NCRBUTTONDBLCLK ON_WM_NCRBUTTONDBLCLK OnNcRButtonDblClk WM_NCMOUSEMOVE ON_WM_NCMOUSEMOVE OnNcMouseMove

37 Nonclient-Area Mouse handlers
Prototype of the handling function nHitTest A hit test code indicates where the event occurred. point Location at which the event occurred (in screen coordinate) Use CWnd::ScreenToClient() function to convert screen coord to client coord. afx_msg void OnNc* (UINT nHitTest, CPoint point) ;

38 Nonclient-Area Mouse Handlers
nHitTest Value Corresponding Location HTCAPTION The title bar HTCLOSE The close button HTGROWBOX The restore button (same as HTSIZE) HTHSCROLL The window's horizontal scroll bar HTMENU The menu bar HTREDUCE The minimize button HTSIZE The restore button (same as HTGROWBOX) HTSYSMENU The system menu box HTVSCROLL The window's vertical scroll bar HTZOOM The maximize button


Download ppt "Bitmap (Chapter 15)."

Similar presentations


Ads by Google