Using a DC brush to draw rectangles, lines, and ellipses _c language

Source: Internet
Author: User

WindowDraw.cpp

Copy Code code as follows:

/* Simply use the DC brush to draw a straight line, rectangle, oval * * *
#include <Windows.h>
#include <tchar.h>
Declaring a window function
Lresult CALLBACK WindowProc (
HWND hwnd,
UINT umsg,
WPARAM WPARAM,
LPARAM LPARAM
);
Entry function WinMain
int WINAPI WinMain (
HINSTANCE HInstance,
HInstance hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
Defining a Window class
Wndclass Wndclass;
wndclass.lpfnwndproc=windowproc;//The specified window function
wndclass.cbclsextra=0;
wndclass.cbwndextra=0;
Wndclass.style=cs_hredraw|cs_vredraw;
wndclass.lpszclassname=_t ("my form");
Wndclass.hinstance=hinstance;
Wndclass.hcursor=loadcursor (Null,idc_arrow);//Get Standard mouse cursor
wndclass.hicon=0;
Wndclass.hbrbackground= (Hbrush) (color_window+1);
wndclass.lpszmenuname=0;
Register window class
if (RegisterClass (&wndclass) ==0)
{
MessageBox (0,_t ("Register window class failed"), _t ("my Form"), MB_OK);
return 0;
}
Create a form real column
HWND hwnd = CreateWindow (_t ("my Form"), _t ("form Drawing"), ws_overlappedwindow,100,100,500,400,0,0,hinstance,0);
displaying and updating forms
ShowWindow (hwnd,sw_show);
UpdateWindow (HWND);
Message loops
MSG msg;
while (GetMessage (&msg,0,0,0))
{
TranslateMessage (&AMP;MSG);
DispatchMessage (&AMP;MSG);
}
return 0;
}
Defining window Functions
Lresult CALLBACK WindowProc (
HWND hwnd,
UINT umsg,
WPARAM WPARAM,
LPARAM LPARAM
)
{
Switch (umsg)
{
Case wm_paint://Response Drawing message
{
Paintstruct PS;
Get DC
HDC HDC = BeginPaint (HWND,&AMP;PS);
Create a solid line with a width of 1, a red pen
Hpen Hpen=createpen (Ps_solid,1,rgb (255,0,0));
To select a pen into a DC
Hpen holdpen= (Hpen) SelectObject (Hdc,hpen);
Draw a red line
Movetoex (Hdc,10,10,null);
LineTo (hdc,90,50);
Create a blue Brush
Hbrush hbrush = CreateSolidBrush (RGB (0,0,255));
Hbrush holdbrush= (Hbrush) SelectObject (Hdc,hbrush);
Draws a rectangle, because the pen does not change, so it draws a red border, a rectangle in the blue area
Rectangle (hdc,100,100,200,170);
Draws an ellipse, because the pen and brush are not swapped, so the red border is drawn, and the ellipse of the blue area
Ellipse (hdc,200,230,260,300);
Output text
TextOut (hdc,200,30,_t ("Drawing Test"), 4);
Recover drawing objects
SelectObject (Hdc,holdpen);
SelectObject (Hdc,holdbrush);
Delete a drawing object
DeleteObject (Hpen);
DeleteObject (Hbrush);
Free DC
EndPaint (HWND,&AMP;PS);

}
Break
Case WM_CLOSE:
PostQuitMessage (0);
Break
Default
return DefWindowProc (Hwnd,umsg,wparam,lparam);
}
return 0;
}

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.