当前位置:网站首页>Draw some simple graphics with MFC
Draw some simple graphics with MFC
2022-07-03 17:32:00 【Domineering Xiao Ming】
Catalog
Two 、 Use the brush to draw a rectangle
One 、 Draw a straight line

Drawing a straight line requires a starting point and an ending point , When the mouse is pressed, it is recorded as the starting point coordinate , When the mouse pops up, it is recorded as the end coordinate .
First, add the left mouse button press event in the class wizard , And the left mouse button pop-up event

Add a through the class wizard CPoint The member variable of type is used to record the position when the left mouse button is pressed

Supplementary event code
void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// Get device context
CDC* pDC = GetDC();
// Move to the starting position
pDC->MoveTo(m_pOrigin);
// Draw a straight line from the place to the end
pDC->LineTo(point);
Release the device context
ReleaseDC(pDC);
CView::OnLButtonUp(nFlags, point);
}
Change the brush style
stay MFC Through CPen Redefine a brush style
CPen pen( Pen shape , Line width , Color )
- Pen shape :PS_SOLID Solid line , PS_DASH Dotted line , PS_DOT Dotted line , PS_DOTDASH Point line
- Line width : Pixel for unit
- Color :RGB(RED,GREE,BLUR)
For example, I want to change the straight line drawn above to 5 Pixel width , green

At this time, you need to customize a brush , Replace the default brush of the device context . But be careful : When you stop using the custom brush, you need to replace the default brush of the device context
void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// Get device context
CDC* pDC = GetDC();
Define the brush
CPen pen(PS_SOLID, 5, RGB(0,255, 0));
// Replace the device context default brush , And keep the original brush
CPen* pOldPen = pDC->SelectObject(&pen);
pDC->MoveTo(m_pOrigin);
pDC->LineTo(point);
// The device context reverts to the original brush
pDC->SelectObject(pOldPen);
Release the device context
ReleaseDC(pDC);
CView::OnLButtonUp(nFlags, point);
}Two 、 Use the brush to draw a rectangle
When drawing a rectangle with a brush , You need the coordinates of the upper left corner and the lower right corner of the rectangle , When the mouse is pressed, it is recorded as the coordinates of the upper left corner , When the mouse pops up, it is recorded as the coordinates of the lower right corner .

void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// A brush
// Get device context , Compared with the above method, this method obtains the device context dc It's a local variable , Automatically release after the program ends , Manually release the device context out of order
CClientDC dc(this);
// Opaque brush
dc.Rectangle(CRect(m_pOrigin,point));
CView::OnLButtonUp(nFlags, point);
}
Transparent rectangle
If you want to draw a transparent rectangle , Get the transparent brush defined by the system , Replace the default brush of the device context , When the transparent brush is not used, you need to replace the original brush

void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// A brush
// Get device context
CClientDC dc(this);
// Transparent brush
// Get the system transparent brush
CBrush* brush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
// Change the device context brush
CBrush* oldBrush = dc.SelectObject(brush);
dc.Rectangle(CRect(m_pOrigin, point));
// Change the device context back to the original brush
dc.SelectObject(oldBrush);
CView::OnLButtonUp(nFlags, point);
}Custom color fill rectangle
Want to customize the filling of the rectangle , Just pass in the custom brush color fill as a parameter when drawing the fill rectangle

void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// A brush
// Get device context
CClientDC dc(this);
// Rectangle painted with fill color
CBrush brush(RGB(255, 0, 0));
dc.FillRect(CRect(m_pOrigin, point), &brush);
CView::OnLButtonUp(nFlags, point);
}Custom graphic fill rectangle
To draw a custom graphic fill, you first need to add a graphic you want to fill in the resource view , Here we use built-in resources Bitmap Draw a picture you want to fill .

First step : add to Bitmap resources
Right click “ project ”, choice “ add to ”, add to “ resources ”

choice Bitmap, Choose new

The second step : Draw custom graphics

After the “ Resource view ” You can view the added resources in

The third step : Load the drawn picture
Make a statement Bitmap, Load the picture just drawn from the resource view , Declared Bitmap Object as parameter , Declare a brush object
void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// A brush
// Get device context
CClientDC dc(this);
CBitmap bitmap;
// Load pictures from resources into bitmap in
bitmap.LoadBitmap(IDB_BITMAP1);
// With bitmap Is the parameter , Generate brush objects
CBrush brush(&bitmap);
// Fill the rectangular area
dc.FillRect(CRect(m_pOrigin, point), &brush);
CView::OnLButtonUp(nFlags, point);
}Four 、 Implement a brush
Pressing the left mouse button will start drawing as the mouse moves , Stop drawing when the left button pops up

Add a mouse movement event “OnMouseMove”, And a member parameter “m_bDraw(bool)” Record whether the left mouse button is pressed . When the mouse moves, first judge m_bDraw, If m_bDraw by true Then press the left key , Began to draw , Otherwise stop drawing
void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
m_bDraw = TRUE;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// Press the key up to set false
m_bDraw = false;
CView::OnLButtonUp(nFlags, point);
}
void CMFCApplication6View::OnMouseMove(UINT nFlags, CPoint point)
{
CClientDC dc(this);
// Draw a line after clicking the mouse
if (m_bDraw) {
dc.MoveTo(m_pOrigin);
dc.LineTo(point);
m_pOrigin = point;
}
CView::OnMouseMove(nFlags, point);
}
5、 ... and 、 Draw fan

The sector only needs to start every time when the left mouse button is pressed before the left mouse button pops up
void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
m_bDraw = TRUE;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
// Press the key up to set false
m_bDraw = false;
CView::OnLButtonUp(nFlags, point);
}
void CMFCApplication6View::OnMouseMove(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_DOT, 1, RGB(0, 255, 255));
// Draw fan
CPen* oldPen = dc.SelectObject(&pen);
if (m_bDraw) {
dc.MoveTo(m_pOrigin);
dc.LineTo(point);
}
dc.SelectObject(oldPen);
CView::OnMouseMove(nFlags, point);
}
6、 ... and 、 Drawing ellipse

Ellipse drawing mechanism is based on rectangle , Just call the draw ellipse function .
void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 5, RGB(0, 0, 255));
CPen* oldpen = dc.SelectObject(&pen);
// Drawing ellipse
dc.Ellipse(CRect(m_pOrigin, point));
CView::OnLButtonUp(nFlags, point);
}
7、 ... and 、 A circle

void CMFCApplication6View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bDraw = false;
CClientDC dc(this);
CPen pen(PS_SOLID, 5, RGB(0, 0, 255));
CPen* oldpen = dc.SelectObject(&pen);
// A circle
long len = point.x - m_pOrigin.x;
dc.Ellipse(m_pOrigin.x, m_pOrigin.y, m_pOrigin.x + len, m_pOrigin.y + len);
CView::OnLButtonUp(nFlags, point);
}
8、 ... and 、 The output text

void CMFCApplication6View::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bDraw = false;
CClientDC dc(this);
CString str(" Here comes Xiao Ming ");
// Set the text color
dc.SetTextColor(RGB(0, 255, 0));
// Set text font
CFont font;
// Create dot matrix fonts
// How many dot fonts are passed , Pass in 200, The font is 20
font.CreatePointFont(200, " Chinese Xingkai ");
// Output string
dc.TextOut(point.x, point.y, str, str.GetLength());
CView::OnLButtonUp(nFlags, point);
}边栏推荐
- c# .net 工具生态
- [RT thread] NXP rt10xx device driver framework -- RTC construction and use
- Simple use of unity pen XR grab
- SVN如何查看修改的文件记录
- Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
- What is the difference between cloud server and cloud virtual machine
- Notes on problems -- watching videos on edge will make the screen green
- STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
- SQL injection database operation foundation
- Detailed explanation of common network attacks
猜你喜欢

Golang单元测试、Mock测试以及基准测试

UE4 official charging resources, with a total price of several thousand

Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
![[RT thread] NXP rt10xx device driver framework -- pin construction and use](/img/75/b4f034bfe49409f76e7fd92758804e.png)
[RT thread] NXP rt10xx device driver framework -- pin construction and use

Unity notes unityxr simple to use

STM32实现74HC595控制

Kubernetes resource object introduction and common commands (III)

SQL injection database operation foundation

Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13

POM in idea XML graying solution
随机推荐
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
Kotlin学习快速入门(7)——扩展的妙用
PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
Vs code plug-in korofileheader
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
Swm32 series Tutorial 4 port mapping and serial port application
Detailed explanation of common network attacks
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
[UE4] brush Arctic pack high quality Arctic terrain pack
List of financial products in 2022
How do large consumer enterprises make digital transformation?
Managing multiple selections with MVVM - managing multiple selections with MVVM
The difference between i++ and ++i: tell their differences easily
Deops入门
Kubernetes resource object introduction and common commands (III)
POM in idea XML graying solution
c# .net 工具生态
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day