The principle of windows-message mapping mechanism and simple drawing technique

Source: Internet
Author: User

the principle of windows-message mapping mechanism and simple drawing technique
1.MFC message mapping mechanism
As we all know, theWindows program is based on message programming , but in MFC has encapsulated the framework of the message mechanism, we need to understand its implementation principle, in order to deep learning and proficient in Visual C + +.

* * (1). The principle of the message mapping mechanism:
The specific implementation of the MFC message mapping mechanism is to define a static table of message and message functions, the message map, in each class that can receive and process messages. In the message-map table, the message is paired with the corresponding message-handling function pointer. All messages that a class can handle and the addresses of their corresponding message handlers are listed in the static table corresponding to this class. When a message needs to be processed, the program simply searches the static table of the message and checks if the table contains the message, knowing that the class can handle the message. If the message can be processed, it is also easy to find and invoke the corresponding message handler function according to the static table. **

(2). Three code required to add a message in Visual C + +
First place in the header file:

The message that this class can handle is defined here in the header file

The second place in the source file:

The message map table that defines the class at the beginning of the source file

Third place in the source file:

The specific logic code implementation of the message in the source file here

(3). The underlying implementation process of the message-mapping mechanism
MFC maintains a window handle in the background and corresponding C + + object pointer of the table, when the window receives a message, the first parameter of the message specifies the message and which window handle correlation, through the table, you can find the relevant C + + object pointers, This pointer is then passed to the base class of the Application Framework window class, which invokes a window procedure function called WindowProc. The function is defined in the WinCore.cpp file, and this function calls the ONWNDMSG function to complete the true message routing, which is what the message map is done by this function.

Windproc

Onwndmsg code is too long, here to give the declaration and file path, you can analyze it yourself, the code and the Platform SDK to write a window callback function is similar to the message is the judgment and processing.

Path: C:\Program Files (x86) \microsoft Visual Studio 12.0\vc\atlmfc\src\mfc\wincore.cpp

diagram The underlying implementation mechanism:

2.MFC ways to draw lines
(1). Drawing with Platform SDK global function

    //平台的SDK,HDC是设备上下文    HDC hdc = ::GetDC(m_hWnd);//获取DC    MoveToEx(hdc, m_ptOrigin.x, m_ptOrigin.y, nullptr);    pointpoint.y);    ::ReleaseDC(m_hWnd, hdc);//记得释放DC

(2). Using MFC's CDC class drawing
The CDC class is a class that is dedicated to drawing in MFC

    //需要手动管理CDC对象的创建和释放    CDC*= GetDC();//得到DC    pDC->MoveTo(m_ptOrigin);    pDC->LineTo(point);//画线的API    ReleaseDC(pDC);//释放DC

(3). Using CClientDC
Inherit CDC, can automatically get and release DC

    //自动创建和释放CDC对象    CClientDC dc(this);//画到视图窗口上     dc.MoveTo(m_ptOrigin);    dc.LineTo(point);

Draw on the main window

    //自动创建和释放CDC对象     dc(GetParent());//画到主窗口上    dc.MoveTo(m_ptOrigin);    dc.LineTo(point);

(4). Using CWINDOWDC

    //Windows屏幕的DC,画出的线条支持直接出现在屏幕上    //CWindowDC dc(this);//画在视图上    //CWindowDC dc(GetParent());//画在主框架窗口上    dc.MoveTo(m_ptOrigin);    dc.LineTo(point);

(5). Draw lines in the desktop window

    dc(GetDesktopWindow());//直接画在屏幕上    dc.MoveTo(m_ptOrigin);    dc.LineTo(point);

(6). Draw colored Lines

    1, RGB(25511));//画红色的点线    CClientDC dc(this);     CPen* pOldPen = dc.SelectObject(&pen);//保存先前的画笔,必要的一步     dc.MoveTo(m_ptOrigin);    dc.LineTo(point);     dc.SelectObject(pOldPen);//恢复先前的画笔

(7). Draw CBrush drawing with brush

    //画出图片bitmap    CBitmap bitmap;    //加载位图资源    bitmap.LoadBitmap(IDR_MAINFRAME);//位图的资源ID    brush(&bitmap);    dc(this);    //填充矩形    dc.FillRect(CRect(m_ptOrigin, point), &brush);

(8). Draw a continuous line

    CClientDC dc(this);    if (m_bDraw)//m_bDraw是鼠标左键是否按下的BOOL成员变量,这里必须为TRUE    {        dc.MoveTo(m_ptOrigin);        dc.LineTo(point);        point;//这次的终点作为下一次绘制线段的起点    }

(9). Draw a continuous line with color

    CClientDC dc(this);    //改变画笔的颜色    1, RGB(25500));    //将画笔选入设备描述表    if (m_bDraw)//m_bDraw是鼠标左键是否按下的BOOL成员变量,这里必须为TRUE    {        dc.MoveTo(m_ptOrigin);        dc.LineTo(point);        point;    }    dc.SelectObject(pOldPen);//恢复之前的画笔

(10). Draw Fan

    CClientDC dc(this);    1, RGB(25500));    CPen* pOldPen = dc.SelectObject(&pen);    if (m_bDraw)    {        dc.MoveTo(m_ptOrigin);//起点不变,画出来的是扇形        dc.LineTo(point);      }    dc.SelectObject(pOldPen);

(11). Draw a sector with a border

    CClientDC dc(this);    1, RGB(25500));    CPen* pOldPen = dc.SelectObject(&pen);    if (m_bDraw)    {          dc.MoveTo(m_ptOrigin);        dc.LineTo(point);        dc.LineTo(m_ptOld);//这次的线段终点画到上一条线段的终点就可以形成边界        point;//保存上一条线段的终点    }    dc.SelectObject(pOldPen);

(12). Draw a rectangle

    //画出矩形并且使用画刷来填充颜色    dc(this);    brush(RGB(255, 0, 0));    dc.FillRect(CRect(m_ptOrigin, point)

Draw a transparent rectangle

    CClientDC dc(this);    //创建透明的画刷,GetStockObject返回的是HGDIOBJ,需要强制转换成HBRUSH,而且还需要将句柄转换成对象指针,要使用FromHandle    //这个静态函数    CBrush* pBrush = CBrush::FromHandle(static_cast<HBRUSH>(GetStockObject(NULL_BRUSH)));    //保存旧的画刷    CBrush* pOldBrush = dc.SelectObject(pBrush);    //画出矩形    dc.Rectangle(CRect(m_ptOrigin, point));    //恢复旧的画刷    dc.SelectObject(pOldBrush);

(13). Set Drawing Mode

    CClientDC dc(this);    dc.SetROP2(R2_BLACK);//设置绘图的模式, 查看MSDN的详细介绍

3.MFCBuild
The build command can be run directly with F7.

4. Summary
In the use of MFC, we need to understand its underlying implementation mechanism, so that we encounter problems in the time to know which part of the problem, this is our only way, and in the drawing of the time, we need to clear the painting of the parent view is.
The following is my summary of the mind map, to facilitate the formation of good thinking, a kind of two, is different time to do.
(1).
(2).
Picture resource sharing : HTTPS://YUNPAN.CN/CPCZAZE4HU5C4 access password f09a

The principle of windows-message mapping mechanism and simple drawing technique

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.