Advertisement

problems with squares

Started by August 08, 2000 03:15 PM
1 comment, last by OoMMMoO 24 years, 5 months ago
I made a program to display random squares on the screen and my first problem is they arnt very random can you look at my code and tell me what I did wrong? Also when I put the program in fullscreen mode it will not let me move my mouse arrow the code is #define WIN32_LEAN_AND_MEAN #include #include #include #include #include //GLOBALS////////////////////////////////////// #define WINDOW_CLASS_NAME "WinClass" HWND main_window_handle=NULL; //FUNCTIONS//////////////////////////////////// LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) { PAINTSTRUCT ps; HDC hdc; switch(msg) { case WM_CREATE: { return(0); }break; case WM_PAINT: { hdc=BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps); return(0); }break; case WM_DESTROY: { PostQuitMessage(0); return(0); }break; default:break; } return(DefWindowProc(hwnd,msg,wparam,lparam)); } //WINMAIN///////////////////////////////////////// int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdline) { WNDCLASS winclass; HWND hwnd; PAINTSTRUCT ps; MSG msg; HDC hdc; 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(BLACK_BRUSH); winclass.lpszMenuName= NULL; winclass.lpszClassName= WINDOW_CLASS_NAME; if(!RegisterClass(&winclass)) return(0); if(!(hwnd=CreateWindow(WINDOW_CLASS_NAME,"Squares",WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0,0,300,200,NULL,NULL,hinstance,NULL))) return(0); main_window_handle=hwnd; srand((unsigned)time(NULL)); while(1) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message==WM_QUIT)break; TranslateMessage(&msg); DispatchMessage(&msg); } //Draw Random Squares/////////////////////////////////// hdc=GetDC(hwnd); HBRUSH randbrush= CreateSolidBrush(RGB((rand()%255)+1,(rand()%255)+1,(rand()%255)+1)); HBRUSH oldbrush= SelectObject(hdc,randbrush); int left=(rand()%799)+1; int right=(rand()%(800-left))+left; int top=(rand()%599)+1; int bottom=(rand()%(600-top))+1; static RECT rect={left,top,right,bottom}; FillRect(hdc,▭,randbrush); SelectObject(hdc,oldbrush); DeleteObject(randbrush); ReleaseDC(hwnd,hdc); } return(msg.wParam); } Please help
What do you mean exactly when you say not very random? They appear at exactly the same place each time (i.e. the sequence of rects are the same or what?)

p.s. I think that you should use the source tags to make your source more readable

[ source ]
//code here
[ /source ]

Get rid of the spaces between the [ & ].
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
looks like you forgot to seed the random number generator
put
srand((unsigned)time(0));
at the top of WinMain(dont forget to include time.h)

as for the cursor thing...
hmmm...........

just try putting ShowCursor(FALSE); in WM_CREATE and ShowCursor(TRUE); in WM_DESTROY to hide and show the cursor.. if thats what you want

This topic is closed to new replies.

Advertisement