Thread: Window closes when edit is made

  1. #1
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53

    Window closes when edit is made

    I'm making a little text editor. So far I have it with a menu and the edit box. When I comment out the edit box the program works fine, but when the edit code is included it tries closing it right after it opens and if you reply no to the close question another pops up. Any ideas?

    Here is the code. I can add resource.h and menu.rc if anyone needs to see them.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "resource.h"
    
    #define EDIT 1
    
    LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
    char szClassName[] = "FreeEdit";
    HINSTANCE hThisInstance;
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) {
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
    HMENU menu;
    
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_HREDRAW | CS_VREDRAW;
    wincl.cbSize = sizeof(WNDCLASSEX);
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
    if(!RegisterClassEx(&wincl)) {
       MessageBox (NULL, "There was an error starting FreePad :(" , "Error!", 0 + MB_ICONHAND + MB_SETFOREGROUND);
       return 0;
    }
    
    hwnd = CreateWindowEx(
       0,
       szClassName,
       "FreePad 00.00",
       WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT,
       CW_USEDEFAULT,
       CW_USEDEFAULT,
       CW_USEDEFAULT,
       HWND_DESKTOP,
       NULL,
       hThisInstance,
       NULL
    );
    
    
    ShowWindow(hwnd, nFunsterStil);
    
    menu = LoadMenu(hThisInstance, MAKEINTRESOURCE(ID_MENU));
    SetMenu(hwnd, menu);
    
    
    while(GetMessage(&messages, NULL, 0, 0)) {
       DispatchMessage(&messages);
    }
    
    return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HDC hdc;
    TEXTMETRIC tm;
    static HWND hwndEdit;
    
    switch (message) {
    
       case WM_CREATE:
    
    	  hwndEdit = CreateWindow(TEXT ("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0,0,0,0, hwnd, (HMENU) EDIT, ((LPCREATESTRUCT) lParam)->hInstance, NULL);
    
       return 0;
    
       case WM_SETFOCUS:
    		SetFocus(hwndEdit);
    		return 0;
    
       case WM_SIZE:
    		MoveWindow (hwndEdit, 0,0, LOWORD (lParam), HIWORD (lParam), TRUE);
    		return 0;
    
       return 0;
    
       case WM_COMMAND:
    
       switch(wParam) {
      
          case MENU_FILE_EXIT:
             SendMessage( hwnd, WM_CLOSE, 0, 0L );
          return 0;
    
          case MENU_HELP_ABOUT:
             MessageBox (NULL, "FreePad 00.00\nCopyright (C) 2003 Eric Marcarelli" , "About FreePad", 0 + MB_SETFOREGROUND);
          return 0;
       }
    
       case WM_CLOSE:
          if (IDYES == MessageBox (NULL, "Unsaved work will be lost. Close anyway?" , "Close?", 0 + MB_YESNO + MB_ICONQUESTION + MB_SETFOREGROUND))
                DestroyWindow( hwnd );
       return 0;
    
       case WM_QUERYENDSESSION:
          if (IDYES == MessageBox (NULL, "Unsaved work will be lost. Close anyway?" , "Close?", 0 + MB_YESNO + MB_ICONQUESTION + MB_SETFOREGROUND))
             return 1;
          else
             return 0;
    
       case WM_DESTROY:
          PostQuitMessage (0);
       return 0;
    
       break;
    
       default:
          return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
    }

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Not really sure, but ...

    I am not sure what the problem is, but I noticed something. I always thought when you pass parameters, you are supposed to '|' them, instead of '+' them. I am talking about in your MessageBox() functions. If it works, then there's nothing wrong there. I just wanted to point that out. Also, in your message loop, i think you need to add "TranslateMessage(&messages);" before you call "DispatchMessage(&messages);". I don't know if that will solve the problem, but you can try it.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    When I used the Dev-C++ inset MessageBox() it used + so I just kept it. I changed them all to |'s and it didn't change anything. Adding TranslateMessage(&messages); didn't help either. I'm pretty sure the code all works since I cut it out of my last project... Except the edit, that's new.

    The edit comes from an example in a book, when I compile the example it works...

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    case WM_COMMAND:
    
       switch(wParam) {
    This should be:
    Code:
    case WM_COMMAND:
       switch(LOWORD(wParam)) {
    Your actual problem is because there is no break or return at the end of the WM_COMMAND handler. Many WM_COMMAND messages are sent in the course of a program's life, and you are either handling MENU_FILE_EXIT, MENU_HELP_about, or letting code flow right on to WM_CLOSE. Add this:
    Code:
    case WM_COMMAND:
    
       switch(wParam) 
       {
       case MENU_FILE_EXIT:
             SendMessage( hwnd, WM_CLOSE, 0, 0L );
             return 0;
    
       case MENU_HELP_about :
             MessageBox (NULL, "FreePad 00.00\nCopyright (C) 2003 Eric Marcarelli" , "About FreePad", 0 + MB_SETFOREGROUND);
              return 0;
         default:
              break;
       }
       break;
    This will not remove all your errors. You also need to return DefWindowProc() for all the normal messages, such as WM_CREATE. Instead of returning 0, use 'break', and at the end, return DefWindowProc().

    Here is a corrected window procedure. Basically, DefWindowProc() is called outside the switch. If something needs to call it, like WM_CREATE or WM_SIZE, they just break, and it is called.

    Code:
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
    {
    	HDC hdc;
    	TEXTMETRIC tm;
    	static HWND hwndEdit;
    
    	switch (message) 
    	{
    	case WM_CREATE:
    		hwndEdit = CreateWindow(TEXT ("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0,0,0,0, hwnd, (HMENU) EDIT, ((LPCREATESTRUCT) lParam)->hInstance, NULL);
    
    		break;
    
    	case WM_SETFOCUS:
    		SetFocus(hwndEdit);
    		break;
    
    	case WM_SIZE:
    		MoveWindow (hwndEdit, 0,0, LOWORD (lParam), HIWORD (lParam), TRUE);
    		break;
    
    	case WM_COMMAND:
    		switch(LOWORD(wParam)) 
    		{
      		case MENU_FILE_EXIT:
    			SendMessage( hwnd, WM_CLOSE, 0, 0L );
    			return 0;
    
    		case MENU_HELP_about:
    			MessageBox (NULL, "FreePad 00.00\nCopyright (C) 2003 Eric Marcarelli" , "About FreePad", 0 + MB_SETFOREGROUND);
    			return 0;
    		default:
    			break;
       		}
    		break;
    
    	case WM_CLOSE:
    		if (IDYES == MessageBox (NULL, "Unsaved work will be lost. Close anyway?" , "Close?", 0 + MB_YESNO + MB_ICONQUESTION + MB_SETFOREGROUND))
    		break; //WM_DESTROY is automatically called by DefWindowProc()
    	case WM_QUERYENDSESSION:
    		if (IDYES == MessageBox (NULL, "Unsaved work will be lost. Close anyway?" , "Close?", 0 + MB_YESNO + MB_ICONQUESTION + MB_SETFOREGROUND))
    	        	return 1;
    		else
    			return 0;
    	case WM_DESTROY:
    		PostQuitMessage (0);
    		return 0;
    	default:
    		break;
    	}
    
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I always thought when you pass parameters, you are supposed to '|' them, instead of '+' them. I am talking about in your MessageBox() functions.
    You should use the bit operator '|'. What it does is ensure a particular bit is turned on, no matter if it already was or not. Using the addition operator '+' will in some circumstances completely bugger up every flag you set.

    If it works, then there's nothing wrong there.
    That logic will only hold up in simple situations. If you retrieve a window style, then add flags to it, using '+' may have negative effects on your application, as some of the flags may already be set.

    i think you need to add "TranslateMessage(&messages);" before you call "DispatchMessage(&messages);".
    You need to do this if you want WM_CHAR messages to be produced, and yes, it must come before DispatchMessage().
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Adding the break fixed it, thanks!

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Did you remember to make WM_CREATE, WM_SIZE etcetra return DefWindowProc()?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Yep.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL Window Closes...
    By FlyingIsFun1217 in forum Game Programming
    Replies: 11
    Last Post: 11-22-2007, 11:41 AM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 12:31 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM