#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#define WIN_NAME "Click on Window..."
#define WIN_X 200
#define WIN_Y 200
#define WINPOS_X 20
#define WINPOS_Y 90
LRESULT CALLBACK WindowProc( HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam );
void Draw_Circle( HWND hWnd,int iLeft,int iTop,
int iRight,int iBot );
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow )
{
HWND hwnd;
MSG msg;
WNDCLASSEX winclass;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH) GetStockObject
(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "WIN_SHELL";
winclass.hIconSm = NULL;
if(!RegisterClassEx( &winclass ))
return FALSE;
if(!(hwnd = CreateWindow("WIN_SHELL",WIN_NAME,
WS_VISIBLE | WS_CAPTION |
WS_MINIMIZEBOX | WS_SYSMENU,
WINPOS_X,WINPOS_Y,
WIN_X,WIN_Y,
NULL,
NULL,
hinstance,
NULL)))
return FALSE;
while ( 1 )
{
if( PeekMessage( &msg,NULL,0,0,PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
break;
TranslateMessage( &msg );
DispatchMessage ( &msg );
}
}
return(msg.wParam);
}
LRESULT CALLBACK WindowProc( HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
int iCursorX = 0;
int iCursorY = 0;
switch ( msg )
{
case WM_CREATE:
srand( time( NULL ) );
return 0;
case WM_PAINT:
hdc = BeginPaint( hwnd,&ps);
EndPaint( hwnd,&ps );
return 0;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_LBUTTONDOWN:
iCursorX = (int)LOWORD(lParam);
iCursorY = (int)HIWORD(lParam);
Draw_Circle( hwnd,iCursorX,iCursorY,60,50 );
return 0;
default:
break;
}
return ( DefWindowProc( hwnd, msg, wParam, lParam ) );
}
void Draw_Circle( HWND hWnd,int iLeft,int iTop,
int iRight,int iBot )
{
HDC hdc = GetDC( hWnd );
HBRUSH hBrush = CreateSolidBrush( RGB( rand()%255,
rand()%255,
rand()%255));
SelectObject( hdc,hBrush );
Ellipse( hdc,iLeft,iTop,iRight,iBot );
ReleaseDC( hWnd,hdc );
DeleteObject( hBrush );
}
C\C++ do not contain a standard graphics lib. I am assuming that you are coding with VC++ on the win32 platform. You must usually go though some sort of API that is not apart of the C\C++ standard library to perfrom bitmap manipulations,sprite bltting etc...On the win32 platform there are many graphics API's to choose from i.e. DirectDraw,OpenGL etc...But each of these api's diminish your programs device independence to some extent. Another API is the win32 GDI. This is what you can go through to perfrom any kind of grahical effects in windows. Of course this is not the only api to go through, and it certainly has it's downfalls( it's very very slow...), but it's a starting point. The above code just creates a window with the raw api. The user is able to click on the windows client area. With each click a randomly colored circle is drawn to the client area via GDI. I hope this gets you started in the right direction. Just create a win32 app. in VC++,copy and paste, then build. If you get errors during the build, you may need to change the source files .c extension to .cpp.
Mike L.G.
mlg400@blazemail.com