Thread: Dialog Buttons without resource files?

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Dialog button& .rc file problem

    Hello,

    I'm trying to create a program that creates a dialog when you click on a certain menu option.
    Now, the problem is my compiler doesn't work well with resource files.

    So, is it possible to create a dialog from within the program, without haveing to create the template in a resource file? I know it's possible to do it with menus, but not sure if you can do it with dialogs.

    If it is possilbe could someone show me an example of what to put when using it?

    EDIT:
    Ok I got this problem solved except one minor thing, I keep getting a parse error on line three of the following .rc file:
    Code:
    IDD_ABOUT DIALOG DISCARDABLE  0, 0, 239, 66 
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    FONT 8, "MS Sans Serif" 
    CAPTION "ORANGE!!!!"
    BEGIN 
        DEFPUSHBUTTON   "OK",IDOK,174,18,50,14 
        PUSHBUTTON      "Cancel",IDCANCEL,174,35,50,14 
        GROUPBOX        "About this program...",IDC_STATIC,7,7,225,52 
        CTEXT           "An example program showing how to use Dialog Boxes\r\n\r\nby theForger", 
                        IDC_STATIC,16,18,144,33 
    END
    No matter wich line is line three there is a parse error. Any ideas what it could be?
    Last edited by 7smurfs; 07-18-2004 at 12:44 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>So, is it possible to create a dialog from within the program, without haveing to create the template in a resource file?<<

    Yes, but it's a major pain. Use memory templates and create the dialog with either CreateDialogIndirectParam or CreateDialogIndirect (which just calls CreateDialogIndirectParam anyway).

    Still, glad to see you're persevering with windres.

    >>Any ideas what it could be?<<

    When using windres (mingw windows resource compiler) always make sure your resource script has something like the following at the top:
    Code:
    #include <windows.h>
    #include "resource.h" /*resource header contains defines for YOUR controls*/
    #if !defined IDC_STATIC
    #define IDC_STATIC -1
    #endif
    [edit]fixed typo[/edit]
    Last edited by Ken Fitlike; 07-18-2004 at 07:31 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Ok that helped for that error but now I get a parse error on the following:
    Code:
    GROUPBOX        "About this program...",IDC_STATIC,7,7,225,52

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Sorry, my fault - a typo in the above. IDCSTATIC -1 should be IDC_STATIC -1. I've fixed it in the original post.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Thanks it works great now =)

  6. #6
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Ok now that I have it compileing, i have *yet* another problem =(
    The dialog doesnt want to show up (I get a -1 return value), here is the code:
    Main.cpp:
    Code:
    #include <windows.h>
    #include "resource.h"
    const char g_szClassName[] = "myWindowClass";
    
    BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_INITDIALOG:
    
            return TRUE;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDOK:
                        EndDialog(hwnd, IDOK);
                    break;
                    case IDCANCEL:
                        EndDialog(hwnd, IDCANCEL);
                    break;
                }
            break;
            default:
                return FALSE;
        }
        return TRUE;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
              case WM_CREATE:
              {
                 HMENU hMenu, hSubMenu;
                 HICON hIcon, hIconsm;
                 hMenu = CreateMenu();
                 hSubMenu = CreatePopupMenu();
                 AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&XIT");
                 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
                 
                 hSubMenu = CreatePopupMenu();
                 AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About...");
                 AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
                 SetMenu(hwnd, hMenu);
                 }
                 break;
            case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case ID_FILE_EXIT:
    					PostMessage(hwnd, WM_CLOSE, 0, 0);
    				break;
            case ID_HELP_ABOUT:
            {
                int ret = DialogBox(GetModuleHandle(NULL), 
                    MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
                if(ret == IDOK){
                    MessageBox(hwnd, "Dialog exited with IDOK.", "Notice",
                        MB_OK | MB_ICONINFORMATION);
                }
                else if(ret == IDCANCEL){
                    MessageBox(hwnd, "Dialog exited with IDCANCEL.", "Notice",
                        MB_OK | MB_ICONINFORMATION);
                }
                else if(ret == -1){
                    MessageBox(hwnd, "Dialog failed!", "Error",
                        MB_OK | MB_ICONINFORMATION);
                }
            }
            break;
    
             } 
          break; 
          case WM_CLOSE: 
             DestroyWindow(hwnd); 
          break; 
          case WM_DESTROY: 
             PostQuitMessage(0); 
          break; 
          default: 
             return DefWindowProc(hwnd, Message, wParam, lParam); 
       } 
       return 0; 
    } 
    
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG Msg;
    
    	wc.cbSize		 = sizeof(WNDCLASSEX);
    	wc.style		 = 0;
    	wc.lpfnWndProc	 = WndProc;
    	wc.cbClsExtra	 = 0;
    	wc.cbWndExtra	 = 0;
    	wc.hInstance	 = hInstance;
    	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	hwnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
    		g_szClassName,
    		"The title of my window",
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
    		NULL, NULL, hInstance, NULL);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }
    Resource.h:
    Code:
    #define ID_FILE_EXIT 9001
    #define ID_HELP_ABOUT 9002
    #define IDD_ABOUT 101
    Resource.rc:
    Code:
    #include <windows.h>/*resource header contains defines for YOUR controls*/
    #include "resource.h"
    #if !defined IDC_STATIC
    #define IDC_STATIC -1
    #endif
    IDD_ABOUT DIALOG DISCARDABLE  0, 0, 239, 66
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "ORANGE!!!!"
    FONT 8, "MS Sans Serif" 
    BEGIN 
        DEFPUSHBUTTON   "OK",IDOK,174,18,50,14 
        PUSHBUTTON      "Cancel",IDCANCEL,174,35,50,14 
        GROUPBOX        "About this program...", IDC_STATIC, 7, 7, 225, 52 
        CTEXT           "An example program showing how to use Dialog Boxes\r\n\r\nby theForger", 
                        IDC_STATIC,16,18,144,33 
    END
    Is there something I can't see thats would result in an error? Sorry for the constant questions but I just started this yesterday and am getting a bit confused with these tuturials.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  3. Resource Files
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 09-10-2001, 09:20 AM
  4. Problems with resource files
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 08-31-2001, 08:45 AM