Code:
#include <windows.h>
const wchar_t g_szClassName[] = L"myWindowClass";
// You can write your own graphic functions
void twoColorLine(HDC & hDC,int x1,int y1,int x2,int y2,int red1,int green1,int blue1,int red2,int green2,int blue2)
{
HPEN hPen1,hPen2;
// Find the midpoint
int mx=(x1+x2)/2;
int my=(y1+y2)/2;
//draw the first half
hPen1 = CreatePen(PS_SOLID, 3, RGB(red1,green1,blue1));
SelectObject(hDC, hPen1);
MoveToEx(hDC, x1,y1,0);
LineTo(hDC, mx,my);
DeleteObject( hPen1 );
//draw the second half
hPen2 = CreatePen(PS_SOLID, 3, RGB(red2,green2,blue2));
SelectObject(hDC, hPen2);
MoveToEx(hDC, mx,my,0);
LineTo(hDC, x2,y2);
DeleteObject( hPen2 );
}
// Recursively define a circle as being made of a circle and 4 more recursive circles
// Have fun with this!
void rCircle(HDC & hDC,int x,int y,int r,int red1,int green1,int blue1)
{
HPEN hPen1;
int x1=x-r;
int y1=y-r;
int x2=x+r;
int y2=y+r;
int thickness=r/5;
if(thickness<1)thickness=1;
hPen1 = CreatePen(PS_SOLID,thickness, RGB(red1,green1,blue1));
SelectObject(hDC, hPen1);
Ellipse(hDC,x1,y1,x2,y2);
DeleteObject( hPen1 );
// if the radius is less than 2 pixels we dont recurse (base case)
if(r>2)
{
int nux,nuy,nur,nuRed,nuGreen,nuBlue;
nuy=r-y/2; // there's a bug on this line, but serendipity is the mother of art
//nuy=y-r/2; // this is the corrected line
nux=x;
nur=r/2;
nuRed=(red1*9)/10;
nuGreen=(green1*9)/10;
nuBlue=(blue1*9)/10;
rCircle(hDC,nux,nuy,nur,nuRed,nuGreen,nuBlue);
nuy=y+r/2;
rCircle(hDC,nux,nuy,nur,nuRed,nuGreen,nuBlue);
nuy=y;
nux=x-r/2;
rCircle(hDC,nux,nuy,nur,nuRed,nuGreen,nuBlue);
nux=x+r/2;
rCircle(hDC,nux,nuy,nur,nuRed,nuGreen,nuBlue);
}
}
// The Callback function is where the logic of a Windows program resides.
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT Ps;
HPEN hPen;
HBRUSH hBrush;
HFONT hFont;
RECT rect;
int fHeight=25;
int red=120;
int green=200;
int blue =255;
// static variables can be used to save the state of your window
static bool is_a_rectangle=false;
switch(msg)
{
case WM_PAINT:
// all "drawing" is done here
hDC=BeginPaint(hwnd,&Ps);
// pens draw lines
hPen=CreatePen(PS_SOLID,10,RGB(20,20,200));
SelectObject(hDC,hPen);
if(is_a_rectangle)
{
Rectangle(hDC,100,100,300,200);
}
else
{
Ellipse(hDC,100,100,300,200);
}
DeleteObject(hPen);
// brushes draw areas
hBrush=CreateSolidBrush(RGB(230,45,67));
rect.bottom=320;
rect.top=120;
rect.right=100;
rect.left=300;
FillRect(hDC,&rect,hBrush);
DeleteObject(hBrush);
// fonts draw words
hFont=CreateFont(fHeight,0,0,0,0,TRUE,0,0,0,0,0,0,0,L"Times New Roman");
SelectObject(hDC,hFont);
rect.bottom=555;
rect.top=480;
rect.right=625;
rect.left=65;
DrawText(hDC,L"(c)Copyright 2008, Dr. Thomas Fernandez\n All rights reserved.",-1,&rect,DT_WORDBREAK);
DeleteObject(hFont);
//call your own functions
twoColorLine(hDC,95,75,200,350,50,50,50,205,205,205);
//Uncomment the following block for a fractal drawing.
/*
hBrush = CreateSolidBrush(RGB(red,green,blue));
SelectObject(hDC, hBrush);
rCircle(hDC,233,230,210,25,25,250);
rCircle(hDC,233,230,140,250,25,250);
DeleteObject(hBrush);
*/
EndPaint(hwnd,&Ps);
break;
case WM_CHAR:
if(wParam=='r')
{
is_a_rectangle=true;
}
else if(wParam=='e')
{
is_a_rectangle=false;
}
// this line forces the window to be redrawn
InvalidateRect(hwnd,NULL,true);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}