Code:
// Resources.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define _WIN32_WINNT 0x500
#include <windows.h>
#include <math.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
//---------------------------------------------------------------------------
#define REALLYSIMPLE_CIRCLE_COLOR RGB (0, 128, 255)
const char *ClsName = "FundApp";
const char *WndName = "Resources Fundamentals";
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
void DrawRectangle(HWND hWnd);
void ReallySimpleCircle (HDC hdc, LONG radius, POINT ptCenter, COLORREF crColor);
void Win32APIGDICircle (HDC hdc, LONG radius, POINT ptCenter, int R, int G, int B);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;
WNDCLASSEX WndClsEx;
// Create the application window
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// Register the application
RegisterClassEx(&WndClsEx);
// Create the window object
hWnd = CreateWindowEx(0,
ClsName,
WndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
// Find out if the window was created
if( !hWnd ) // If the window was not created,
return FALSE; // stop the application
// Display the window to the user
ShowWindow(hWnd, nCmdShow);// SW_SHOWNORMAL);
UpdateWindow(hWnd);
// Decode and treat the messages
// as long as the application is running
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
// Circle variables
POINT ptCenter = {450,300};
HDC hdc = GetDC(hWnd);
COLORREF crColor;
// Handle messagse
switch(Msg)
{
case WM_PAINT:
{
//DrawRectangle(hWnd);
//ReallySimpleCircle (hdc, 50, ptCenter, REALLYSIMPLE_CIRCLE_COLOR);
Win32APIGDICircle (hdc, 200, ptCenter, 0, 255, 128);
break;
}
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
// Process the left-over messages
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
// If something was not done, let it go
return 0;
}
// Win32API GDI circle:
void Win32APIGDICircle (HDC hdc, LONG radius, POINT ptCenter, int R, int G, int B)
{
SelectObject (hdc, GetStockObject (DC_BRUSH));
SetDCBrushColor(hdc,RGB(R,G,B));
Ellipse (hdc,
ptCenter.x - radius,
ptCenter.y + radius,
ptCenter.x + radius,
ptCenter.y - radius);
}
// the simplest way to draw a circle:
void ReallySimpleCircle (HDC hdc, LONG radius, POINT ptCenter, COLORREF crColor)
{
LONG cx = 0, cy;
double xLimit = sqrt ((double) (radius * radius) / 2);
while (cx++ <= xLimit)
{
cy = (LONG) floor (sqrt ((double) radius * radius - cx * cx));
SetPixel (hdc, cx + ptCenter.x, cy + ptCenter.y, crColor); // 45-90 degrees
SetPixel (hdc, cx + ptCenter.x, -cy + ptCenter.y, crColor); // 270-315 degrees
SetPixel (hdc, -cx + ptCenter.x, cy + ptCenter.y, crColor); // 90-135 degrees
SetPixel (hdc, -cx + ptCenter.x, -cy + ptCenter.y, crColor); // 225-270 degrees
SetPixel (hdc, cy + ptCenter.x, cx + ptCenter.y, crColor); // 0-45 degrees
SetPixel (hdc, cy + ptCenter.x, -cx + ptCenter.y, crColor); // 315-360 degrees
SetPixel (hdc, -cy + ptCenter.x, cx + ptCenter.y, crColor); // 135-180 degrees
SetPixel (hdc, -cy + ptCenter.x, -cx + ptCenter.y, crColor); // 180-225 degrees
}
}