VC Study Notes: Simple Drawing

Source: Internet
Author: User
Tags textout

VC Study Notes: Simple Drawing

SkySeraph Oct.29th 2009 HQU

Email-zgzhaobo@gmail.com QQ-452728574

Latest Modified Date: Oct.31th 2010 HQU reorganizing

// Note: take note of Lesson 4 of Sun Xin video 2009.10.29HQU

Draw line and related

Create a single document project and add the message functions OnLButtonDown and OnLButtonUp for the View class (not MainFrame)

① Add the member variable m_ptOrigin and CPoint to save the coordinate origin. Assign the value to 0 in the View constructor and m_pOrigin = point in OnLButtonDown.

CMyView: OnLButtonUp (UINT nFlags, CPoint point)

{

// Method 1: Use the SDK global function/API function in OnLButtonUp of the View class

HDC hdc; // get the device description table

Hdc =: GetDC (m_hWnd );

// Use the global function instead of the CWnd member function. m_hWnd is the view window handle. In the classes derived from CWnd, each data member saves the window handle,

// The CDC class also has the same function. m_hWnd is a protected variable and is used to construct the hwnd of the CWnd pointer of the m_hWnd object.

MoveToEx (hdc, m_ptOrigin.x, m_ptOrigin.y, NULL); // start point of the moving line

LineTo (hdc, point. x, point. y); // draw a line

: ReleaseDC (m_hWnd, hdc); // paired use; cannot be used in WM_PAINT

 

// Method 2: The CDC class provides a data member m_hDC to store the data in the CDC class related handles, similar to m_hWnd.

CDC * pDC = GetDC (); // defines the CDC class pointer

PDC-> MoveTo (m_ptOrigin );

PDC-> LineTo (point );

ReleaseDC (pDC );

 

// Method 3: CClientDC is used to create a DC object of the View window. The drawing can only be performed in the view's customer service area.

CClientDC dc (GetParent (); // = CClientDC dc (this); // The DC will be released from the end

// CClientDC constructor CClientDC (CWnd * pWnd); indicates that a pointer is required.

// Class CClientDC is derived from CDC. The Windows function GetDC is called during constructor and ReleaseDC is called during destructor. This means that the device context related to the CClientDC object is the customer zone of the window.

Dc. MoveTo (m_ptOrigin); // The variable dc of the CClientDC type is an object. The point operator is used to call the function of this object.

Dc. LineTo (point );

 

// Method 4: Use CWindowDC // by selecting the pointer of the window object, you can create DC objects of various windows or even DC objects of the entire screen window. The drawing can only be made in the window's customer service area.

CWindowDC dc (GetDesktopWindow (); // The entire Screen

// CWindowDC dc (GetParent (); // = CWindowDC dc (this); // view the customer area. The DC is automatically released at the end of the process.

// The CWindowDC class is inherited from CDC. It calls the Windows function GetWindowDC during construction and the ReleaseDC upon destruction.

Dc. MoveTo (m_ptOrigin );

Dc. LineTo (point );

 

// You can use a paint brush to select or change the color, width, width, and other attributes.

CPen pen (PS_DOT, 1, RGB (0,255, 0 ));

CClientDC dc (this );

CPen * pOldPen = dc. SelectObject (& pen );

Dc. MoveTo (m_ptOrigin );

Dc. LineTo (point );

Dc. SelectObject (pOldPen );

 

CView: OnLButtonUp (nFlags, point );

}

 

Paint Brush

Method 1 simple painter

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

CClientDC dc (this );

Dc. FillRect (CRect (m_ptOrigin, point), & brush );

// Note: Only the specified image brush is used to fill the area. Therefore, you do not need to select the image brush from the device description table. The device description table contains a default white brush.

 

Method 2 bitmap filling/bitmap painting

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

CBitmap bitmap;

Bitmap. LoadBitmap (IDB_BITMAP1 );

CBrush brush (& bitmap );

 

Method 3: transparent paint brush

CClientDC dc (this );

CBrush * pBrush = CBrush: FromHandle (HBRUSH) GetStockObject (NULL_BRUSH); // CBrush: FromHandle is a static member function

// FromHandle returns a pointer to the CBrush object when a Windows HBRUSH object handle is provided. Converts the paint brush handle to the object pointer.

CBrush * pOldBrush = dc. SelectObject (pBrush );

Dc. Rectangle (CRect (m_ptOrigin, point ));

Dc. SelectObject (pOldBrush );

 

Example of static member Principle

  • Example 1.0

# Include "iostream"

Class Point

{

Public:

Void output (){}

Static void init () {}// static function. It does not belong to a specific object. That is, when no Point object is generated, this class already exists in the code area of the program.

}

 

Void main ()

{

/* Method 1

Point pt; // construct an object

Pt. init ();

Pt. output ();

*/

// Method 2

Point: init ();/

Point: output (); // Error

}

Note: method 1 is correct; method 2 is incorrect because:

// Static member functions and static member variables belong to the class itself. When a class is loaded, they are allocated space. Therefore, you can use the Class Name: function name or Class Name: variable name access;

// Methods and data of non-static member functions and non-static members belonging to objects, that is, class objects should be generated first and then referenced through class objects.

 

  • 1.1:

Class Point

{

Public:

Void output (){}

Static void init () {x = 0; y = 0 ;}

Private:

Int x, y;

}

Void main ()

{Point: init (); // error: non-static members cannot be called in static member functions.

}

Note: non-static members (static member functions and static member variables) cannot be called in static member functions; otherwise, static members can be called in non-static member functions, you can run the void output () {init () ;}; check

Memory Model: No matter what operations are taken, program code is executed in the memory. We can access it only when there is a place in the memory.

 

 

  • Modify 1.2

Add static before int x, y;. No compilation error. Link error.

Note: static member variables must be initialized and must be performed outside the class.

Plus: int Point: x = 0; int Point: y = 0; OK!

 

Any line or slice

① Add the message processing function OnMouseMove first.

(2) set another bool variable (when the left mouse button is pressed to true, It is popped up to false): m_bDraw = 0 in the View constructor, m_bDraw = TRUE in OnLButtonDown & m_bDraw = FALSE in OnLButtonUp

③ Draw any continuous line

CClientDC dc (this); if (m_bDraw = TRUE)

{Dc. MoveTo (m_ptOrigin); dc. LineTo (point); m_ptOld = point ;}

④ Add a continuous line of color

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

CPen * pOldPen = dc. SelectObject (& pen );{......}

Dc. SelectObject (pOldPen );

⑤ Draw slices

First, add a CPoint member variable m_ptOld, and assign the value to m_ptOld when the left button is pressed.

... {Dc. MoveTo (m_ptOrigin );

Dc. LineTo (m_ptOld );

Dc. MoveTo (m_ptOrigin );

Dc. LineTo (point );

M_ptOld = point ;}...

6. Slice with edges: you only need to change the third sentence dc. MoveTo (m_ptOrigin) to dc. MoveTo (m_ptOld.

7. Function for setting the drawing mode: dc. SetROP2 (R2_BLACK );

 

 

Add knowledge points

  1. The CWnd class defines a member variable m_hWnd of the hwnd type, which is used to save the handle of the current window and has the public access permission. Based on the inheritance principle, the CWnd subclass has this member variable.
  1. In a single document, the view is in front of the MainFrame. At this time, if you write a mouseClick event for MainFrame, no response will be made.

Why the message with the left mouse click is not received in the framework class: The View Class Is Always overwritten in the Frame class. That is to say, all operations, including mouse movement and mouse clicking, can only be completed by the View class.

 

Differences between CDC, HDC, and pDC

① What is the difference between CDC * pDC and HDC hdc? similar to CWnd * pWnd and HWnd?

PDC is a class pointer, HDC is a windows handle

Obtain hdc through pDC: HDC hdc = pDC-> GetSafeHdc ();

Obtain pDC through hdc: CDC * pDC = new CDC; pDC-> Attach (hdc );

 

② There are essential differences between hDC and CDC

HDC is a data type in WINDOWS and a device description handle. CDC is a class in MFC, which encapsulates almost all HDC operations.

It can also be said that the HDC variable points to a piece of memory, which is used to describe the content of a device, so it can be considered that HDC defines a pointer;

The CDC class defines an object, which has a device description table defined by HDC and also contains functions related to HDC operations. This is the same as the difference between HPEN, CPen, and POINT and CPoint.

CDC encapsulates hDC-related operations. For example, a TextOut function of CDC hides its error detection, which can be simplified to the following degree: CDC: TextOut (int x, int y, const CString & str)

{

TextOut (m_hDC, x, y, (LPCTSTR) str, str. GetLength ());

}

M_hDC is the member variable HDC m_hDC of CDC;

CDC has an operator HDC () const {return m_hDC ;}

You can use it as an HDC

 

③ This is the pointer of the dc output target window. It can be used to obtain the window handle. What's strange about constructing an object with parameters?

Invalid CPaintDC zone dc, equivalent to BeginPaint, EndPaint

CClientDC customer zone dc, equivalent to GetDC, ReleaseDC

CWindowDC is equivalent to GetWindowDC and ReleaseDC.

CDC any dc, equivalent to CreateDC, DeleteDC

 

HDC is the original DC handle, and the first parameter of many APIS is an HDC type, such

HDC hDC =: GetDC (m_hWnd );

: MoveToEx (hDC, 0, 0, NULL );

: LineTo (hDC, 0,100 ,);

: ReleaseDC (m_hWnd, hDC );

 

In MFC, a CDC is added to encapsulate an API as a class for operation. So in MFC

CDC dc = GetDC ();

Dc. MoveTo (0, 0 );

Dc. LineTo (0,100 );

This-> ReleaseDC (& dc );

 

However, this is not enough, because the CDC requires you to release it by yourself, and there is another CClientDC in all the MFC, so that you can:

CClientDC dc (this );

Dc. MoveTo (0, 0 );

Dc. LineTo (0,100 );

The destructor of CClientDC will release itself.

 

DC is neither an object nor a device context. Like CClientDC, there are also CWindowDC and CPaintDC, but their painting ranges are different.

But in the end, it is only some encapsulation of HDC. You can directly reference m_hDC in the CDC class. This is the original HDC handle.

 

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.