Bitmap (Chapter 15). 2 CBrush with bitmap A Brush defines how to fill a region Different kinds of...

38
Bitmap (Chapter 15)

Transcript of Bitmap (Chapter 15). 2 CBrush with bitmap A Brush defines how to fill a region Different kinds of...

Page 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 BrushesExample.

Bitmap (Chapter 15)

Page 2: 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 BrushesExample.

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);

Page 3: 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 BrushesExample.

Bitmap?

• Represent an image as a set of dots(pixels)

Page 4: 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 BrushesExample.

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"#include "resource.h"

Page 5: 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 BrushesExample.

CBitmap

• CBitmap: a class for storing a bitmap

• Example:

CBitmap bitmap;bitmap.LoadBitmap(IDB_BITMAP1);

CBrush brush(&bitmap);dc.SelectObject(&brush);dc.Rectangle(0,0,200,200);

CBitmap bitmap;bitmap.LoadBitmap(IDB_BITMAP1);

CBrush brush(&bitmap);dc.SelectObject(&brush);dc.Rectangle(0,0,200,200);

Resource ID

Page 6: 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 BrushesExample.

6

CBitmap::GetBitmap( )

• How to get the information about the bitmap:

int CBitmap::GetBitmap (BITMAP* pBitMap) ;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;};

Page 7: 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 BrushesExample.

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);

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);

Page 8: 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 BrushesExample.

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

Page 9: 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 BrushesExample.

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)

Page 10: 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 BrushesExample.

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);

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);

Page 11: 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 BrushesExample.

11

Bit Block Transfer

• BitBlt

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

ynWidth

nHeight

xSrc

ySrcnWidth

nHeight

Parameter for copy:SRCCOPY

Page 12: 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 BrushesExample.

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) ;

(original device context)

pSrcDC(new memory device context)

x

ynWidth

nHeight

xSrc

ySrcnSrcWidth

nSrcHeight

Page 13: 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 BrushesExample.

More Advanced Drawing:Double Buffering

Page 14: 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 BrushesExample.

The Problems:

• Image displays while drawing– Especially when using a slow computer

• It is blinking when redrawing– When invalidating, it first erases the scene.

Page 15: 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 BrushesExample.

Double Buffering

• Using two Device Context– 1. one for painting (Back Buffer)– 2. the other for displaying (Front Screen Buffer)

Page 16: 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 BrushesExample.

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

Page 17: 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 BrushesExample.

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);

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);

Page 18: 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 BrushesExample.

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

Page 19: 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 BrushesExample.

WM_ERASEBKGND

• WM_ERASEBKGND: when erasing the screen

Page 20: 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 BrushesExample.

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;

}

BOOL CMainWindow::OnEraseBkgnd(CDC* pDC){

// TODO: Add your message handler code here and/or call default

// return CWnd::OnEraseBkgnd(pDC);return true;

}

Page 21: 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 BrushesExample.

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)

Page 22: 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 BrushesExample.

Chapter 3 The mouse and the

Keyboard

Page 23: 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 BrushesExample.

Getting input from the Mouse

Page 24: 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 BrushesExample.

24

Client-Area Mouse Messages

• Client-Area Mouse Messages

Message

WM_LBUTTONDOWNWM_LBUTTONDOWN

WM_LBUTTONUPWM_LBUTTONUP

WM_LBUTTONDBLCLK

WM_MBUTTONDOWN

WM_MBUTTONUP

WM_MBUTTONDBLCLK

WM_RBUTTONDOWN

WM_RBUTTONUP

WM_RBUTTONDBLCLK

WM_MOUSEMOVEWM_MOUSEMOVE

Sent When

The left mouse button is pressedThe left mouse button is pressed

The left mouse button is releasedThe left mouse button is released

The left mouse button is double-clickedThe middle mouse button is pressed

The middle mouse button is released

The middle mouse button is double-clickedThe right mouse button is pressed

The right mouse button is released

The right mouse button is double-clickedThe cursor is moved over the client areaThe cursor is moved over the client area

Page 25: 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 BrushesExample.

Client-Area Mouse Messages

• Messages for Mouse Events

WM_LBUTTONDOWNWM_LBUTTONDOWNWM_LBUTTONUPWM_LBUTTONUP

WM_MOUSEMOVEWM_MOUSEMOVE

When movingWhen clicking

Left buttonClicking and

DraggingDouble clicking

WM_MOUSEMOVEWM_MOUSEMOVE

WM_MOUSEMOVEWM_MOUSEMOVE

WM_MOUSEMOVEWM_MOUSEMOVE

WM_MOUSEMOVEWM_MOUSEMOVE

WM_LBUTTONDOWNWM_LBUTTONDOWNWM_MOUSEMOVEWM_MOUSEMOVE

WM_LBUTTONUPWM_LBUTTONUP

WM_MOUSEMOVEWM_MOUSEMOVE

WM_MOUSEMOVEWM_MOUSEMOVE

WM_LBUTTONDOWNWM_LBUTTONDOWNWM_LBUTTONUPWM_LBUTTONUP

WM_LBUTTONDBLCLKWM_LBUTTONDBLCLKWM_LBUTTONUPWM_LBUTTONUP

Page 26: 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 BrushesExample.

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 functionOnLButtonDown

OnLButtonUp

OnLButtonDblClkOnMButtonDown

OnMButtonUp

OnMButtonDblClkOnRButtonDown

OnRButtonUp

OnRButtonDblClkOnMouseMove

Page 27: 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 BrushesExample.

How to add the event handler

• Using “Properties” window of CMainWindow

Page 28: 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 BrushesExample.

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

Page 29: 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 BrushesExample.

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);}

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);}

Page 30: 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 BrushesExample.

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);}

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);}

Page 31: 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 BrushesExample.

Coding practice: Line drawing 1

• Draw lines by using mouse

• Key Idea:– Remembering the beginning and ending position

1.When the mouse left button is down• Set the position as the beginning point

2.When the mouse left button is released• Set the position as the ending point• Drawing the line

Page 32: 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 BrushesExample.

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

1.When the mouse left button is down• Set the position as the beginning point

2.When dragging the mouse• Set the position as the ending point• Drawing the line

3.When the mouse left button is released• Set the position as the ending point• Drawing the line

Page 33: 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 BrushesExample.

Any problem?

• What happen when moving the mouse outside the window’s client area while dragging?

Page 34: 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 BrushesExample.

34

Capturing the mouse

• Mouse Capture:– 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

Page 35: 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 BrushesExample.

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.

Page 36: 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 BrushesExample.

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

Page 37: 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 BrushesExample.

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) ;

Page 38: 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 BrushesExample.

38

Nonclient-Area Mouse Handlers

• nHitTestValue 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