Copy
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"WindowClass" ;////in modern C and C++ declare string literals as const
WNDCLASSEXW wc = {0};
MSG msg;
//Registering the Window Class
wc.cbSize = sizeof (WNDCLASSEXW);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassExW(&wc);
//Creating the Window
CreateWindowExW(WS_EX_CLIENTEDGE, CLASS_NAME, L"Graphics Demo" , WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 680, 290, NULL, NULL, hInstance, NULL);
//The Message Loop
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast <int >(msg.wParam);
}
//WndProc procedure. Application acts on messages
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
HDC hdc=NULL;
PAINTSTRUCT ps;
const POINT polygon[10] = { 80, 185, 145, 185, 155, 130, 115, 145, 80, 135 };
const POINT bezier[4] = {320, 180, 370, 180, 375, 130, 400, 130};
HBRUSH hbrush=NULL;
HPEN hpen;
switch (msg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
int hDCLast;
hDCLast=SaveDC(hdc);//save current device context
MoveToEx(hdc,50,10,NULL);
SelectObject(hdc,hbrush);
//draws line around window
LineTo(hdc,620,10);
LineTo(hdc,620,240);
LineTo(hdc,50,240);
LineTo(hdc,50,10);
//draws ellipse with default pen and brush
Ellipse(hdc, 80, 30, 170, 90);
SelectObject(hdc, hbrush);
TextOutW(hdc,100,95,L"Ellipse" ,7);
//creates hbrush with colour green and draws roundrect
hbrush=CreateSolidBrush(RGB(0,255,0));
SelectObject(hdc, hbrush);
RoundRect(hdc, 200, 30, 290, 90, 15, 20);
TextOutW(hdc,180,95,L"Rounded rectangle" ,17);
//creates hbrush with yellow cross hatch and draws chord
hbrush=CreateHatchBrush(HS_CROSS ,RGB(255,255,0));
SelectObject(hdc, hbrush);
Chord(hdc, 320, 30, 410, 90, 320, 45, 410, 45);
TextOutW(hdc,350,95,L"Chord" ,5);
//creates hbrush white and draws pie
hbrush=CreateSolidBrush(RGB(255,255,255));
SelectObject(hdc, hbrush);
Pie(hdc, 450,30, 540,90, 320,45,410,45);
TextOutW(hdc,485,95,L"Pie" ,3);
//creates hbrush with red cross hatch and draws polygon
hbrush=CreateHatchBrush(HS_VERTICAL ,RGB(255,0,0));
SelectObject(hdc, hbrush);
Polygon(hdc, polygon, 5);
TextOutW(hdc,100,195,L"Polygon" ,7);
//creates hbrush black and draws rectangle
hbrush=CreateSolidBrush(RGB(0,0,0));
SelectObject(hdc, hbrush);
Rectangle(hdc, 200, 130, 280, 180);
TextOutW(hdc,210,195,L"Rectangle" ,9);
//draws Bezier curve
PolyBezier(hdc, bezier, 4);
TextOutW(hdc,340,195,L"Bezier" ,6);
//draws 3 lines with 3 different style in 3 different colours
MoveToEx(hdc,450,130,NULL);
hpen=CreatePen(PS_DASH,1,RGB(0, 0, 255));
SelectObject(hdc,hpen);
LineTo(hdc,540,130);
MoveToEx(hdc,450,155,NULL);
hpen=CreatePen(PS_DOT,1,RGB(255, 0, 255));
SelectObject(hdc,hpen);
LineTo(hdc,540,155);
MoveToEx(hdc,450,180,NULL);
hpen=CreatePen(PS_DASHDOTDOT,1,RGB(255, 0, 0));
SelectObject(hdc,hpen);
LineTo(hdc,540,180);
TextOutW(hdc,480,195,L"Lines" ,5);
EndPaint(hwnd, &ps);
RestoreDC(hdc,hDCLast);
DeleteObject(hbrush);
DeleteObject(hpen);
break ;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}