Draw lines in different windows in different ways

Source: Internet
Author: User
Tags transparent image

Draw lines in different windows using different methods

 
Void CMyView: OnLButtonUp (UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default // MessageBox ("WM_LBUTTONUP "); /// 1: Use the global function of the SDK to implement the draw line function // HDC hdc; hdc =: GetDC (m_hWnd);: MoveToEx (hdc, m_ptOld.x, m_ptOld.y, NULL );: LineTo (hdc, point. x, point. y);: ReleaseDC (m_hWnd, hdc); // 2: Use the CDC class of MFC to implement the draw line function // CDC * pDC = GetDC (); pDC-> MoveTo (m_ptOld); pDC-> LineTo (point); ReleaseDC (pDC); // 3: use the CClientDC class of MFC to implement the wire draw function // CClientDC dc (this); dc. moveTo (m_ptOld); dc. lineTo (point); // 4: Use the CWindowDC class of MFC to implement the line drawing function // CWindowDC dc_2 (this); dc_2.MoveTo (m_ptOld); dc_2.LineTo (point); // 5: use the MFC CWindowDC class to implement the line drawing function in the parent window // CWindowDC dc_3 (GetParent (); dc_3.MoveTo (m_ptOld); dc_3.LineTo (point); // 6: use the MFC CWindowDC class to implement the line drawing function in the desktop window // CWindowDC dc_4 (GetDesktopWindow (); dc_4.MoveTo (m_ptOld); dc_4.LineTo (point); CView: OnLButtonUp (nFlags, point );}
 

Now let's analyze the above Code.

1: first of all, what should I do if I want to complete the line drawing function?

Start by pressing the left mouse button, move the mouse to another location, and release the left mouse button. The entire operation requires the PC to be fully qualified for the draw line. After the PC is processed, the results are displayed on the display screen. So far, we have been dealing with hardware (operating the mouse and the screen we see ). Do we deal directly with hardware? NO! We only need to deal with the OS when dealing with hardware. The OS is the interpreter between people and machines.

2: The above code is the language used to communicate with the OS. Tell the OS what to do and how to do it. Then the OS translates our meaning to the machine, and then the machine completes the task.

3: I want the operating system to do this.

 
// 1: Use the global function of the SDK to implement the wire draw function // HDC hdc; // The device context handle hdc =: GetDC (m_hWnd ); // associate the window with the device context handle: MoveToEx (hdc, m_ptOld.x, m_ptOld.y, NULL); // The Position of the initialization point: LineTo (hdc, point. x, point. y); // move to this point: ReleaseDC (m_hWnd, hdc); // release the handle Resource
 

OS translates the above Code into machine code for the machine to see. The machine looks at it. Oh, that's it.

4: The methods 2 to 6 in the code above are based on method 1. When MFC encapsulates API functions into classes, it creates classes such as CDC and CClientDC. They all work in the same way as follows: In the constructor, You can associate the window with the device context handle, call the API function MoveTo in MoveTo, and call the API function LineTo in LineTo, for details, see the source code.

 
Void CMyView: OnLButtonUp (UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default // Device description table (Device Context, DC) it is an information structure that contains the physical output device and its driver. // In Windows, all graphic operations are completed through it. // MessageBox ("WM_LBUTTONUP"); // draw a colored line /// CPen pen (PS_SOLID, 10, RGB (255, 0, 0 )); // PS_SOLID solid CPen pen (PS_DASH, 1, RGB (255,0, 0); // PS_DASH dotted line, valid only when the width is <= 1. CClientDC dc (this); CPen * pOldPen = dc. selectObject (& pen); dc. moveTo (m_ptOld); dc. lineTo (point); dc. selectObject (pOldPen); // use a paint brush to draw images // CBrush brush (RGB (0,255, 0); // a total of 4 constructors CBrush * oldBrush; oldBrush = dc. selectObject (& brush); // first select your own paint brush to the device description table dc. fillRect (CRect (m_ptOld, point), NULL); // use a custom image brush // dc. fillRect (CRect (m_ptOld, point), & brush); // specifies the image brush // dc. fillRect (CRect (m_ptOld, point), NULL); // the color of the system's first default paint brush is white // bitmap paint brush // CBitmap bitmap; bitmap. loadBitmap (IDB_BITMAP1); CBrush brush2 (& bitmap); dc. fillRect (CRect (m_ptOld, point), & brush2); // transparent image brush // dc. rectangle (CRect (m_ptOld, point); // converts a paint brush handle to a paint brush object // CBrush * pBrush = CBrush: FromHandle (HBRUSH) GetStockObject (NULL_BRUSH )); oldBrush = dc. selectObject (pBrush); dc. rectangle (CRect (m_ptOld, point); m_bDraw = FALSE; CView: OnLButtonUp (nFlags, point );}
Void CMyView: OnMouseMove (UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default // MessageBox ("WM_MOUSEMOVE "); // draw continuous lines CClientDC dc (this); if (TRUE = m_bDraw) {dc. moveTo (m_ptOld); dc. lineTo (point); m_ptOld = point;} CView: OnMouseMove (nFlags, point );}
Void CMyView: OnLButtonUp (UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default // Device description table (Device Context, DC) it is an information structure that contains the physical output device and its driver. // In Windows, all graphic operations are completed through it. // MessageBox ("WM_LBUTTONUP"); // draw a colored line /// CPen pen (PS_SOLID, 10, RGB (255, 0, 0 )); // PS_SOLID solid CPen pen (PS_DASH, 1, RGB (255,0, 0); // PS_DASH dotted line, valid only when the width is <= 1. CClientDC dc (this); CPen * pOldPen = dc. selectObject (& pen); dc. moveTo (m_ptOld); dc. lineTo (point); dc. selectObject (pOldPen); // use a paint brush to draw images // CBrush brush (RGB (0,255, 0); // a total of 4 constructors CBrush * oldBrush; oldBrush = dc. selectObject (& brush); // first select your own paint brush to the device description table dc. fillRect (CRect (m_ptOld, point), NULL); // use a custom image brush // dc. fillRect (CRect (m_ptOld, point), & brush); // specifies the image brush // dc. fillRect (CRect (m_ptOld, point), NULL); // the color of the system's first default paint brush is white // bitmap paint brush // CBitmap bitmap; bitmap. loadBitmap (IDB_BITMAP1); CBrush brush2 (& bitmap); dc. fillRect (CRect (m_ptOld, point), & brush2); // transparent image brush // dc. rectangle (CRect (m_ptOld, point); // converts a paint brush handle to a paint brush object // CBrush * pBrush = CBrush: FromHandle (HBRUSH) GetStockObject (NULL_BRUSH )); oldBrush = dc. selectObject (pBrush); dc. rectangle (CRect (m_ptOld, point); m_bDraw = FALSE; CView: OnLButtonUp (nFlags, point );}
Void CMyView: OnMouseMove (UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default // MessageBox ("WM_MOUSEMOVE "); // draw continuous lines CClientDC dc (this); if (TRUE = m_bDraw) {dc. moveTo (m_ptOld); dc. lineTo (point); m_ptOld = point;} CView: OnMouseMove (nFlags, point );}
 

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.