Advertisement

newbie windows programming help

Started by August 03, 2000 11:44 PM
4 comments, last by OoMMMoO 24 years, 4 months ago
I wrote a program to display radom lines in a window the program runs fine but after I close it windows starts screwing up an I get the blue screen of death a couple of times and I cant run any more of my programs the code is #define WIN32_LEAN_AND_MEAN #include #include #include #include #include #include #include //DEFINES/////////////////////////////// #define WINDOW_CLASS_NAME "WinClass" //GLOBALS/////////////////////////////// HWND main_window_handle= NULL; //FUNCTIONS///////////////////////////// LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) { PAINTSTRUCT ps; HDC hdc; char buffer[80]; static int counter=0; 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; } return(DefWindowProc(hwnd,msg,wparam,lparam)); } //WINMAIN////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow) { WNDCLASS winclass; HWND hwnd; 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_CROSS); winclass.hbrBackground= (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName= NULL; winclass.lpszClassName= WINDOW_CLASS_NAME; //RegisterWindow////////////////////////////////////// if(!RegisterClass(&winclass)) return(0); //CreateWindow//////////////////////////////////////// if(!(hwnd=CreateWindow(WINDOW_CLASS_NAME,"Hello",WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0,0,320,200,NULL,NULL,hinstance,NULL))) return(0); //save the window handle in a global////////////////// 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); } //DRAWLINE///////////////////////////////////////////////// PAINTSTRUCT ps; hdc=GetDC(hwnd); HPEN green_pen=CreatePen(PS_SOLID,0,RGB(0,255,0)); SelectObject(hdc,green_pen); MoveToEx(hdc,(rand()%799)+1,(rand()%599)+1,NULL); LineTo(hdc,(rand()%799)+1,(rand()%599)+1); DeleteObject(green_pen); ReleaseDC(hwnd,hdc); } return(msg.wParam); } can someone help me out?
hmm.... try creating the pen outside of the main loop and deleting it in WM_DESTROY, instead of creating it and deleting it with each iteration

Edited by - Quantum on August 4, 2000 4:42:42 AM
Advertisement
looking over the code again, your window is only 320x200, yet you try to draw lines between 1-800 and 1-600...
make the window 800x600 and it should be fine
Don''t worry. LineTo does clipping. So it doesn''t really matter what coordinates you send it.

I think you should change your drawing function.
    //DRAWLINE/////////////////////////////////////////////////PAINTSTRUCT ps;hdc=GetDC(hwnd);HPEN hOldPen;//need to add thisHPEN green_pen=CreatePen(PS_SOLID,0,RGB(0,255,0));hOldPen = (HPEN)SelectObject(hdc,green_pen); //modified this lineMoveToEx(hdc,(rand()%799)+1,(rand()%599)+1,NULL);LineTo(hdc,(rand()%799)+1,(rand()%599)+1);DeleteObject(green_pen); DeleteObject(hOldPen);ReleaseDC(hwnd,hdc);    


When you select a pen or brush into a HDC, you need to store the old pen/brush that is returned. Don''t ask me why ''cos I don''t know either. If you don''t you program is going to start acting weird after about 3000 or so calls to SelectObject. I''m not sure if this applies to other GDI objects (I''ve only tried Pens & Brushes).

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
i still dont think creating the pen and deleting it with each iteration is a good idea... oh well
Another little change...

    hOldPen = (HPEN)SelectObject(hdc,green_pen);//Do drawing//Then return to the old state. Never delete a selected objectSelectObject(hdc, hOldPen);DeleteObject(green_pen);     





Edited by - Armitage on August 4, 2000 8:01:52 AM
A polar bear is a rectangular bear after a coordinate transform.

This topic is closed to new replies.

Advertisement