Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Draw circle on screen and have color change?

Status
Not open for further replies.

jonnie75

Technical User
Nov 9, 2001
6
US
What would I need to do to place several circles on the screen that would change color (red to green)? Can graphics like this be easily created in C? How about the use of a mouse?
 
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <time.h>
#include <stdlib.h>

#define WIN_NAME &quot;Click on Window...&quot;
#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 = &quot;WIN_SHELL&quot;;
winclass.hIconSm = NULL;


if(!RegisterClassEx( &winclass ))
return FALSE;

if(!(hwnd = CreateWindow(&quot;WIN_SHELL&quot;,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
 
In case u r using borland c/c++ (any version), still u can do the same. I would suggest u go thru the book Programming in Turbo c/c++ by Robert Lafore. This book covers the basic requirements for graphics programming.

There may be other books which may also cover this topic, only i am not aware of.


icici
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top