/* 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 (&MSG);
DispatchMessage (&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,&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,&PS);
}
Break
Case WM_CLOSE:
PostQuitMessage (0);
Break
Default
return DefWindowProc (Hwnd,umsg,wparam,lparam);
}
return 0;
}