11 Wi n d o w s C A D 53 M F C 54 55 56 57 58 M F C 11.1 53 11-1 11-1 MFC M F C C D C Wi n d o w s Wi n d o w s 4
11 199 1. 1) W M _ PA I N T p W n d C W n d C D C * p D C = p W n d GetDC( ); 2) p W n d ReleaseDC (pdc); AfxGetMainWnd( ) GetDC( ) ::GetDC (NULL) CDC P e n ) 2. 1) C P e n CPen pen( P S _ S O L I D, // solid line also // PS_DASH,PS_DOT, P S _ D A S H D O T, // PS_DASHDOTDOT, PS_INSIDEFRAME and // PS_NULL 2, // width in pixels R G B ( 1 2 8, 1 2 8, 1 2 8 ) ) ; // color 2) Cpen *ppen=pdc->selectobject(&pen) //save old pen 3. 1) C B r u s h CBr u s h ( R G B ( 128, 128, 128 ) / / c l o l o r 2) CBrush *pbrush=pdc->selectobject(&brush) //save old brush C P e n C B r u s h 4 MFC 4. C D C 1) p D C - > M o v e To ( 5, 5 ) ; p D C - > L i n e To ( 2 5, 2 5 ) ;
200 2) p D C - > R e c t a n g l e ( CRe c t ( 5, 5 5, 5 0, 8 5 ) ) ; 3) p D C - > A r c ( C R e c t ( 5, 11 5, 5 0, 1 4 5 ), C P o i n t ( 5, 115), C P o i n t ( 5 0, 115)); // area encompassing the arc // starting point of the arc // end point 4) p D C - > R o u n d R e c t ( C R e c t ( 5, 1 8 5, 5 0, 2 1 5 ), CPoint(15,15)); // distance from corner to draw arc 5) p D C - > E l l i p s e ( C R e c t ( 2 5 0, 5, 3 0 5, 2 5 ) ) ; 6) p D C - > P i e ( C R e c t ( 250, 55, 305, 85 ), // area encompassing chart C P o i n t ( 2 5 0, 5 5 ), C P o i n t ( 3 0 5, 5 5 ) ) ; // starting point // end point 7) p D C - > D r a w E d g e ( C R e c t ( 2 5 0, 11 5, 3 0 5, 1 4 5 ), E D G E _ B U M P, BF_RECT); // also EDGE_ETCHED,EDGE_RAISED,EDGE_SUNKEN // also BF_LEFT, B F _ B O T TO M, B F _ R I G H T, B F _ TO P 8) POINT pt[8]; p t [ 0 ]. x = 4 9 5 ; p t [ 0 ]. y = 5 ; p t [ 1 ]. x = 5 1 0 ; p t [ 1 ]. y = 1 0 ; p t [ 2 ]. x = 5 1 5 ; p t [ 2 ]. y = 1 2 ; p t [ 3 ]. x = 4 9 5 ; p t [ 3 ]. y = 1 5 ; p t [ 4 ]. x = 5 5 0 ; p t [ 4 ]. y = 2 5 ; pdc->polyline(pt, 5); 9) p t [ 0 ]. x = 4 9 5 ; p t [ 0 ]. y = 5 5 ; p t [ 1 ]. x = 5 5 0 ; p t [ 1 ]. y = 5 5 ; p t [ 2 ]. x = 5 3 0 ; p t [ 2 ]. y = 6 5 ; p t [ 3 ]. x = 5 5 0 ; p t [ 3 ]. y = 8 5 ;
11 201 p t [ 4 ]. x = 520 ; p t [ 4 ]. y = 70 ; p t [ 5 ]. x = 495 ; p t [ 5 ]. y = 85 ; p t [ 6 ]. x = 510 ; p t [ 6 ]. y = 65 ; p t [ 7 ]. x = 495 ; p t [ 7 ]. y = 55 ; pdc->polygon(pt, 8); 10 1 5 C D C : L i n e To( ) 4 4 M F C CD C D M F C Windows API 11.2 54 ( 11-2 ) 11-2 C D C C D C :: TextOut( ) C D C :: D r a w Text( ) 11. 2. 4 CDC *pdc=pwnd->getdc(); 1. TextOut( ) // where pwnd is a pointer to an // MFC window class 1)
202 CString str( This is drawn text ) ; p D C - > Te x t O u t ( x, y, s t r, s t r.getlength()); // device location (ex: screen pixels) // text as either a string pointer or // a CString variable // text length x y x y C D C :: S e t TextAlign( ) x y 2) x y TextOut( ) p D C - > S e t Te x t A l i g n ( TA_CENTER); //(also TA _ R I G H T ) y p D C - > S e t Te x t A l i g n ( TA _ B O T TOM); //(also TA _ B A S E L I N E ) 3) ( M F C ) p D C - > S e l e c t S t o c k O b j e c t ( A N S I _ VA R _ F O N T ) ; 4) CFont font; f o n t. C r e a t e F o n t ( -22, // point size 0, 0, 0, FW_NORMAL, 0, // if 1=italic // weight, also FW_BOLD 0, // if 1=underline 0, // if 1=strike through 0, 0, 0, 0, 0, C o u r i e r ); // typeface CFont *pfont = (CFont *)pdc->selectobject(&font); CreatePointFont( ) CreateFont( ) 5) p w n d - > S e t F o n t ( p F o n t ) ; //the font 6) p D C - > S e t Te x t C o l o r ( R G B ( 1 0 0, 1 0 0, 1 0 0 ) ) ; 7) p D C - > S e t B k C o l o r ( R G B ( 2 0 0, 2 0 0, 2 0 0 ) ) 8) p D C - > S e t B k M o d e ( O PA Q U E ) ; 9) p D C - > S e t B k M o d e ( T R A N S PA R E N T ) ;
11 203 C D C : : TextOut( ) C D C : : D r a w Text( ) D r a w Text( ) 2. D r a w Text( ) D r a w Text( ) p D C - > D r a w Te x t ( s t r, rect, D T _ C E N T E R ) ; // a CString value // a bounding rectangle // an alignment style-text will be centered E x t TextOut( ) Ta b b e d TextOut( ) DrawState( ) CD C D 11.3 55 LoadIcon( ) Window API LoadImage( ) Windows API ExtractIcon( ) 1. HICON hicon; h i c o n = A f x G e t A p p ( ) - > L o a d I c o n ( I D R _ M A I N F R A M E ) ; 2.. i c o. i c o W z d. i c hicon = (HICON)LoadImage( NULL, // handle of the instance that contains
204 W z d. i c o, I M A G E _ I C O N, // the image // name or identifier of image // type of image- // can also be IMAGE_CURSOR or IMAGE_ICON 0, 0, // desired width and height L R _ L O A D F R O M F I L E ) ; // load flags 3. D L L. e x e W z d. e x e H I N S TANCE hinst=afxgetinstancehandle(); h i c o n = E x t r a c t I c o n ( h i n s t, D e b u g \ \ w z d. e x e, 1 ) ; D L L a - 1 ExtractIcon( ) h I c o n 4. 0, 0 p D C - > D r a w I c o n ( 0, 0, h i c o n ) ; 5. LoadImage( ) ExtractIcon( ) D e s t r o y I c o n ( h i c o n ) ; LoadIcon( ) CD C D 11.4 56 C B i t m a p Windows API LoadImage( ). b m p 1. Developer Studio
11 205 Developer Studio Insert/Insert Resource I m p o r t I D 2. I D I D B _ W Z D CBitmap bitmap; b i t m a p. L o a d B i t m a p ( I D B _ W Z D ) ; 3.. b m p LoadImage( ) CBitmap bitmap; HBITMAP hbitmap = (HBITMAP)::LoadImage( N U L L, W z d 2. b m p, I M A G E _ B I T M A P, // handle of the instance that contains // the image // name or identifier of image // type of image- // can also be IMAGE_CURSOR or IMAGE_ICON 0, 0, // desired width and height L R _ L O A D F R O M F I L E ) ; // load from file // attach this bitmap object to our bitmap class b i t m a p. A t t a c h ( h b i t m a p ) ; 4. BitBlt( ) CDC dccomp; d c C o m p. C r e a t e C o m p a t i b l e D C ( p D C ) ; d c C o m p. S e l e c t O b j e c t ( & b i t m a p ) ; // get size of bitmap for BitBlt() BITMAP bminfo; b i t m a p. G e t O b j e c t ( s i z e o f ( b m I n f o ), & b m I n f o ) ; // use BitBlt() to draw bitmap p D C - > B i t B l t ( 0, 0, b m I n f o. b m Wi d t h, b m I n f o. b m H e i g h t, &dccomp, 0,0,SRCCOPY); LoadImage( ) L R _ L O A D F R O M - F I L E L R _ L O A D T R A N S PA R E N T LoadImage( ) ( ) LoadImage( ) Windows NT L R _ L O A D F R O M F I L E LoadImage( ) C F i l e 12
206 BitBlt( ) 4 CD C D 11.5 57. b m p Wi n d o w s A P I CreateDIBitmap( ) CreateDIBitmap( ) C B i t m a p 1. C l a s s Wi z a r d C B i t m a p LoadBitmapEx( ) 2. 1) LoadBitmapEx( ) LoadBitmapEx( CDC dcscreen; d c S c r e e n. A t t a c h ( : : G e t D C ( N U L L ) ) ; 2) HRSRC hrsrc = FindResource(AfxGetResourceHandle(), M A K E I N T R E S O U R C E ( n I D ), RT _ B I T M A P ) ; HGLOBAL hglb = LoadResource(AfxGetResourceHandle(), hrsrc); LPBITMAPINFOHEADER lpbitmap = ( L P B I T M A P I N F O H E A D E R ) L o c k R e s o u r c e ( h g l b ) ; l p B i t m a p RT _ B I T M A P CreateDIBitmap( ) 3) CreateDIBitmap( ) // get pointers into bitmap structures // (header, color table and picture bits) LPBITMAPINFO pbitmapinfo = (LPBITMAPINFO)lpBitmap; LPBITMAPINFOHEADER pbitmapinfoheader = ( L P B I T M A P I N F O H E A D E R ) l p B i t m a p ; // determine number of colors in bitmap s palette now because // bitmap pixel is right after it
11 207 int nnumberofcolors=0; if (lpbitmap->biclrused) nnumberofcolors = lpbitmap->biclrused; else if (pbitmapinfoheader->bibitcount <= 8) nnumberofcolors = (1<<pBitmapInfoHeader->biBitCount); // else there IS no color table LPBYTE pbitmappicturedata = (LPBYTE)lpBitmap+lpBitmap->biSize+ ( n N u m b e r O f C o l o r s * s i z e o f ( R G B Q U A D ) ) ; 24 RGB 4) m _ Width = lpbitmap->biwi d t h ; m_height = lpbitmap->biheight; CreateDIBitmap( ) CreateDIBitmap( ) 5) C P a l e t t e // create a logical palette from the color table in this bitmap if (nnumofcolors) L O G PALETTE *plogpal = (LOGPALETTE *) new BYTE[ s i z e o f ( L O G PALETTE) + (nnumberofcolors * s i z e o f ( PA L E T T E E N T RY ) ) ] ; p L o g P a l - > p a l Version = 0x300; plogpal->palnumentries = nnumberofcolors; for (int i = 0; i < nnumberofcolors; i++) // if flag set, replace grey color with window s // background color if (btransparent && pbitmapinfo->bmicolors[i].rgbred==192 && pbitmapinfo->bmicolors[i].rgbgreen==192 && p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b B l u e = = 192 ) p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b R e d = G e t RVa l u e (:: G e t S y s C o l o r ( C O L O R _ B T N FA C E )); p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b G r e e n = G e t G Va l u e (:: G e t S y s C o l o r ( C O L O R _ B T N FA C E )); p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b B l u e = G e t B Va l u e (:: G e t S y s C o l o r ( C O L O R _ B T N FA C E )); plogpal->palpalentry[i].pered = p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b R e d ; plogpal->palpalentry[i].pegreen =
208 p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b G r e e n ; plogpal->palpalentry[i].peblue = p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b B l u e ; plogpal->palpalentry[i].peflags = 0; m_ppalette=new CPalette; m _ p P a l e t t e - > C r e a t e P a l e t t e ( p L o g P a l ) ; delete []plogpal; d c S c r e e n. S e l e c t P a l e t t e ( m _ p P a l e t t e, T R U E ) ; d c S c r e e n. R e a l i z e P a l e t t e ( ) ; 6) 3 ::CreateDIBitmap( ) HBITMAP bitmap = ::CreateDIBitmap(dcScreen.m_hDC, p B i t m a p I n f o H e a d e r, C B M _ I N I T, pbitmappicturedata, pbitmapinfo, DIB_RGB_COLORS); 7) A t t a c h ( b i t m a p ) ; 8) 3. BitBlt( ) StretchBlt( ) // load bitmap with our new function m _ b i t m a p. L o a d B i t m a p E x ( I D B _ W Z D, T R U E ) ; // select our palette into the device context CPalette *poldpal = p D C - > S e l e c t P a l e t t e ( m _ b i t m a p. G e t P a l e t t e ( ), FA L S E ) ; p D C - > R e a l i z e P a l e t t e ( ) ; // get device context to select bitmap into CDC dccomp; d c C o m p. C r e a t e C o m p a t i b l e D C ( p D C ) ; d c C o m p. S e l e c t O b j e c t ( & m _ b i t m a p ) ; // draw bitmap p D C - > B i t B l t ( 0, 0, m _ b i t m a p. m _ Width,m_bitmap.m_Height, &dccomp, 0, 0, S R C C O P Y ) ; // reselect old palette p D C - > S e l e c t P a l e t t e ( p O l d P a l, FA L S E ) ;
11 209 ::CreateDIBitmap( ) 4 CD 12 C F i l e C S h a r e M e m ::CreateDIBitmap( ) C D #ifndef WZDBITMAP_H #define WZDBITMAP_H class CWzdBitmap : public CBitmap p u b l i c : D E C L A R E _ D Y N A M I C ( C W z d B i t m a p ) // Constructors C W z d B i t m a p ( ) ; void LoadBitmapEx(UINT nid, BOOL biconbkgrd); CPalette *GetPalette()return m_ppalette;; // Implementation p u b l i c : virtual ~CWzdBitmap(); // Attributes int m_wi d t h ; int m_height; // Operations p r i v a t e : CPalette *m_ppalette; ; # e n d i f // WzdBitmap.cpp : implementation of the CWzdBitmap class / / #include "stdafx.h" #include "WzdBitmap.h"
210 / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / // CWzdBitmap IMPLEMENT_DYNAMIC(CWzdBitmap, CBitmap) C W z d B i t m a p :: C W z d B i t m a p () m _ p P a l e t t e = N U L L ; C W z d B i t m a p :: ~ C W z d B i t m a p () if (m_ppalette) delete m_ppalette; void CWzdBitmap::LoadBitmapEx(UINT nid, BOOL btr a n s p a r e n t ) // can only load once A S S E RT (! m _ p P a l e t t e ); CDC dcscreen; d c S c r e e n. A t t a c h ( : : G e t D C ( N U L L ) ) ; // find and lock bitmap resource HRSRC hrsrc = FindResource(AfxGetResourceHandle(), M A K E I N T R E S O U R C E ( n I D ), RT _ B I T M A P ); HGLOBAL hglb = LoadResource(AfxGetResourceHandle(), hrsrc); LPBITMAPINFOHEADER lpbitmap = (LPBITMAPINFOHEADER)LockResource(hglb); // get pointers into bitmap structures (header, color table and picture bits) LPBITMAPINFO pbitmapinfo = (LPBITMAPINFO)lpBitmap; LPBITMAPINFOHEADER pbitmapinfoheader = (LPBITMAPINFOHEADER)lpBitmap; int nnumberofcolors=0; if (lpbitmap->biclrused) nnumberofcolors = lpbitmap->biclrused; else if (pbitmapinfoheader->bibitcount <= 8) nnumberofcolors = (1<<pBitmapInfoHeader->biBitCount); LPBYTE pbitmappicturedata = (LPBYTE)lpBitmap+lpBitmap->biSize+ ( n N u m b e r O f C o l o r s * s i z e o f ( R G B Q U A D )); // get width and height m _ Width = lpbitmap->biwi d t h ; m_height = lpbitmap->biheight; // create a logical palette from the color table in this bitmap if (nnumberofcolors)
11 211 L O G PALETTE *plogpal = (LOGPALETTE *) new BYTE[sizeof(LOGPALETTE) + (nnumberofcolors * sizeof(pa L E T T E E N T RY ) ) ] ; p L o g P a l - > p a l Version = 0x300; plogpal->palnumentries = nnumberofcolors; for (int i = 0; i < nnumberofcolors; i++) // if flag set, replace grey color with window s background color if (btransparent && pbitmapinfo->bmicolors[i].rgbred==192 && pbitmapinfo->bmicolors[i].rgbgreen==192 && p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b B l u e = = 1 9 2 ) p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b R e d = G e t RVa l u e ( : : G e t S y s C o l o r ( C O L O R _ B T N FA C E ) ) ; p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b G r e e n = G e t G Va l u e ( : : G e t S y s C o l o r ( C O L O R _ B T N FA C E ) ) ; p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b B l u e = G e t B Va l u e ( : : G e t S y s C o l o r ( C O L O R _ B T N FA C E ) ) ; plogpal->palpalentry[i].pered = pbitmapinfo->bmicolors[i].rgbred; plogpal->palpalentry[i].pegreen = p B i t m a p I n f o - > b m i C o l o r s [ i ]. r g b G r e e n ; plogpal->palpalentry[i].peblue = pbitmapinfo->bmicolors[i].rgbblue; plogpal->palpalentry[i].peflags = 0; m_ppalette=new CPalette; m _ p P a l e t t e - > C r e a t e P a l e t t e ( p L o g P a l ); delete []plogpal; d c S c r e e n. S e l e c t P a l e t t e ( m _ p P a l e t t e, T R U E ); d c S c r e e n. R e a l i z e P a l e t t e (); // create device dependant bitmap HBITMAP bitmap = ::CreateDIBitmap(dcScreen.m_hDC, pbitmapinfoheader, C B M _ I N I T, pbitmappicturedata, pbitmapinfo, DIB_RGB_COLORS); // attach this new bitmap object to our CBitmap class A t t a c h ( b i t m a p ) ; // release dc ::ReleaseDC(NULL, dcscreen.detach()); 11.6 58 M F C
212 Wi n d o w s C B i t m a p 1. 1) C l a s s Wi z a r d C B i t m a p 2) CreateBitmapEx( ) 2. 1) CreateBitmapEx( ) C D C CDC dcscreen; d c S c r e e n. A t t a c h ( : : G e t D C ( N U L L ) ) ; 2) CDC dcmem; d c M e m. C r e a t e C o m p a t i b l e D C ( & d c S c r e e n ) ; 3) CreateCompatibleBitmap(&dcScreen, size.cx, size.cy); d c M e m. S e l e c t O b j e c t ( t h i s ) ; 4) CBrush bluebrush,greenbrush; b l u e b r u s h. C r e a t e S o l i d B r u s h ( R G B ( 0, 0, 2 5 5 ) ) ; g r e e n b r u s h. C r e a t e S o l i d B r u s h ( R G B ( 0, 2 5 5, 0 ) ) ; dcmem.fillrect(crect(0,0,size.cx,size.cy), &bluebrush); d c M e m. S e l e c t O b j e c t ( & g r e e n b r u s h ) ; dcmem.ellipse(0,0, size.cx, size.cy); HICON hicon=afxgetapp()->loadicon(idr_mainframe); d c M e m. D r a w I c o n ( ( s i z e. c x - 3 2 ) / 2, ( s i z e. c y - 3 2 ) / 2, h i c o n ) ; 5) d c M e m. D e l e t e D C ( ) ; ::ReleaseDC(NULL, dcscreen.detach()); 4
11 213 CD C D M F C #ifndef WZDBITMAP_H #define WZDBITMAP_H class CWzdBitmap : public CBitmap p u b l i c : D E C L A R E _ D Y N A M I C ( C W z d B i t m a p ) // Constructors C W z d B i t m a p ( ) ; void CreateBitmapEx(CSize size); // Implementation p u b l i c : virtual ~CWzdBitmap(); // Attributes int m_wi d t h ; int m_height; // Operations ; # e n d i f // WzdBitmap.cpp : implementation of the CWzdBitmap class / / #include s t d a f x. h #include W z d B i t m a p. h #include r e s o u r c e. h / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / // CWzdBitmap IMPLEMENT_DYNAMIC(CWzdBitmap, CBitmap) C W z d B i t m a p :: C W z d B i t m a p () m _ Wi d t h = 0 ; m _ H e i g h t = 0 ;
214 C W z d B i t m a p : : ~ C W z d B i t m a p ( ) void CWzdBitmap::CreateBitmapEx(CSize size) CDC dcmem; CDC dcscreen; d c S c r e e n. A t t a c h (:: G e t D C ( N U L L )); // create our bitmap in memory d c M e m. C r e a t e C o m p a t i b l e D C (& d c S c r e e n ); CreateCompatibleBitmap(&dcScreen, size.cx, size.cy); d c M e m. S e l e c t O b j e c t ( t h i s ); // do our drawing CBrush bluebrush,greenbrush; b l u e b r u s h. C r e a t e S o l i d B r u s h ( R G B ( 0, 0, 255 )); g r e e n b r u s h. C r e a t e S o l i d B r u s h ( R G B ( 0, 255, 0 )); dcmem.fillrect(crect(0,0,size.cx,size.cy), &bluebrush); d c M e m. S e l e c t O b j e c t (& g r e e n b r u s h ); dcmem.ellipse(0,0, size.cx, size.cy); HICON hicon=afxgetapp()->loadicon(idr_mainframe); d c M e m. D r a w I c o n (( s i z e. c x - 32 )/ 2,( s i z e. c y - 32 )/ 2, h i c o n ); // delete and release device contexts d c M e m. D e l e t e D C ( ) ; ::ReleaseDC(NULL, dcscreen.detach()); m _ Wi d t h = s i z e. c x ; m _ H e i g h t = s i z e. c y ;