Use the dc brush to draw examples of rectangles lines and ellipses
- 2020-04-02 02:18:42
- OfStack
WindowDraw.cpp
#include<Windows.h>
#include<tchar.h>
//Declare window function
LRESULT CALLBACK WindowProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
//The entry function WinMain
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//Define window classes
WNDCLASS wndclass;
wndclass.lpfnWndProc=WindowProc;//Specify 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);//Gets the 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 failure "),_T(" My form "),MB_OK);
return 0;
}
//Create the form's real column
HWND hWnd = CreateWindow(_T(" My form "),_T(" Form drawing "),WS_OVERLAPPEDWINDOW,100,100,500,400,0,0,hInstance,0);
//Displays and updates forms
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
//Message loop
MSG msg;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
//Define the window function
LRESULT CALLBACK WindowProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_PAINT://Response plotting message
{
PAINTSTRUCT ps;
//For DC
HDC hDC = BeginPaint(hwnd,&ps);
//Create a solid line, width 1, red pen
HPEN hPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
//Select the pen into 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 I haven't changed my pen, so I'm drawing a rectangle with a red border and a blue area
Rectangle(hDC,100,100,200,170);
//Draw an ellipse, because the pen and brush have not changed, so draw a red border, blue area of the ellipse
Ellipse(hDC,200,230,260,300);
//The output text
TextOut(hDC,200,30,_T(" The drawing test "),4);
//Recovery drawing object
SelectObject(hDC,hOldPen);
SelectObject(hDC,hOldBrush);
//Delete drawing object
DeleteObject(hPen);
DeleteObject(hBrush);
//The release of the DC
EndPaint(hwnd,&ps);
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}