第六章 绘图与打印

Click here to load reader

Upload: lara

Post on 11-Feb-2016

106 views

Category:

Documents


0 download

DESCRIPTION

第六章 绘图与打印. 在 Windows 应用程序中,无论被 显示输出的数据 的形式是 图 形 、 图象 还是 文本 ,也不管输出设备是 显示器 、 打印机 、 内存 还是其他输出设备,都是通过 图形设备接口 ( GDI ) 以 图形 的 形式来处理的。也就是 Windows 程序通过调用 GDI 函数,请求 进行各类 绘制 操作; GDI 通过相应的 设备驱动 程序,完成各类 绘图 操作在特定 硬件 设备(如 显示器 、 打印机 )上的 显示输出 。 GDI 提供的 绘制 操作有三种类型:. - PowerPoint PPT Presentation

TRANSCRIPT

  • Windows GDI Windows GDI GDI GDI

  • GDI

  • MFC MFC CDC CGdiObject CPen CBrush

  • 6.1 MFC MFC 6.1.1 Windows Device Context MFC Device Context C++ CDC Windows

  • MFC CDC CDC CWnd CDC MFC CObject

  • 1 CDC The CDC class defines a class of device-context objects. The CDC object provides member functions for working with a device context, such as a display or printer, as well as members for working with a display context associated with the client area of a window. Do all drawing through the member functions of a CDC object. The class provides member functions for device-context operations, working with drawing tools, type-safe graphics device interface (GDI) object selection, and working with colors and palettes. It also provides member functions for getting and setting drawing attributes, mapping, working with the viewport, working with the window extent, converting coordinates, working with regions, clipping, drawing lines, and drawing simple shapes, ellipses, and polygons. Member functions are also provided for drawing text, working with fonts, using printer escapes, scrolling, and playing metafiles.

  • To use a CDC object, construct it, and then call its member functions that parallel Windows functions that use device contexts. For specific uses, the Microsoft Foundation Class Library provides several classes derived from CDC . CPaintDC encapsulates calls to BeginPaint and EndPaint. CClientDC manages a display context associated with a windows client area. CWindowDC manages a display context associated with an entire window, including its frame and controls. CMetaFileDC associates a device context with a metafile. CDC contains two device contexts, m_hDC and m_hAttribDC, which, on creation of a CDC object, refer to the same device. CDC directs all output GDI calls to m_hDC and most attribute GDI calls to m_hAttribDC. (An example of an attribute call is GetTextColor, while SetTextColor is an output call.)

  • For example, the framework uses these two device contexts to implement a CMetaFileDC object that will send output to a metafile while reading attributes from a physical device. Print preview is implemented in the framework in a similar fashion. You can also use the two device contexts in a similar way in your application-specific code.

  • 2 CClietDC The CClientDC class is derived from CDC and takes care of calling the Windows functions GetDC at construction time and ReleaseDC at destruction time. This means that the device context associated with a CClientDC object is the client area of a window.

    3 CPaintDC The CPaintDC class is a device-context class derived from CDC. It performs a CWnd::BeginPaint at construction time and CWnd::EndPaint at destruction time. A CPaintDC object can only be used when responding to a WM_PAINT message, usually in your OnPaint message-handler member function.

  • 4 CWindowDC The CWindowDC class is derived from CDC. It calls the Windows functions GetWindowDC at construction time and ReleaseDC at destruction time. This means that a CWindowDC object accesses the entire screen area of a CWnd (both client and nonclient areas).

    5 CMetaFileDC A Windows metafile contains a sequence of graphics device interface (GDI) commands that you can replay to create a desired image or text. To implement a Windows metafile, first create a CMetaFileDC object. Invoke the CMetaFileDC constructor, then call the Create member function, which creates a Windows metafile device context and attaches it to the CMetaFileDC object.

  • Next send the CMetaFileDC object the sequence of CDC GDI commands that you intend for it to replay. Only those GDI commands that create output, such as MoveTo and LineTo, can be used. After you have sent the desired commands to the metafile, call the Close member function, which closes the metafile device contexts and returns a metafile handle. Then dispose of the CMetaFileDC object. CDC::PlayMetaFile can then use the metafile handle to play the metafile repeatedly. The metafile can also be manipulated by Windows functions such as CopyMetaFile, which copies a metafile to disk. When the metafile is no longer needed, delete it from memory with the DeleteMetaFile Windows function. You can also implement the CMetaFileDC object so that it can handle both output calls and attribute GDI calls such as GetTextExtent. Such a metafile is more flexible and can more easily reuse general GDI code, which often consists of a mix of output and attribute calls.

  • The CMetaFileDC class inherits two device contexts, m_hDC and m_hAttribDC, from CDC. The m_hDC device context handles all CDC GDI output calls and the m_hAttribDC device context handles all CDC GDI attribute calls. Normally, these two device contexts refer to the same device. In the case of CMetaFileDC, the attribute DC is set to NULL by default. Create a second device context that points to the screen, a printer, or device other than a metafile, then call the SetAttribDC member function to associate the new device context with m_hAttribDC. GDI calls for information will now be directed to the new m_hAttribDC. Output GDI calls will go to m_hDC, which represents the metafile.

  • 6.1.2 MFC CGdiObject MFC CObject

  • CGdiObject 1 CPen The CPen class encapsulates a Windows graphics device interface (GDI) pen.

    2 CBrush The CBrush class encapsulates a Windows graphics device interface (GDI) brush. To use a CBrush object, construct a CBrush object and pass it to any CDC member function that requires a brush. Brushes can be solid, hatched, or patterned.

  • 3 CFont The CFont class encapsulates a Windows graphics device interface (GDI) font and provides member functions for manipulating the font. To use a CFont object, construct a CFont object and attach a Windows font to it with CreateFont, CreateFontIndirect, CreatePointFont, or CreatePointFontIndiret, and then use the objects member functions to manipulate the font. The CreatePointFont and CreatePointFontIndirect functions are often easier to use than CreateFont or CreateFontIndirect since they do the conversion for the height of the font from a point size to logical units automatically.

  • 4 CBitmap The CBitmap class encapsulates a Windows graphics device interface (GDI) bitmap and provides member functions to manipulate the bitmap. To use a CBitmap object, construct the object, attach a bitmap handle to it with one of the initialization member functions, and then call the objects member functions.

  • 5 CPalette The CPalette class encapsulates a Windows color palette. A palette provides an interface between an application and a color output device (such as a display device). The interface allows the application to take full advantage of the color capabilities of the output device without severely interfering with the colors displayed by other applications. Windows uses the applications logical palette (a list of needed colors) and the system palette (which defines available colors) to determine the colors used. A CPalette object provides member functions for manipulating the palette referred to by the object. Construct a CPalette object and use its member functions to create the actual palette, a graphics device interface (GDI) object, and to manipulate its entries and other properties.

  • 6. CRgn The CRgn class encapsulates a Windows graphics device interface (GDI) region. A region is an elliptical or polygonal area within a window. To use regions, you use the member functions of class CRgn with the clipping functions defined as members of class CDC.

  • 6.2 DisplaySupports drawing operations on a video display.PrinterSupports drawing operations on a printer or plotter.MemorySupports drawing operations on a bitmap.InformationSupports the retrieval of device data.

  • 6.2.1 CDC *pDC = pWnd->GetDC();pWnd->ReleaseDC( pDC );pWnd pDC

    6.2.2 CDC *pDC = pWnd->GetWindowDC();pWnd->ReleaseDC( pDC );pWnd pDC

  • 6.2.3 CDC *pDC = CDC::FromHandle( ::GetDC( NULL ) );pDC->DeleteDC(); DeleteDC FromHandle CDC DeleteDC Device Context GDI Object

  • 6.2.4 CClientDC dc( this ); this CClientDC API ::GetDC( this->m_hWnd ); dc this->m_hWnd CClientDC HWND m_hWnd HDC m_hDC dc API ::ReleaseDC( m_hWnd, m_hDC ); ::GetDC

  • 6.2.5 WM_PAINTCPaintDC dc( this ); this CPaintDC BeginPaintthis->BeginPain( &m_ps ); dc m_ps CPaintDC PAINTSTRUCT BeginPain this CPaintDC HWND m_hWnd dc EndPaintFromHandle( m_hWnd )->EndPaint ( & m_ps ); BeginPaint

  • 6.2.6 CWindowDC dc( this );this CWindowDC API::GetWindowDC( this->m_hWnd ); dc this->m_hWnd CWindowDC m_hWnd m_hDC dc API ::ReleaseDC( m_hWnd, m_hDC ); ::GetWindowDC

  • 6.3 6.3.1 CPen 1 CPen CPen( int nPenStyle, int nWidth, COLORREF crColor ); throw( CResourceException ); nPenStylePS_SOLIDCreates a solid pen.PS_DASHCreates a dashed pen. Valid only when the pen width is 1 or less, in device units.PS_DOTCreates a dotted pen. Valid only when the pen width is 1 or less, in device units.

  • PS_DASHDOTCreates a pen with alternating dashes and dots. Valid only when the pen width is 1 or less, in device units.PS_DASHDOTDOTCreates a pen with alternating dashes and double dots. Valid only when the pen width is 1 or less, in device units.PS_NULLCreates a null pen. MSDN nWidth crColorRGB CPen Mypen( PS_SOLID, 2, RGB( 255, 0,0 ) ); CPen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL ); throw( CResourceException );

  • 2 CPen CPen( ); BOOL CreatePen( int nPenStyle, int nWidth, COLORREF crColor ); BOOL CreatePenIndirect( LPLOGPEN lpLogPen ); lpLogPen LOGPEN typedef struct tagLOGPEN { /* lgpn */ UINT lopnStyle; POINT lopnWidth; COLORREF lopnColor;} LOGPEN;

  • CPen pen; pen.CreatePen( PS_SOLID, 2, RGB( 255, 0, 0 )); CPen pen; LOGPEN logPen; logPen.lopnStyle = PS_SOLID; logPen.lopnWidth = 2; logPen.lopnColor = RGB( 255, 0, 0 ); pen.CreatePenIndirect( &logPen );

  • 3 Windows HPEN CPen CPen static CPen* PASCAL FromHandle( HPEN hPen ); hPenWindows CPen FromHandle CPen CPen CPen Windows

  • 4 DC CDC SelectStockObject virtual CGdiObject* SelectStockObject( int nIndex ); nIndex GDI GDI BLACK_BRUSHBlack brush.DKGRAY_BRUSHDark gray brush.GRAY_BRUSHGray brush.HOLLOW_BRUSHHollow brush.LTGRAY_BRUSHLight gray brush.NULL_BRUSHNull brush.WHITE_BRUSHWhite brush.BLACK_PENBlack pen.

  • NULL_PENNull pen.WHITE_PENWhite pen.ANSI_FIXED_FONTANSI fixed system font.ANSI_VAR_FONTANSI variable system font.DEVICE_DEFAULT_FONTDevice-dependent font.OEM_FIXED_FONTOEM-dependent fixed font.SYSTEM_FONTThe system font. By default, Windows uses the system font to draw menus, dialog-box controls, and other text. In Windows versions 3.0 and later, the system font is proportional width; earlier versions of Windows use a fixed-width system font.

  • SYSTEM_FIXED_FONTThe fixed-width system font used in Windows prior to version 3.0. This object is available for compatibility with earlier versions of Windows.DEFAULT_PALETTEDefault color palette. This palette consists of the 20 static colors in the system palette. pDC->SelectStockObject(BLACK_PEN);

  • 6.3.2 CBrush 1 CBrush CBrush( COLORREF crColor ); throw( CResourceException ); crColor CBrush br ( RGB( 255, 0, 0 ));

  • CBrush CBrush( int nIndex, COLORREF crColor ); throw( CResourceException ); nIndexHS_BDIAGONAL Downward hatch (left to right) at 45 degreesHS_CROSS Horizontal and vertical crosshatchHS_DIAGCROSS Crosshatch at 45 degreesHS_FDIAGONAL Upward hatch (left to right) at 45 degreesHS_HORIZONTAL Horizontal hatchHS_VERTICAL Vertical hatch

  • CBrush br( HS_DIAGCROSS, RGB( 255, 0, 0 )); CBrush CBrush( CBitmap* pBitmap ); throw( CResourceException ); pBitmap 8X8 MSDN CBitmap bmp; bmp.LoadBitmap( IDB_MYBITMAP ); CBrush br(&bmp);

  • 2 CBrush BOOL CreateSolidBrush( COLORREF crColor ); CBrush br; br.CreateSolidBrush( RGB( 255, 0, 0 ));

  • CBrush BOOL CreateHatchBrush( int nIndex, COLORREF crColor ); nIndex CBrush br; br.CreateHatchBrush( HS_BDIAGONAL, RGB( 255, 0, 0 ));

  • CBrush BOOL CreatePatternBrush( CBitmap* pBitmap ); pBitmap 8X8 MSDN CBitmap bmp;// bmp.LoadBitmap( IDB_MYBITMAP );// CBrush br;// br.CreatePaletteBrush( &bmp );//

  • 3 Windows CBrush CBrush static CBrush* PASCAL FromHandle( HBRUSH hBrush ); hBrushWindows CBrush FromHandle CBrush CBrush CBrush Windows 4 DC pDC->SelectStockObject( WHITE_BRUSH );

  • 6.3.3 CFont 1 CFont CreateFont

  • BOOL CreateFont ( int nHeight /* */, int nWidth /* */, int nEscapement,// 0.1int nOrientation, // 0.1 int nWeight, // BYTE bItalic, // BYTE bUnderline, // BYTE cStrikeOut,// BYTE nCharSet, // BYTE nOutPrecision,// BYTE nClipPrecision, // BYTE nQuality, // BYTE nPitchAndFamily, // LPCTSTR lpszFacename // );

  • CFont Myfont; Myfont.CreateFont( 16 /* */, 0 /* */, 0 /* */, 0 /* */, FW_SEMIBOLD /* */, 0 /* */, 0 /* */, 0 /* */, ANSI_CHARSET, // OUT_DEFAULT_PRECIS, // CLIP_DEFAULT_PRECIS, // DEFAULT_QUANLITY, // DEFAULT_PITCH|FF_DONTCARE, // Courier New // );

  • CFont CreateFontIndirect BOOL CreateFontIndirect( const LOGFONT* lpLogFont ); lpLogFontLOGFONT typedef struct tagLOGFONT { LONG lfHeight; // LONG lfWidth; // LONG lfEscapement; // LONG lfOrientation; // LONG lfWeight; // BYTE lfItalic; // BYTE lfUnderline; // BYTE lfStrikeOut; //

  • BYTE lfCharSet; // BYTE lfOutPrecision; // BYTE lfClipPrecision; // BYTE lfQuality; // BYTE lfPitchAndFamily; // TCHAR lfFaceName[LF_FACESIZE]; // } LOGFONT; CreateFont LOGFONT logFont; logFont.lfHeight = 16; logFont.lfWidth = 0; logFont.lfEscapement = 0;

  • logFont.lfOrientation = 0; logFont.lfWeight = FW_SEMIBOLD; logFont.lfItalic = 0; logFont.lfUnderline = 0; logFont.lfStrikeOut = 0; logFont.lfCharSet = ANSI_CHARSET; logFont.lfOutPrecision = OUT_DEFAULT_PRECIS; logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS; logFont.lfQuality = DEFAULT_QUALITY; logFont.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE; strcopy( logFont.lfFaceName, Courier New ); CFont font; font.CreateFontIndirect( &logFont );

  • CFont CreateFont BOOL CreatePointFont( int nPointSize, LPCTSTR lpszFaceName, CDC* pDC = NULL ); BOOL CreatePointFontIndirect( const LOGFONT* lpLogFont, CDC* pDC = NULL );2 Windows CFont CFont static CFont* PASCAL FromHandle( HFONT hFont ); hFontWindows

  • CFont FromHandle CFont CFont CFont Windows

    3 DC pDC->SelectStockObject( SYSTEM_FONT );

  • 6.3.4 CBitmap 1 CBitmap CBitmap CBitmap CBitmap Windows Bitmap CBitmap BOOL CreateBitmap( int nWidth, int nHeight, UINT nPlanes, UINT nBitcount, const void* lpBits ); BOOL CreateBitmapIndirect( LPBITMAP lpBitmap ); BOOL CreateCompatibleBitmap( CDC* pDC, int nWidth, int nHeight ); BOOL CreateDiscardableBitmap( CDC* pDC, int nWidth, int nHeight ); MSDN

  • 2 Windows CBitmap CBitmap static CBitmap* PASCAL FromHandle( HBITMAP hBitmap ); hBitmapWindows CBitmap FromHandle CBitmap CBitmap CBitmap Windows

  • 6.3.5 CPalette 1 CPalette CPalette CPalette CPalette Windows PaletteCPalette BOOL CreateHalftonePalette( CDC* pDC ); BOOL CreatePalette( LPLOGPALETTE lpLogPalette ); MSDN

  • 2 Windows CPalette CPalette static CPalette* PASCAL FromHandle( HPLATTE hPalette ); hPaletteWindows CPalette FromHandle CPalette CPalette CPalette Windows 3 DC pDC->SelectStockObject( DEFAULT_PALETTE );

  • 6.3.6 CRgn 1 CRgn CRgn CRgn CRgn Windows Region CRgn BOOL CreateEllipticRgn( int x1, int y1, int x2, int y2 ); BOOL CreateEllipticRgnIndirect( LPCRECT lpRect ); BOOL CreateFromData( const XFORM* lpXForm, int nCount, const RGNDATA* pRgnData ); BOOL CreateFromPath( CDC* pDC );

  • BOOL CreatePolygonRgn( LPPOINT lpPoints, int nCount, int nMode ); BOOL CreatePolyPolygonRgn( LPPOINT lpPoints, LPINT lpPolyCounts, int nCount, int nPolyFillMode ); BOOL CreateRectRgn( int x1, int y1, int x2, int y2 ); BOOL CreateRectRgnIndirect( LPCRECT lpRect ); BOOL CreateRoundRectRgn( int x1, int y1, int x2, int y2, int x3, int y3 ); MSDN

  • 2 Windows CRgn CRgn static CRgn* PASCAL FromHandle( HRGN hRgn ); hRgnWindows CRgn FromHandle CRgn CRgn CRgn Windows

  • 6.4

  • 6.4.1 1 Windows

  • 8

    * Twip 1/72 GDI1/721 Twip = 1/20 1/1440

    XYMM_TEXTMM_LOMETRIC0.1 mmMM_HIMETRIC0.01 mmMM_LOENGLISH0.01 inchMM_HIENGLISH0.001 inchMM_TWIP1/1440 inchMM_ISOTROPIC ( x == y )MM_ANISOTROPIC ( x y )

  • Windows GDI GetWindowDC CreateDC 0,0 GDI Windows

  • xViewExt xViewport = (xWindow xWinOrg) + xViewOrg xWinExt yViewExt yViewport = (yWindow yWinOrg) + yViewOrg yWinExt

    xWindow, yWindow xViewPort, yViewPort xWinOrg, yWinOrg xViewOrg, yViewOrg 0,0 xWinOrg, yWinOrg xViewOrg, yViewOrg

  • xWinExt, yWinExt xViewExt, yViewExt xy Windows xWinExt xWindow = (xViewport xViewOrg) + xWinOrg xViewExt yWinExt yWindow = (yViewport yViewOrg) + yWinOrg yViewExt

  • 2 CDC::SetMapMode API virtual int SetMapMode( int nMapMode ); // MFC CDC int SetMapMode( HDC hdc, int fnMapMode ); // API nMapMode fnMapMode 8 CClientDC dc; int PreMapMode = dc.SetMapMode( MM_LOMETRIC );

  • CDC::GetMapMode API int GetMapMode( ) const;// MFC CDC int GetMapMode( HDC hdc ); // API CClientDC dc; int PreMapMode = dc.GetMapMode();

  • MM_TEXT MM_TEXT 0,0 0,0 1,1 1,1 1 xViewport = xWindow xWinOrg + xViewOrg yViewport = yWindow yWinOrg + yViewOrg TEXT

  • Windows SetViewportOrg SetWindowOrg ( 0,0 ) SetViewportOrg SetWindowOrg ( 0,0 ) xViewOrg, yViewOrg xWinOrg, yWinOrg ( 0,0 )

  • cxClient cyClient SetViewportOrg( cxClient/2, cyClient/2 ); SetViewportOrg 0,0cxClient/2, cyClient/2

    X -cxClient/2 +cxClient/2 Y -cyClient/2 +cyClient/2

  • TextOut( -cxClient/2, -cyclient/2, hello, 5 ); SetWindowOrg SetViewportOrg SetWindowOrg( -cxClient/2, -cyClient/2 ); SetWindowOrg ( -cxClient/2, -cyClient/2 ) ( 0,0 )

    SetViewportOrg( cxClient/2, cyClient/2 ); SetWindowOrg( -cxClient/2, -cyClient/2 );

  • ( -cxClient/2, -cyClient/2 ) ( cxClient/2, cyClient/2 ) GetViewportOrg GetWindowOrg CPoint ptViewOrigin = GetViewportOrg(); CPoint ptWindowOrigin = GetWindowOrg(); GetViewportOrg GetWindowOrg

  • MM_TEXT ( 0,0 ) y

  • Windows X Y

    ()()MM_LOENGLISH10.010.254MM_LOMETRIC10.003940.1MM_HIENGLISH10.0010.0254MM_TWIPS11/1440=0.0006940.0176MM_HIMETRIC10.0003940.001

  • MM_TEXT 640X480 0.325mm0.0128640X480 300 300dpi 0.0033( 0.08382mm ) MM_LOENGLISH MM_LOMETRIC MM_HIENGLISHMM_TWIPS MM_HIMETRIC

  • 0,00,0?,??,? xWinExt xViewExt xViewExt / xWinExt yViewExt / yWinExt MM_LOENGLISHWindowsxViewExt / xWinExt = 0.01-yViewExt / yWinExt = 0.01

  • 640X480 0.01 1 0.01 1 yViewExt / yWinExt Y Y (0,0) Y Y 0 MM_LOENGLISH

  • HelloSetMapMode( MM_LOENGLISH );TextOut(100, -100, Hello, 5); ( 0,0 ) cyClient SetViewportOrg( 0cyClient ); MM_LOENGLISH

  • SetViewportOrg( cxClient/2, cyClient/2 ); MM_LOENGLISH

    SetWindowOrg (0,0) SetWindowOrg cxClient, cyClient

  • MM_ISOTROPIC MM_ANISOTROPIC Windows Isotropic X Y Anisotropic X Y MM_ISOTROPIC MM_ANISOTROPIC

  • MM_ISOTROPIC CLOCK MM_TEXT / MM_ISOTROPIC X Y / X Y MM_ANISOTROPIC / X Y

  • MM_ISOTROPIC MM_ISOTROPIC MM_LOMETRIC SetWindowExt SetViewportExt X Y X Y SetWindowExt SetViewportExt/ SetViewportExt SetWindowExt

  • 0,0 0 32767 X Y SetMapMode( MM_ISOTROPIC );// SetWindowExt( 32767, 32767 );// SetViewportExt( cxClient, -cyClient );// SetViewportOrg( 0, cyClient );//

  • GetWindowExt GetViewportExt X Y X

  • Y

  • SetMapMode( MM_ISOTROPIC );SetWindowExt( 32767, 32767 );SetViewportExt( cxClient, -cyClient );SetViewportOrg( 0, cxClient );

  • CLOCK ( 0,0 ) X Y 0 - 1000 SetMapMode( MM_ISOTROPIC );SetWindowExt( 1000, 1000 );SetViewportExt( cxClient / 2, -cyClient / 2 );SetViewportOrg( cxClient / 2, cyClient / 2 );

  • MM_ISOTROPIC ( 0,0 ) Y MM_TEXT 1/16 1/16 SetMapMode( MM_ISOTROPIC );SetWindowExt( 160L * GetDeviceCaps( HORZSIZE ) / 254, 160L * GetDeviceCaps( VERTSIZE ) / 254 );SetViewportExt( GetDeviceCaps( HORZSIZE ), GetDeviceCaps( VERTSIZE ));

  • 1/16 HORZSIZE VERTSIZE GetDeviceCaps 1/16 1/16

  • MM_ANISOTROPIC MM_ANISOTROPIC MM_ANISOTROPIC X Y 0 32767SetMapMode( MM_ANISOTROPIC );SetWindowExt( 32767, 32767 );SetViewportExt( cxClient, -cyClient );SetViewportOrg( 0, cyClient ); ( 32767, 32767 ) X Y

  • MM_ISOTROPIC MM_ANISOTROPIC SetMapMode( MM_ANISOTROPIC );SetWindowExt( 1000, 1000 );SetViewportExt( cxClient / 2, -cyClient / 2 );SetViewportOrg( cxClient / 2, cyClient / 2 ); MM_ISOTROPIC

  • MM_ANISOTROPIC X Y SetMapMode( MM_ANISOTROPIC );SetWindowExt( 1, 1 );SetViewportExt( cxChar, cyChar ); cxChar cyChar TextOut TextOut( 3, 2, Hello, 5 ); 3 2 Hello

  • MM_ANISOTROPIC 0.01 Y MM_TEXT y CSize extSize;SetMapMode( MM_LOENGLISH );SetMapMode( MM_ANISOTROPIC );extSize = GetViewportExt();SetViewportExt( extSize.cx, -extSize.cy ); MM_LOENGLISH MM_ANISOTROPIC GetViewportExt CSize extSize SetViewportExt extSize.cx-extSize.cy

  • 3 CDC GDI GDI CWnd Windows ( 0,0 ) ( WM_LBUTTONDOWNWM_MOUSEMOVE ) CDC

  • API CreateDC() GetDC( NULL ) SetCursorPos GetCursorPos CreateWindowMoveWindowSetWindowPlacement

  • MFC CWnd ClientToScreen ScreenToClient API void ScreenToClient( LPPOINT lpPoint ) const;void ScreenToClient( LPRECT lpRect ) const;void ClientToScreen( LPPOINT lpPoint ) const;void ClientToScreen( LPRECT lpRect ) const; MFC CDC LPToDP DPToLP API

  • void LPtoDP( LPPOINT lpPoints, int nCount = 1 ) const;void LPtoDP( LPRECT lpRect ) const;void LPtoDP( LPSIZE lpSize ) const;void DPtoLP( LPPOINT lpPoints, int nCount = 1 ) const;void DPtoLP( LPRECT lpRect ) const;void DPtoLP( LPSIZE lpSize ) const; MapDemo

  • 4 OnDraw OnDraw OnPrepareDC SetMapMode CScrollView OnInitialUpdate SetScrollSizes OnPrepareDC( &dc )

  • 6.4.2 1 CDC virtual COLORREF SetTextColor( COLORREF crColor )CDC virtual COLORREF SetBkColor( COLORREF crColor ) CDC UINT SetTextAlign( UINT nFlags ) nFlags

  • TA_CENTERAligns the point with the horizontal center of the bounding rectangle. TA_LEFTAligns the point with the left side of the bounding rectangle. This is the default setting. TA_RIGHTAligns the point with the right side of the bounding rectangle. TA_BASELINEAligns the point with the base line of the chosen font. TA_BOTTOMAligns the point with the bottom of the bounding rectangle. TA_TOPAligns the point with the top of the bounding rectangle. This is the default setting.

  • TA_NOUPDATECPDoes not update the current position after each call to a text-output function. This is the default setting. TA_UPDATECPUpdates the current x-position after each call to a text-output function. The new position is at the right side of the bounding rectangle for the text. When this flag is set, the coordinates specified in calls to the TextOut member function are ignored.

  • 2 Windows ANDORXOR NOT CDC SetRop2 int SetROP2( int nDrawMode );nDrawMode

  • R2_BLACK Pixel is always black. R2_WHITE Pixel is always white. R2_NOP Pixel remains unchanged. R2_NOT Pixel is the inverse of the screen color. R2_COPYPEN Pixel is the pen color. R2_NOTCOPYPEN Pixel is the inverse of the pen color. R2_MERGEPENNOTPixel is a combination of the pen color and the inverse of the screen color (final pixel = (NOT screen pixel) OR pen).

  • R2_MASKPENNOT Pixel is a combination of the colors common to both the pen and the inverse of the screen (final pixel = (NOT screen pixel) AND pen). R2_MERGENOTPEN Pixel is a combination of the screen color and the inverse of the pen color (final pixel= (NOT pen) OR screen pixel). R2_MASKNOTPEN Pixel is a combination of the colors common to both the screen and the inverse of the pen (final pixel = (NOT pen) AND screen pixel).

  • R2_MERGEPEN Pixel is a combination of the pen color andthe screen color (final pixel = pen OR screen pixel). R2_NOTMERGEPEN Pixel is the inverse of the R2_MERGEPEN color (final pixel = NOT(pen OR screen pixel)). R2_MASKPEN Pixel is a combination of the colors common to both the pen and the screen (final pixel = pen AND screen pixel). R2_NOTMASKPEN Pixel is the inverse of the R2_MASKPEN color (final pixel = NOT(pen AND screen pixel)).

  • R2_XORPEN Pixel is a combination of the colors that are in the pen or in the screen, but not in both (final pixel = pen XOR screen pixel). R2_NOTXORPEN Pixel is the inverse of the R2_XORPEN color (final pixel = OT(pen XOR screen pixel)). R2_XORPEN CDC::GetROP2

  • CDC::SetBKMode( int nBkMode ) nBkMode

    OPAQUE

    TRANSPARENT

  • 3 CRgn CRgn CDC SelectObject SelectClipRgn CRgn

  • 6.4.3

    OPAQUE0,00,0R2_COPYPENMM_TEXT CDC::SelectObjectCDC::SelectObjectCDC::SetArcDirectionCDC::SetBKColorCDC::SetBKModeCDC::SetBrushOrgCDC::MoveToCDC::SetROP2CDC::SetMapMode

  • 6.5 6.5.1 1 CDC::GetTextMetrics BOOL GetTextMetrics( LPTEXTMETRIC lpMetrics ); TEXTMETRIC 2 CDC::SetTextColor( COLORREF crColor ) CDC::SetBkColor( COLORREF crColor )

  • 3 CDC CDC::TextOut virtual BOOL TextOut( int x, int y, LPCTSTR lpszString, int nCount ); BOOL TextOut( int x, int y, const CString& str ); dc.SetTextColor( RGB( 255,255,255 )); dc.SetBKColor( RGB( 0,0,255 )); dc.TextOut( 10,10,Here it is. );

  • CDC::TabbedTextOut TextOut Tab virtual CSize TabbedTextOut( int x,int y,LPCTSTR lpszString,int nCount, int nTabPositions, LPINT lpnTabStopPositions, int nTabOrigin ); CSize TabbedTextOut( int x, int y, const CString& str, int nTabPositions, LPINT lpnTabStopPositions, int nTabOrigin );

  • CDC::ExtTextOut virtual BOOL ExtTextOut( int x, int y, UINT nOptions, LPCRECT lpRect, LPCTSTR lpszString, UINT nCount, LPINT lpDxWidths ); BOOL ExtTextOut( int x, int y, UINT nOptions, LPCRECT lpRect, const CString& str, LPINT lpDxWidths );

  • CDC::DrawText Tab virtual int DrawText( LPCTSTR lpszString, int nCount, LPRECT lpRect, UINT nFormat ); int DrawText( const CString& str, LPRECT lpRect, UINT nFormat );

  • 6.5.2 CDC 1 COLORREF SetPixel( int x, int y, COLORREF crColor ); COLORREF SetPixel( POINT point, COLORREF crColor );2 CPoint MoveTo( int x, int y ); CPoint MoveTo( POINT point ); BOOL LineTo( int x, int y ); BOOL LineTo( POINT point );

  • 3 BOOL Arc( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ); BOOL Arc( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); BOOL ArcTo( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ); BOOL ArcTo( LPCRECT lpRect, POINT ptStart, POINT ptEnd );4 BOOL Rectangle( int x1, int y1, int x2, int y2 ); BOOL Rectangle( LPCRECT lpRect ); BOOL RoundRect( int x1, int y1, int x2, int y2, int x3, int y3 ); BOOL RoundRect( LPCRECT lpRect, POINT point );

  • BOOL Ellipse( int x1, int y1, int x2, int y2 ); BOOL Ellipse( LPCRECT lpRect ); BOOL Chord( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ); BOOL Chord( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); BOOL Pie( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ); BOOL Pie( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); BOOL Polygon( LPPOINT lpPoints, int nCount ); BOOL PolyPolygon( LPPOINT lpPoints, LPINT lpPolyCounts, int nCount );

  • 5 void FillRect( LPCRECT lpRect, CBrush* pBrush ); void Draw3dRect( LPCRECT lpRect, COLORREF clrTopLeft, COLORREF clrBottomRight ); void Draw3dRect( int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight ); void DrawFocusRect( LPCRECT lpRect ); BOOL ExtFloodFill( int x, int y, COLORREF crColor, UINT nFillType ); BOOL FloodFill( int x, int y, COLORREF crColor ); void FrameRect( LPCRECT lpRect, CBrush* pBrush );

  • void InvertRect( LPCRECT lpRect ); BOOL PolyBezier( const POINT* lpPoints, int nCount ); BOOL PolyBezierTo( const POINT* lpPoints, int nCount );6 BOOL PatBlt( int x, int y, int nWidth, int nHeight, DWORD dwRop ); BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop ); BOOL StretchBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop );

  • 6.6

  • void CMyView::OnDraw( CDC *pDC )// { CBrush br, *pOldBr; CPen pen, *pOldPen; pen.CreatePen( PS_SOLID, 3, RGB( 0, 0, 255 )); // br.CreateHatchBrush( HS_CROSS,RGB( 255, 0, 0 )); // pOldBr = pDC->SelectObject( &br ); //

  • pOldPen = pDC->SelectObject( &pen ); // pDC->Ellipse( 0, 0, 200, 200 );// pDC->SelectObject( pOldPen ); // pDC->SelectObject( pOldBr ); // }

  • void CHelloView::OnDraw( CDC* pDC ){ CFont font, *pOldFont; font.CreatePointFont( 240, "_GB2312", pDC );// 24_GB2312 COLORREF oldBColor = pDC->SetBkColor( RGB( 255,255,0 ));// COLORREF oldTColor = pDC->SetTextColor( RGB( 0,0,255 ));//

  • UINT oldAlign = pDC->SetTextAlign( TA_LEFT|TA_BOTTOM );// pOldFont = pDC->SelectObject( &font );// pDC->TextOut( 50,50, );// pDC->SelectObject( pOldFont );// pDC->SetTextAlign( oldAlign );// pDC->SetTextColor( oldTColor );// pDC->SetBkColor( oldBColor );// }

  • 6.7

    1 Draw MDI CScrollView

  • 2 IDR_DRAWTYPE (&D)(&F)(&L) ID_FREE_DRAW ID_LINE_DRAW

  • 3 IDR_DRAWTYPE ID_FREE_DRAW ID_LINE_DRAW

    4 CLine CLine CObject

  • ClassWizard CObject CCmdTarget CLine CCmdTarget CObject CLine line.h DECLARE_MESSAGE_MAP() line.cpp BEGIN_MESSAGE_MAP(CLine, CObject) //{{AFX_MSG_MAP( CLine ) END_MESSAGE_MAP()

  • public: CPoint m_pt; CPoint m_pt1; line.h CLine( CPoint pt, CPoint pt1 ); line.cpp CLine::CLine( CPoint pt, CPoint pt1 ){ m_pt = pt; m_pt1 = pt1;}

  • line.h CLine& operator=( CLine& line ); line.cpp CLine& CLine::operator=( CLine& line ){ m_pt = line.m_pt; m_pt1 = line.m_pt1; return *this;}

  • line.h void Draw( CDC *pDC ); line.cpp void CLine::Draw ( CDC *pDC ){ pDC->MoveTo( m_pt ); pDC->LineTo( m_pt1);}

  • 5 CDrawDoc CDrawView CDrawDoc CLine CTypedPtrList m_ptList; // Save all of free drawings and lines CTypedPtrList MFC Stdafx.h #endif // _AFX_NO_AFXCMN_SUPPORT#include CTypedPtrList CLine CDrawDoc CLine #include line.h

  • CDrawView protected: int m_typeDraw;// Specify the type of current drawing. enum { FreeDraw, Line}; CPoint m_ptStart;// Save the previous point for the cureent// free drawing or the start point for the // current line. CPoint m_ptEnd;// Save the end point for the cureent free// drawing or the cureent line.

  • ClassWizard CDrawView : DrawView.h afx_msg void OnFreeDraw();afx_msg void OnUpdateFreeDraw( CCmdUI* pCmdUI );afx_msg void OnLineDraw();afx_msg void OnUpdateLineDraw( CCmdUI* pCmdUI ); DrawView.cpp

  • ON_COMMAND( ID_FREE_DRAW, OnFreeDraw )ON_UPDATE_COMMAND_UI( ID_FREE_DRAW, OnUpdateFreeDraw )ON_COMMAND( ID_LINE_DRAW, OnLineDraw )ON_UPDATE_COMMAND_UI( ID_LINE_DRAW, OnUpdateLineDraw )

    void CDrawView::OnFreeDraw() { m_typeDraw = FreeDraw;// }void CDrawView::OnUpdateFreeDraw( CCmdUI* pCmdUI ) { pCmdUI->SetCheck( m_typeDraw == FreeDraw );}

  • void CDrawView::OnLineDraw() { m_typeDraw = Line;// }void CDrawView::OnUpdateLineDraw( CCmdUI* pCmdUI ) { pCmdUI->SetCheck( m_typeDraw == Line );}

  • CDrawView OnInitialUpdate void CDrawView::OnInitialUpdate(){ CScrollView::OnInitialUpdate(); CSize sizeTotal; // TODO: calculate the total size of this view sizeTotal.cx = 800; sizeTotal.cy = 900; SetScrollSizes( MM_LOENGLISH, sizeTotal ); // Set mapping mode and view size}

  • WM_LBUTTONDOWN WM_MOUSEMOVE WM_LBUTTONUP ClassWizard DrawView.h

  • //{{AFX_MSG(CDrawView)afx_msg void OnLButtonDown( UINT nFlags, CPoint point );afx_msg void OnLButtonUp( UINT nFlags, CPoint point );afx_msg void OnMouseMove( UINT nFlags, CPoint point );//}}AFX_MSG DrawView.cpp ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_MOUSEMOVE()

  • void CDrawView::OnLButtonDown( UINT nFlags, CPoint point ) { CClientDC dc( this );// OnPrepareDC( &dc );// dc.DPtoLP( &point );// SetCapture();// // switch( m_typeDraw ) { case FreeDraw:m_ptStart = point; // break;

  • case Line:m_ptStart = m_ptEnd = point; // break; } CScrollView::OnLButtonDown( nFlags, point ); }

  • void CDrawView::OnLButtonUp( UINT nFlags, CPoint point ) { if( GetCapture() != this )// // return; CDrawDoc* pDoc = GetDocument(); ASSERT_VALID( pDoc ); CClientDC dc( this );// OnPrepareDC( &dc ); dc.DPtoLP( &point ); CLine *pl;

  • switch( m_typeDraw ) { case FreeDraw: if( m_ptStart != point ) { pl = new CLine( m_ptStart, point ); // CLine pDoc->m_ptList.AddTail( pl ); // CLine pl->Draw( &dc ); } ReleaseCapture();// GetCapture break;

  • case Line: if( m_ptStart != point ) { pl = new CLine( m_ptStart, point ); // CLine pDoc->m_ptList.AddTail( pl ); // CLine Invalidate(); // } ReleaseCapture(); // GetCapture break; } CScrollView::OnLButtonUp( nFlags, point ); }

  • void CDrawView::OnMouseMove( UINT nFlags, CPoint point ) { if( GetCapture() != this )// // return; CDrawDoc* pDoc = GetDocument(); ASSERT_VALID( pDoc ); CClientDC dc( this );// OnPrepareDC( &dc ); dc.DPtoLP( &point ); CLine *pl;

  • switch( m_typeDraw ) { case FreeDraw: if( m_ptStart != point ) { pl = new CLine( m_ptStart, point ); pDoc->m_ptList.AddTail( pl ); pl->Draw( &dc ); m_ptStart = point; } break;

  • case Line: // Check if the left mouse button is down if(( nFlags & MK_LBUTTON ) == MK_LBUTTON ){ // Have you capture the mouse? if( GetCapture() == this ) { // Create a temporary pen, select it into the // divice context and save the current pen for restoring CPen *pOldPen, tempPen; const COLORREF crBlue = RGB( 0, 0, 255 ); tempPen.CreatePen( PS_DOT, 1, crBlue ); pOldPen = dc.SelectObject( &tempPen ); // Set new drawing mode dc.SetROP2( R2_XORPEN );//

  • // Erase previous line CLine tempLine( m_ptStart, m_ptEnd ); templine.Draw( &dc ); // Update end point of line m_ptEnd = tempLine.m_pt1 = point; // Draw current line tempLine.Draw( &dc ); // Restore the original pen in device context dc.SelectObject( pOldPen ); }}break; } CScrollView::OnMouseMove( nFlags, point ); }

  • 6 Draw CDrawView::OnDraw

  • 7 OnDraw void CDrawView::OnDraw( CDC* pDC ) { CDrawDoc* pDoc = GetDocument(); ASSERT_VALID( pDoc ); POSITION pos = pDoc->m_ptList.GetHeadPosition(); CLine *pl;while(pos != NULL) { pl = (CLine*)pDoc->m_ptList.GetNext( pos );pl->Draw( pDC );} }8 Draw

  • 9 MFC AppWizard OnInitialUpdate

  • MFC MFC CDC

  • CDC::StartDoc CDC::StartPage CDC::OnDraw CDC::EndPage - CDC::EndDoc

  • CDrawView OnPreparePrinting BOOL CDrawView::OnPreparePrinting( CPrintInfo* pInfo ) { pInfo->SetMaxPage(2);// the document is two pages long// the first page is the title page// the second is drawing BOOL bRet = DoPreparePrinting( pInfo ); // default preparation pInfo->m_nNumPreviewPages = 2; // preview 2 pages at a time return bRet; }

  • ClassWizard CDrawView OnPrint OnDraw OnDraw OnPrint CDC::IsPrinting OnDraw DrawView.h OnPrint virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo); DrawView.cpp OnPrint

  • void CDrawView::OnPrint( CDC* pDC, CPrintInfo* pInfo ) { CString strpage; strpage.Format( "%d", pInfo->m_nCurPage ); if( pInfo->m_nCurPage == 1 )// Page no.1 is the title page { PrintTitlePage( pDC, pInfo ); PrintPageFoot( pDC, pInfo, strpage ); return; // nothing else to print on page 1 but the page title }CString strHeader = GetDocument()->GetTitle(); // get title of the document PrintPageHeader( pDC, pInfo, strHeader ); // print page header

  • // draw a box for the the border of print view CRect rect = pInfo->m_rectDraw; rect.bottom += 35; pDC->Rectangle( &rect ); PrintPageFoot( pDC, pInfo, strpage ); // print page foot pDC->SetWindowOrg( pInfo->m_rectDraw.left + 2, -pInfo->m_rectDraw.top + 2 );// setting the origin for printting // Now print the rest of the page OnDraw( pDC ); CScrollView::OnPrint( pDC, pInfo ); }

  • PrintTitlePage DrawView.h PrintTitlePage void PrintTitlePage( CDC* pDC, CPrintInfo* pInfo ); DrawView.cpp PrintTitlePage void CDrawView::PrintTitlePage( CDC *pDC, CPrintInfo *pInfo ) { // Prepare a font size for displaying the file name LOGFONT logFont; memset( &logFont, 0, sizeof( LOGFONT )); logFont.lfHeight = 75; logFont.lfWeight = 700;// FW_BOLD CFont font;

  • CFont* pOldFont = NULL;if( font.CreateFontIndirect( &logFont ))pOldFont = pDC->SelectObject( &font ); // Get the file name, to be displayed on title page CString strPageTitle = GetDocument()->GetTitle(); // Display the file name at 1 inch below top of the page, // centered horizontally pDC->SetTextAlign( TA_CENTER ); pDC->TextOut( pInfo->m_rectDraw.right/2, -100, strPageTitle ); if( pOldFont != NULL )pDC->SelectObject( pOldFont ); }

  • PrintPageHeader DrawView.h PrintPageHeader void PrintPageHeader( CDC* pDC, CPrintInfo* pInfo, CString& strHeader ); DrawView.cpp PrintPageHeader void CDrawView::PrintPageHeader( CDC *pDC, CPrintInfo *pInfo, CString &strHeader ) { // Print a page header consisting of the name of document LOGFONT logFont; memset( &logFont, 0, sizeof( LOGFONT )); logFont.lfHeight = 25; logFont.lfWeight = 500;// FW_MEDIUM

  • CFont font; CFont* pOldFont = NULL;if( font.CreateFontIndirect( &logFont ))pOldFont = pDC->SelectObject( &font ); pDC->SetTextAlign( TA_LEFT ); pDC->TextOut( 0, -25, strHeader ); // Modify the top coordinate of drawing rectangle of printer TEXTMETRIC textMetric; pDC->GetTextMetrics( &textMetric ); pInfo->m_rectDraw.top += -35 - textMetric.tmHeight; if( pOldFont != NULL )pDC->SelectObject( pOldFont );}

  • DrawView.h PrintPageFoot void PrintPageFoot( CDC* pDC, CPrintInfo* pInfo, CString& strFoot ); DrawView.cpp PrintPageFoot void CDrawView::PrintPageFoot( CDC *pDC, CPrintInfo *pInfo, CString &strFoot ) { // Print a page foot consisting of the number of page LOGFONT logFont; memset( &logFont, 0, sizeof( LOGFONT )); logFont.lfHeight = 25; logFont.lfWeight = 500;// FW_MEDIUM

  • CFont font; CFont* pOldFont = NULL;if( font.CreateFontIndirect( &logFont ))pOldFont = pDC->SelectObject( &font ); pDC->TextOut( pInfo->m_rectDraw.Width() / 2, pInfo->m_rectDraw.bottom + 35, strFoot ); if( pOldFont != NULL )pDC->SelectObject( pOldFont ); }

    10 Draw