Thread: my smallest window prog

  1. #1
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218

    my smallest window prog

    I was playing with the code to make it, trimmest window
    programm and i came up with this.

    good for beginners(atleast it creates a window for u)hehe

    it creates a window and it works.

    however if i change anycode in it , even a caption, it will
    not compile a 2nd time.and it will give me error:

    can not open debug.exe.

    now if i go back and recode it as original, it still gives me
    the same error. do you know why?im not exactly sure.

    here is the code:


    Code:
    /*------------------------------------------------------------
       
                     Sia k
      ------------------------------------------------------------*/
    
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
        HWND         hwnd ;
         MSG          msg ;
         WNDCLASSEX     wndclass ;
         wndclass.cbSize        = sizeof(wndclass); 
         wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = TEXT("hello") ;
         wndclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
         RegisterClassEx(&wndclass);
         
           
         hwnd = CreateWindow (TEXT("hello"), TEXT (" Hello"), WS_OVERLAPPEDWINDOW,                 
                CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,                
                 NULL, hInstance,  NULL) ;                    
                                         
                                        
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    { 
    	return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I suspect it can't recompile it because it cannot open the module because the module has not exited. Change your WindProc to process WM_DESTROY, something like this...

    Code:
        switch(message)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    
        default:
            return DefWindowProc(hWnd,
                                 message,
                                 wParam,
                                 lParam);
        }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    Ya , i get you adrian. but the thing is i can recompile the
    code as many times as i want .and it will show the window
    and you can terminate it.

    but as soon as you make any changes like:

    window caption = "hello world"

    and change it to:

    window caption = "helo"

    it wont recompile. and worse if u retype it as original
    it keeps giving you the same error msg.

    i am not sure. is it like a one time object window
    that you cant change its attributes.

    if you get the time can anyone try to recompile this
    code?


    *note; credit to adrians msg .later when i wanted to delete
    the workspace for this program , windows wouldnt let me
    by saying,

    "you cant delete this file while its being used by another
    application"

    eventhough i had closed both the program window and VC++
    window prior to this. and no other program was using it.

  4. #4
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Perhaps the IDE realizes that nothing has changed, doesn't do a full recompile, and simply allows you to run the same executable without modification. However, once you change the contents of the file, the IDE realizes that it must recompile the entire project. It tries to write to the .exe, but fails since the old program is still running. Even if you change the code back to the original, the IDE senses that something has changed, attempts to recompile, and you run into the same error. Handling WM_DESTROY is most likely the solution.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Ya , i get you adrian. but the thing is i can recompile the
    code as many times as i want .and it will show the window
    and you can terminate it.


    If you have not changed anything between the last compilation and now, VC does not recompile it. Why should it, it is the same.

    If I get a minute I'll have a look at it, but I'm pretty sure the problem is what we've already discussed.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    <<If you have not changed anything between the last compilation and now, VC does not recompile it. Why should it, it is the same>>

    Ok that says it all.thx

    -----------------------------------------------------------------------------
    while we are at the subject.
    I have another program in win API->opengl that executes
    fullscreen mode.and has the exact same problem.
    I processed WM_KEYDOWN (VK_ESCAPE) to close the program.

    yet after i terminate the fullscreen window , when i check
    the task manager it will show the program process as still
    running and (to rebuil) VC++ will give me the same error msg.

    "cant open debug.exe"

    i cant think of any reason why. i realy cant.

    do you c the problem?

    Code:
    
    #define WIN32_LEAN_AND_MEAN
    #pragma comment(lib, "opengl32.lib")
    #pragma comment(lib, "glu32.lib")
    #pragma comment(lib, "glaux.lib")
    #pragma comment(linker, "/subsystem:windows")
    
    
    
    
    
    
    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    #include <gl/glaux.h>
    
    float angle = 0.0f;
    
    
    HDC g_HDC;
    BOOL fullScreen = FALSE;
    static char szAppName[] = TEXT ("Hello ") ;
    HINSTANCE hInstance;
    void SetupPixelFormat(HDC hdc)
    {
    int npixelformat;
    
    static PIXELFORMATDESCRIPTOR pfd = {
    	sizeof(PIXELFORMATDESCRIPTOR),
    		1,
    		PFD_DRAW_TO_WINDOW |
    		PFD_SUPPORT_OPENGL |
    		PFD_DOUBLEBUFFER,
    		PFD_TYPE_RGBA,
    		32,
    		0, 0, 0, 0, 0, 0,
    		0,
    		0, 
    		0,
    		0, 0, 0, 0,
    		16,
    		0,
    		0,
    		PFD_MAIN_PLANE,
    		0, 
    		0, 0, 0,};
    	
    	
    	npixelformat = ChoosePixelFormat(hdc, &pfd);
    	SetPixelFormat(hdc, npixelformat, &pfd);
    	}
    LRESULT  CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
    
    
    GLushort StipplePattern = 0xF000;
    GLboolean b;	
    	
    	
        
    	  BOOL  done;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASSEX     wndclass ;
    //--------------------------------
    DWORD	   dwExStyle;				
    	DWORD	   dwStyle;					
    	RECT	   windowRect;
    
    	// temp var's
    	int width = 800;
    	int height = 600;
    	int bits = 32;
    
    	fullScreen = TRUE;
    
    	windowRect.left=(long)0;					
    	windowRect.right=(long)width;		
    	windowRect.top=(long)0;					
    	windowRect.bottom=(long)height;					
    //--------------------------------------------
    
         wndclass.cbSize        = sizeof(WNDCLASSEX); 
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = NULL;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         wndclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
         RegisterClassEx(&wndclass);
    
    //------------------------------------------------------------	
    	if (fullScreen)					
    	{
    		DEVMODE dmScreenSettings;		
    		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
    		dmScreenSettings.dmSize = sizeof(dmScreenSettings);	
    		dmScreenSettings.dmPelsWidth = width;			
    		dmScreenSettings.dmPelsHeight = height;			
    		dmScreenSettings.dmBitsPerPel = bits;	
    		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
    
    		
    		if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
    		{
    			// setting display mode failed, switch to windowed
    			MessageBox(NULL, "Display mode failed", NULL, MB_OK);
    			fullScreen=FALSE;	
    		}
    	}
    
    	if (fullScreen)							
    	{
    		dwExStyle=WS_EX_APPWINDOW;				
    		dwStyle=WS_POPUP;					
    		ShowCursor(FALSE);	
    	}
    	else
    	{
    		dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			
    		dwStyle=WS_OVERLAPPEDWINDOW;					
    	}
    
    	AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);		// Adjust Window To True Requested Size
    //------------------------------------------------------------------------------------------------
    
    hwnd = CreateWindowEx(dwExStyle, 									// extended style
    						  szAppName,							// class name
    						  "hello sara",	                      	// app name
    						  dwStyle | WS_CLIPCHILDREN |
    						  WS_CLIPSIBLINGS,
    						  0, 0,								
    						  windowRect.right - windowRect.left,
    						  windowRect.bottom - windowRect.top, 
    						  NULL,								
    						  NULL,							
    						  hInstance,						
    						  NULL);	
    
    
                  if(!hwnd)
    		 return 0;   
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         SetFocus(hwnd);
         done = FALSE;
    
    	 while(!done)
    	 {
    		 PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE);
    		 if(msg.message == WM_QUIT)
    		 {
    			 done  = TRUE;
    			 break;
    
    		 }
    		 else
    		 {
    
    TranslateMessage(&msg);
    	DispatchMessage(&msg);
    
    
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
    	angle = angle + 0.7f;
    
    	if(angle >= 360.0f)
        angle = 0.0f;
    	glTranslatef(0.0f, 0.0f, -5.0f);
    
        glColor3f(1.0f, 0.0f, 1.0f);
    	
    	glPolygonMode(GL_FRONT, GL_LINE);
        glBegin(GL_TRIANGLES);
    glEdgeFlag(GL_TRUE);
    	glVertex3f(0.0f, 1.0f, 0.0f);
    glEdgeFlag(GL_FALSE);
       glVertex3f(-1.0f, 0.0f, 0.0f);
    glEdgeFlag(GL_TRUE);
       glVertex3f(1.0f, 0.0f, 0.0f);
        
       
    
    			
    	
    	glVertex3f(0.0f, -1.0f, 0.0f);
    glEdgeFlag(GL_FALSE);
        glVertex3f(1.0f, 0.0f, 0.0f);
    glEdgeFlag(GL_TRUE);    
        glVertex3f(-1.0f, 0.0f, 0.0f);
    	
    	glEnd();
    
       
    	SwapBuffers(g_HDC);
    
    	 
    		 }
    	
    	 
    	 }
    
    
         
    		
    		
    	 return  msg.wParam;
    }
    LRESULT  CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HGLRC hRC;
    	static HDC hdc;
    	char  hello[] = "HELLO SARA";
    	int width, height;
       
    
    	switch(message){
    
    	case WM_CREATE:
    
    		hdc = GetDC(hwnd);
    		g_HDC = hdc;
    
    		SetupPixelFormat(hdc);
    
    		hRC = wglCreateContext(hdc);
            wglMakeCurrent(hdc ,hRC);
    
    
    		return 0;
    		break;
    
    	case WM_KEYDOWN:
    		if(wParam == VK_ESCAPE)
    
               SendMessage(hwnd, WM_CLOSE, 0, 0);
    	    return 0;
    		break;
    
    	case WM_SIZE:
    		height = HIWORD (lParam);
    		width  = LOWORD (lParam);
    		if(height == 0)
    		{
    			height = 1;
    		}
    
    		glViewport(0, 0, width, height);
    		glMatrixMode(GL_PROJECTION);
    		glLoadIdentity();
    		gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f);
    		glMatrixMode(GL_MODELVIEW);
    		glLoadIdentity();
    		return 0;
    		break;
         case WM_DESTROY:
        									
    	
    		wglMakeCurrent(hdc, NULL);
    		wglDeleteContext(hRC);
           if (fullScreen)
    	{
    		ChangeDisplaySettings(NULL,0);			
    		ShowCursor(TRUE);						
    	}
    	UnregisterClass(szAppName, hInstance);	
    		PostQuitMessage(0);
    		
    		break;
    	default:
    
             break;
    	}
    	return (DefWindowProc(hwnd, message, wParam, lParam));
    	
    }

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Try adding a return 0; after your call to PostQuitMessage(). You need to be a bit careful the way you have it now. If you don't return from each message handler, you are then passing the message off to the default processor.

    I don't have OpenGL so I can't try compiling this.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    I added "return 0".

    program still running after termination.

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Take PostQuitMessage(0); out of WM_DESTROY and instead handle it inside of WM_CLOSE, use DestroyWindow(hWnd) in WM_DESTROY
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  10. #10
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    Thx.
    but no luck , process still running.

  11. #11
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Are you sure? I copied your code and compiled it with the changes i suggested and the process ended when i closed the program. You sure theres not more code you have?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  12. #12
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    Excellent !

    thx xds4lx.

    i missed processing WM_DESTROY at first try.after your
    2nd thread(push in the butt) i checked back & u r right.

    but how come?

    WM_DESTROY-->WM_QUIT-->postquitMessage(0);
    should have done the job in original code.yet yours works.




    Code:
     case WM_CLOSE:
        									
    	    wglMakeCurrent(hdc, NULL);
    		wglDeleteContext(hRC);
           if (fullScreen)
    	   {
    		ChangeDisplaySettings(NULL,0);			
    		ShowCursor(TRUE);						
    	   }
    	    UnregisterClass(szAppName, hInstance);
    
    			
    		PostQuitMessage(0);
    		return 0;
    		break;
    
    	 case WM_DESTROY:
    		 DestroyWindow(hwnd);
    		 return 0;
    		 break;

  13. #13
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    doesnt PostQuitMessage(0); post a WM_QUIT message in the message queue..which then returns 0 in the message loop which causes to exit out of the message loop...

  14. #14
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    in the messageloop it gets messages..translate the message (keyboard) and dispatchmessage(&msg) which calls windows to call the Window Procedure...only when the while loop doesnt return a 0 value...a WM_QUIT returns 0...so once the WM_QUIT message is received it exits out of the loop...and therefore at the end, ends program execution
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM