VC Study Notes: Text Graphics

Source: Internet
Author: User

VC Study Notes: Text Graphics

 SkySeraph OCT.30th 2010 HQU

Email-zgzhaobo@gmail.com QQ-452728574

Latest Modified Date: NOV.2th 2010 HQU

 

Text output

  • Output text in the specified font format [1]

Void CSpecificFontView: OnDraw (CDC * pDC)

{

CSpecificFontDoc * pDoc = GetDocument (); // obtain the document object associated with the view.

ASSERT_VALID (pDoc); // verify the Document Object

CFont Font;

Font. CreateFont (24, 24, 0, 0, FW_NORMAL, 0, TRUE, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,

CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "");

CFont * pOldFont = NULL;

POldFont = pDC-> SelectObject (& Font );

PDC-> TextOut (10, 10, "the same world, the same dream! ");

 

PDC-> SelectObject (pOldFont );

Font. DeleteObject ();

}

 

  • Output Information at the center of the rectangle area [1]

Void CDrawTextView: OnDraw (CDC * pDC)

{

CDrawTextDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

CRect rc (100,20, 300,200 );

CString str = "I love Beijing, I love Olympics! ";

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

PDC-> FrameRect (rc, & brush); // draw a rectangle with a black brush

PDC-> DrawText (str, rc, DT_CENTER | DT_SINGLELINE | DT_VCENTER); // draw text in the rectangle area

Brush. DeleteObject (); // release the painter object

}

Function Description:

FrameRect

Use the specified image brush to draw a border for the specified rectangle. The Border width and height are always a logical unit.

DrawText

Used to output text & TextOut in a region to output text in a specified Coordinate

 

  • Output by using the tabulation null value text [1]

Void CTaboutView: OnDraw (CDC * pDC)

{

CTaboutDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

Int pts [4] = {100,150,300,400 };

PDC-> TabbedTextOut (, "\ t2008 \ t Beijing Olympics \ t the same world \ t the same dream", 4, pts, 0 );

}

 

  • Set Font and color [1]

Void CSelFontView: OnDraw (CDC * pDC)

{

CSelFontDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

CFont Font; // define a Font object

// Create a font

Font. CreateFont (, FW_NORMAL, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,

CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "");

CFont * pOldFont = NULL; // defines a font pointer.

POldFont = pDC-> SelectObject (& Font); // select the Font

PDC-> SetTextColor (RGB (255, 0, 0); // you can specify the color of the output text.

PDC-> TextOut (, 50, "Beijing Olympics"); // output text information

PDC-> SelectObject (pOldFont); // restore the selected font

Font. DeleteObject (); // release the Font object

Font. CreatePointFont (120, "", pDC); // create a Font

POldFont = pDC-> SelectObject (& Font); // select the Font

PDC-> TextOut (, 70, "same world ");

PDC-> TextOut (, 90, "same dream ");

PDC-> SelectObject (pOldFont); // restore the selected font

Font. DeleteObject ();

}

 

  • Output text in path [1]

Void CPathFontView: OnDraw (CDC * pDC)

{

CPathFontDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

CFont mFont;

// Create a font

VERIFY (mFont. CreateFont (

150, 50, 0, 0, FW_HEAVY, TRUE, FALSE,

0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,

CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,

DEFAULT_PITCH | FF_SWISS, ""));

CPen pen (PS_SOLID, 2, RGB (255, 0, 0 ));

PDC-> SelectObject (& pen );

PDC-> BeginPath (); // start a path

CFont * pOldFont = pDC-> SelectObject (& mFont );//

PDC-> SetBkMode (TRANSPARENT); // set the background mode of the canvas to TRANSPARENT.

PDC-> TextOut (30,30, "1 lunar exploration satellite ");

PDC-> EndPath (); // close the path

PDC-> StrokePath (); // draw a path with the current paint brush

MFont. DeleteObject ();

PDC-> SelectObject (pOldFont );

}

 

  • Output transparent text on the image background [1]

Void CTransTextView: OnDraw (CDC * pDC)

{

CTransTextDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

// TODO: add draw code for native data here

CBitmap bmp; // defines a bitmap.

Bmp. LoadBitmap (IDB_BKBITMAP); // loads a bitmap.

BITMAP bInfo; // defines the BITMAP structure.

Bmp. GetBitmap (& bInfo); // retrieves bitmap Information

Int width = bInfo. bmWidth;

Int height = bInfo. bmHeight;

CDC memDC; // defines a device context

MemDC. CreateCompatibleDC (pDC); // create a compatible device context

MemDC. SelectObject (& bmp); // select a bitmap object

PDC-> BitBlt (, width, height, & memDC, SRCCOPY); // draw a bitmap in the device context

MemDC. DeleteDC (); // release the device context

Bmp. DeleteObject (); // release a bitmap object

CFont mFont; // define a font object

// Create a font

VERIFY (mFont. CreateFont (

24, 20, 0, 0, FW_HEAVY, FALSE, FALSE,

0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,

CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,

DEFAULT_PITCH | FF_SWISS, ""));

CFont * pOldFont = pDC-> SelectObject (& mFont );

PDC-> SetBkMode (TRANSPARENT); // you can specify a TRANSPARENT background.

PDC-> SetTextColor (RGB (0,255, 0); // you can specify the text color.

PDC-> TextOut (60, 60, "Rural Life ");

PDC-> SelectObject (pOldFont );

MFont. DeleteObject ();

}

 

 

Drawing

  • Some functions

Painting point

CDC: SetPixel ()/CDC: SetPixelV ()

The latter does not need to return the RGB of the actual pixel, which is faster

Current location acquisition

CDC: GetCurrentPosition

Prototype CPoint GetCurrentPosition () const;

Line

Polyline/PolyPolyline/PolylineTo

Polyline and PolyPolyline do not use the current location or update the current location; while PolylineTo always sets the current location

After the line is drawn, the position of the line end is set to the new current position.

Rectangle and rounded rectangle

Rectangle and RoundRect

 

 

  • Use a line to draw a polygon [1]

Void CDrawLineView: OnDraw (CDC * pDC)

{

CDrawLineDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

// Draw a rectangle

CPen pen (PS_SOLID, 2, RGB (255, 0, 0 ));

CPen * pOldPen = pDC-> SelectObject (& pen );

PDC-> MoveTo (50, 30 );

PDC-> LineTo (); // draw the upper border

PDC-> LineTo (240,120); // draw the right border

PDC-> LineTo (50,120); // draw the bottom border

PDC-> LineTo (50, 30); // draw the Left Border

// Draw multi-Deformation

PDC-> MoveTo (); // sets the current Coordinate

PDC-> LineTo (); // draw the upper border

PDC-> LineTo (450,100); // draw the right oblique border

PDC-> LineTo (400,150); // draw the right oblique border

PDC-> LineTo (300,150); // draw the bottom border

PDC-> LineTo (250,100); // draw the upper left border

PDC-> LineTo (); // draw the upper left border

// Draw an arc

CRect rc (600,100, 50 );

PDC-> Arc (600,100,520, 50, 70,560, 30 );

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

PDC-> FrameRect (rc, & brush );

PDC-> SelectObject (pOldPen );

}

  • Draw a polygon directly [1]

Void CPolyView: OnDraw (CDC * pDC)

{

CPolyDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

CRect rc (20, 20, 80, 80 );

PDC-> Rectangle (rc); // draw a rectangular area

CRect RndRC (20,100, 80,160 );

PDC-> RoundRect (RndRC, CPoint (10, 10); // draw a rounded rectangle

CPoint pts [6] = {CPoint (450,100), CPoint (), CPoint ),

CPoint (400,150), CPoint (300,150), CPoint (250,100)}; // defines the polygon endpoint.

PDC-> Polygon (pts, 6); // draw a Polygon

}

  • Draw the widget appearance [1]

Void CDrawCtrlView: OnDraw (CDC * pDC)

{

CDrawCtrlDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

// Draw button

CRect rc (50, 50 );

PDC-> DrawFrameControl (rc, DFC_BUTTON, DFCS_BUTTONPUSH );

// Click the close button to draw the title bar.

CRect CapRC (130,50, 160,80 );

PDC-> DrawFrameControl (CapRC, DFC_CAPTION, DFCS_CAPTIONHELP );

// Draw the scroll bar button

CRect ScrollRC (170,50, 80 );

PDC-> DrawFrameControl (ScrollRC, DFC_SCROLL, DFCS_SCROLLCOMBOBOX );

}

Function Description:

BOOL DrawFrameControl (LPRECT lpRect, UINT nType, UINT nState );

LpRect: the area of the rectangle where the control is located; nState: draws the style or status of the control.

NType: control type, DFC_BUTTON, DFC_CAPTION/title bar, DFC_MENU, DFC_SCROLL/scroll bar;

 

  • Fill the rectangular area [1]

Void CFillRCView: OnDraw (CDC * pDC)

{

CFillRCDoc * pDoc = GetDocument (); // get the Document Object

ASSERT_VALID (pDoc); // verification document

CRect rc (100,120 );

CBrush brush (RGB (128,128,128 ));

// Fill the area with colors

PDC-> FillRect (rc, & brush );

Brush. DeleteObject ();

CBitmap bmp;

Bmp. LoadBitmap (IDB_BKBITMAP );

Brush. CreatePatternBrush (& bmp); // create a bitmap image brush

CRect bmp rc (200,120 );

// Fill the area with bitmap

PDC-> FillRect (bmp rc, & brush );

Bmp. DeleteObject (); // release the bitmap

Brush. DeleteObject (); // release the paint brush

// Fill the selection area

CRect rectrc (300,120 );

CRect hrc (350,140, 60 );

PDC-> Rectangle (rectrc );

PDC-> Rectangle (hrc );

HRGN hRect = CreateRectRgn (300,120 );

HRGN hrgn = CreateRectRgn (350,140, 60 );

HRGN hret = CreateRectRgn (0, 0, 0 );

CombineRgn (hret, hRect, hrgn, RGN_AND); // select a combination to obtain the public parts of the two selections.

Brush. CreateSolidBrush (RGB (, 0); // create a color paint brush

CRgn rgn; // defines a selection object

Rgn. Attach (hret); // append a selection handle to the selection object

PDC-> FillRgn (& rgn, & brush );//

Brush. DeleteObject ();

Rgn. Detach (); // separate the constituency

DeleteObject (hRect );

DeleteObject (hrc );

DeleteObject (hret );

}

 

 

Drawing example: course score Histogram

① Use MFC AppWizard to create a default single-document application Ex_Draw

② Add a member function DrawScore to the CEx_DrawView class to draw a histogram Based on the score. The code of this function is as follows:

Void CEx_DrawView: DrawScore (CDC * pDC, float * fScore, int nNum)

// FScore is the score array pointer, and nNum is the number of students

{

Int nScoreNum [] = {0, 0, 0, 0, 0}; // the initial value of the number of people in each score segment

// The following is used to count the number of people in each shard

For (int I = 0; I <nNum; I ++)

{

Int nSeg = (int) (fScore [I])/10; // obtain the value in the "Ten" bits.

If (nSeg <6) nSeg = 5; // <60 minutes

If (nSeg = 10) nSeg = 9; // when the value is 100, the value is greater than 90.

NScoreNum [nSeg-5] ++; // count each shard

}

Int nSegNum = sizeof (nScoreNum)/sizeof (int); // calculates the number of shards.

// Calculate the maximum number of people in the score segment

Int nNumMax = nScoreNum [0];

For (I = 1; I <nSegNum; I ++)

{

If (nNumMax <nScoreNum [I]) nNumMax = nScoreNum [I];

}

CRect rc;

GetClientRect (rc );

Rc. DeflateRect (40, 40); // reduce the rectangle size

Int nSegWidth = rc. Width ()/nSegNum; // calculate the Width of each segment

Int nSegHeight = rc. Height ()/nNumMax; // calculates the unit Height of each segment.

COLORREF crSeg = RGB (192,); // define a color variable

CBrush brush1 (HS_FDIAGONAL, crSeg );

CBrush brush2 (HS_BDIAGONAL, crSeg );

CPen pen (PS_INSIDEFRAME, 2, crSeg );

CBrush * oldBrush = pDC-> SelectObject (& brush1); // select brush1 into the device Environment

CPen * oldPen = pDC-> SelectObject (& pen); // select the pen to the device Environment

CRect rcSeg (rc );

RcSeg. right = rcSeg. left + nSegWidth; // make the width of each rectangle equal to nSegWidth

CString strSeg [] = {"<60", "60-70", "70-80", "80-90", "> = 90 "};

CRect rcStr;

For (I = 0; I <nSegNum; I ++)

{

// Ensure that the adjacent rectangular fill styles are different

If (I % 2)

PDC-> SelectObject (& brush2 );

Else

PDC-> SelectObject (& brush1 );

RcSeg. top = rcSeg. bottom-nScoreNum [I] * nSegHeight-2; // calculate the height of each rectangle

PDC-> Rectangle (rcSeg );

If (nScoreNum [I]> 0)

{

CString str;

Str. Format ("% d", nScoreNum [I]);

PDC-> DrawText (str, rcSeg, DT_CENTER | DT_VCENTER | DT_SINGLELINE );

}

RcStr = rcSeg;

RcStr. top = rcStr. bottom + 2; rcStr. bottom + = 20;

PDC-> DrawText (strSeg [I], rcStr, DT_CENTER | DT_VCENTER | DT_SINGLELINE );

// I will discuss it later

RcSeg. OffsetRect (nSegWidth, 0); // right shift rectangle

}

PDC-> SelectObject (oldBrush); // restores the original paint brush attribute

PDC-> SelectObject (oldPen); // restore the original paint brush attribute

}

③ Add the following code to the CEx_DrawView: OnDraw function:

Void CEx_DrawView: OnDraw (CDC * pDC)

{

CEx_DrawDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

Float fScore [] = {66,82, 79,74, 86,82, 67,60, 45,44, 77,98, 65,90, 66,76, 66,

};

DrawScore (pDC, fScore, sizeof (fScore)/sizeof (float ));

}

 

Author: SKySeraph

Email/GTalk: zgzhaobo@gmail.com QQ: 452728574

From: http://www.cnblogs.com/skyseraph/

The copyright of this article is shared by the author and the blog. You are welcome to repost this article. However, you must keep this statement without the author's consent and provide the original article connection clearly on the article page. Please respect the author's Labor achievements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.