So far no one has been able to tell me why this code is not working, or what I need to do to THIS code to make it work.
Can someone simply show using MY code below where the problem is, and why it's not working properly?

Problem: I am following the tutorial at www.winprog.org/tutorial for windows API programming. I am using Dev-C++ for my windows compiler. What I am after and is not happening in my program is when I click on the "HELP", and then "ABOUT" an "ABOUT" dialog is supposed to appear, but it is not! I have checked and found other people running into my same problem using the tutorial at www.winprog.org/tutorial , but no answer to what needs to be done to fix the issue and make the ABOUT dialog appear with the code I'm about to paste!

#include <windows.h>


#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define IDD_ABOUT 9003
#define IDD_THIS 9004
#define IDC_STATIC -1
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;
             }

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        
    switch(msg)
    {
               case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;

            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_STUFF_GO, "&Go");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
            
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, IDD_ABOUT, "&ABOUT");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&HELP");

            SetMenu(hwnd, hMenu);

//THis code below was commented out due to no icon being used.
            /*hIcon = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
            if(hIcon)
                SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
            else
                MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);

            hIconSm = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
            if(hIconSm)
                SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
            else
                MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
        */
        }
        
        break;
        
        
        case WM_COMMAND:
            
            
         switch(LOWORD(wParam))
    {
        case IDD_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;

                     
            
                case ID_FILE_EXIT:
                     
                      PostQuitMessage(0);
                break;
                case ID_STUFF_GO:
                     
                   system("explorer c:\\");

                break;
            }
        break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
            
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    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;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "OEBEL STUDIOS",
        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);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Below is the .rc resource file for the dialog box that is not appearing.

// by: Cody Oebel
// San Antonion, Tx
// ID_ABOUT dialog resource script for my program
// FileName: "diag1.rc"
IDD_ABOUT DIALOG DISCARDABLE  0, 0, 239, 66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My About Box"
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 Cody",
                    IDC_STATIC,16,18,144,33
END

Recommended Answers

All 24 Replies

When you get into problems with resources like this, just start over, it saves yourself trouble in the long run. Also, use a resource Viewer/Editor if possible, it prevents these sort of problems.

When you get into problems with resources like this, just start over, it saves yourself trouble in the long run. Also, use a resource Viewer/Editor if possible, it prevents these sort of problems.

The problem is the fact that the tutorial at www.winprog.org/tutorial states this should work! I really like this tutorial, and in my opinion a programmer should be able to raw code the resources into the program. This tutorial shows the exact code I displayed, but it's not working.. WHY ?
I really just want to know what in the code I have would need to be changed for my "ABOUT" window to appear. Thats it!
I just want to know what is wrong here by using RAW code, and no editor to build the resource script for me. Ive re-built this damn project from scratch more times than I can stand and it runs into the same problem each time.

you've forgotten to include "resource.h"..
use the resource editor to see the ID of the "About Dialog" ..
If you can not find it..then create..
then see the following codes..

// your code
case IDD_ABOUT:
        {
            int ret = DialogBox(GetModuleHandle(NULL), 
                MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
// my code..and it worked..
case IDD_ABOUT:
        {
            int ret = DialogBox(GetModuleHandle(NULL), 
                MAKEINTRESOURCE(IDD_DIALOGABOUT), hwnd, AboutDlgProc);

you know what that means..
and why..

or you should change the dialog ID in the *rc file..

[B]IDD_DIALOGABOUT[/B] DIALOG DISCARDABLE  0, 0, 239, 66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My About Box"
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 Cody",IDC_STATIC,16,18,144,33
END

and

// resource.h
#define IDD_DIALOGABOUT    10000

and..

case IDD_ABOUT:
        {
            int ret = DialogBox(GetModuleHandle(NULL), 
                MAKEINTRESOURCE(IDD_DIALOGABOUT), hwnd, AboutDlgProc);

you've forgotten to include "resource.h"..
use the resource editor to see the ID of the "About Dialog" ..
If you can not find it..then create..
then see the following codes..

// your code
case IDD_ABOUT:
        {
            int ret = DialogBox(GetModuleHandle(NULL), 
                MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
// my code..and it worked..
case IDD_ABOUT:
        {
            int ret = DialogBox(GetModuleHandle(NULL), 
                MAKEINTRESOURCE(IDD_DIALOGABOUT), hwnd, AboutDlgProc);

you know what that means..
and why..

I changed the code completely and changing the identifier name made no difference. Same outcome!

I wonder if it's a compiler issue or something I dont know because it's not working and Ive done it over and over till I'm just stuck in the wall with it.

you've forgotten to include "resource.h"..

I'm pretty confident that's not how it works, the preprocessor just replaces #include "resource.h" with the content of the file anyway. I think it's something to do with your resource file code, it seems quite different to what's generated by the Resource Editor.

what i did..
1. create an empty win32 proj..
2. add a new *.cpp file named "Main.cpp"
3. add a new *.rc file named "Main.rc"

here's my Main.cpp

#include <windows.h>
#include "resource.h"

#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define IDD_ABOUT 9003
#define IDD_THIS 9004
#define IDC_STATIC -1
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;
             }

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        
    switch(msg)
    {
               case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;

            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_STUFF_GO, "&Go");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
            
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, IDD_ABOUT, "&ABOUT");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&HELP");

            SetMenu(hwnd, hMenu);

//THis code below was commented out due to no icon being used.
            /*hIcon = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
            if(hIcon)
                SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
            else
                MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);

            hIconSm = LoadImage(NULL, "menu_two.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
            if(hIconSm)
                SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
            else
                MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
        */
        }
        
        break;
        
        
        case WM_COMMAND:
            
            
         switch(LOWORD(wParam))
    {
        case IDD_ABOUT:
        {
            int ret = DialogBox(GetModuleHandle(NULL), 
                MAKEINTRESOURCE(IDD_DIALOGABOUT), 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;

                     
            
                case ID_FILE_EXIT:
                     
                      PostQuitMessage(0);
                break;
                case ID_STUFF_GO:
                     
                   system("explorer c:\\");

                break;
            }
        break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
            
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    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;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "OEBEL STUDIOS",
        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);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

here's my Main.rc

#include "resource.h"

IDD_DIALOGABOUT DIALOG DISCARDABLE  0, 0, 239, 66
STYLE 0x80L | 0x80000000L | 0x00C00000L | 0x00080000L
CAPTION "My About Box"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "&OK",1,174,18,50,14
    PUSHBUTTON      "&Cancel",2,174,35,50,14
    GROUPBOX        "About this program...",-1,7,7,225,52
    CTEXT           "An example program showing how to use Dialog Boxes\r\n\r\nby Cody",
                    -1,16,18,144,33
END

and here's my resource.h

#define IDD_DIALOGABOUT 10000

it worked i'm telling you..:)

It has to be this compiler being buggy or something.
I used your code, started fresh with a whole new project, and I still get the same exact error message.

When I click on Help -> About -> I get the error message instead of the dialog.

I am going to download a different version of dev-c++ I am using the beta 5.

It has to be this compiler being buggy or something.

Just because it doesn't work, doesn't mean the compiler has a bug. I changed it, and it worked. I just used the resource code from another project which had a auto-generated resource file.

There's one easy fix for all this, just don't do it manually :icon_lol:

I'll take a shot at it later when I have time Cody.

It seems to me you are letting yourself get overly hung up on these resource compiler/dialog issues. I'd move on to other stuff. Personally, I dislike everything to do with dialogs. I make very minimal use of them in my programs. There isn't hardly anything in Win32 programming that can't be done some other way than by using resource scripts and .rc files.

Hi Cody!

I just tried the code posted by cikara21 and it worked perfectly including showing the 'About' dialog box. Somehow when I copied the code a few html tags showed up after the case about and I had to remove those. I used my Dev-C++ setup - 4.9.9.2 or something like that. Not sure if that's the newest or not. Is there a 5?

Dev-C++ is OK except the codetips infuriate me they are so intrusive. Maybe try CodeBlocks. I like that a lot. I also have VS2008 Pro but it cost me about $550. Like anything having to do with .NET its clunky and slow. It does produce generally small executables though. I believe CodeBlocks is only about a 20MB download. I'd guess that's doable from a dial up or otherwise slow connection.

Just because it doesn't work, doesn't mean the compiler has a bug. I changed it, and it worked. I just used the resource code from another project which had a auto-generated resource file.

There's one easy fix for all this, just don't do it manually :icon_lol:

Once more please suggest an easy to use resource editor and I will go that route, and will inform you of the results once I try the resource file compiled by a resource editor, and IF it works I will mark as resolved to the first person who suggested the resource editor!

You could just use the free Microsoft Visual C++ Compiler / IDE, it comes with everything together. [link]

I'm inclined to think Cody might have a point about there being something wrong either with his Dev-Cpp install or something of that sort. He is continually having problems getting his programs to compile if they have resource scripts in them. And these same exact files are compiling with no problem for many of us here who try them- even when we use the Dev-C++ IDE.

For these reasons I'd be a bit surprised if a separate resource editor would make a difference. After all, all they do is auto-generate hopefully valid resource scripts, and the ones Cody already has ARE already valid resource scripts.

I've seen lots of anomolous behavior with the Dev-C++ setup already - especially regarding resource scripts.

Does anyone know the exact command line switches for compiling Cody's program from the command line using the gcc compiler installed with Dev-C++? I do command line compiling a pretty lot with MS compilers, and with gcc under Linux, but I've never done it with Windows gcc programs. If Cody would try that it might give a hint as to what his problems are.

I reworked your code Cody to do without the resource script. Now there is only one file - Main.cpp. I did make quite a few changes; you should be able to recognize it as your program though; and this should work. Hope it helps.

#include <windows.h>
#define IDC_STATIC          -1
#define ID_FILE_OPEN      1500
#define ID_FILE_SAVE      1505
#define ID_FILE_EXIT      1510
#define ID_OPTIONS        1600
#define IDD_ABOUT         1700
#define IDD_DIALOGABOUT   1700
#define IDD_GROUP         1705
#define IDD_OK            1710
#define IDD_CANCEL        1715


long __stdcall DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)
 {
    case WM_CREATE:
      EnableWindow(GetParent(hwnd),FALSE);  //To make the popup dialog modal
      return 0;
    case WM_COMMAND:
      switch(LOWORD(wParam))
      {
         case IDD_OK:
           MessageBox(hwnd,"You Clicked The OK Button!","OK Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
         case IDD_CANCEL:
           MessageBox(hwnd,"You Clicked The Cancel Button!","Cancel Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
      }
      return 0;
    case WM_CLOSE:
      EnableWindow(GetParent(hwnd),TRUE);   //To re-enable main window
      DestroyWindow(hwnd);
      return 0;
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


long __stdcall WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 HINSTANCE hIns;

 switch(msg)
 {
    case WM_CREATE:
    {
      char szClassName[]="Dialog";
      HMENU hMenu, hSubMenu;
      WNDCLASSEX wc;

      hIns=((LPCREATESTRUCT)lParam)->hInstance;
      hMenu = CreateMenu();
      hSubMenu = CreatePopupMenu();
      AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_OPEN,   "&Open");
      AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_SAVE,   "&Save");
      AppendMenu(hSubMenu,  MF_SEPARATOR,         0,              0      );
      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_OPTIONS,     "Explorer");
      AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Options");
      hSubMenu = CreatePopupMenu();
      AppendMenu(hSubMenu,  MF_STRING,            IDD_ABOUT,      "&About");
      AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help" );
      SetMenu(hwnd, hMenu);

      //Register Window Class For Dialog Box
      wc.lpszClassName=szClassName,             wc.lpfnWndProc=DlgProc;
      wc.cbSize=sizeof(wc),                     wc.style=CS_HREDRAW | CS_VREDRAW;
      wc.cbClsExtra=0,                          wc.cbWndExtra=0;
      wc.hInstance=hIns,                        wc.hCursor=LoadCursor(NULL, IDC_ARROW);
      wc.hbrBackground=(HBRUSH)LTGRAY_BRUSH,    wc.lpszMenuName=NULL;
      wc.hIcon=0;                               wc.hIconSm=0;
      RegisterClassEx(&wc);

      return 0;
    }
    case WM_COMMAND:
      switch(LOWORD(wParam))
      {
         case ID_FILE_OPEN:
         {
           MessageBox(hwnd,"You Want To Open A File!","File Open!",MB_OK);
           return 0;
         }
         case ID_FILE_SAVE:
         {
           MessageBox(hwnd,"You Want To Save A File!","File Save!",MB_OK);
           return 0;
         }
         case IDD_ABOUT:
         {
           DWORD dwExStyle,dwStyle;
           HWND hDlg,hCtl,hGroup;
           MSG Msg;

           dwExStyle=WS_EX_DLGMODALFRAME | WS_EX_CONTROLPARENT;
           dwStyle=WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE;
           hIns=GetModuleHandle(0);
           hDlg=CreateWindowEx(dwExStyle,"Dialog","Dialog Window",dwStyle,175,200,400,137,hwnd,0,hIns,NULL);
           hGroup=CreateWindow("button","About This Program",BS_GROUPBOX|WS_CHILD|WS_VISIBLE,5,5,385,100,hDlg,(HMENU)IDD_GROUP,hIns,0);
           hCtl=CreateWindow("button","OK",WS_CHILD|WS_VISIBLE|WS_TABSTOP,295,25,80,25,hDlg,(HMENU)IDD_OK,hIns,0);
           hCtl=CreateWindow("button","Cancel",WS_CHILD|WS_VISIBLE|WS_TABSTOP,295,65,80,25,hDlg,(HMENU)IDD_CANCEL,hIns,0);
           hCtl=CreateWindow("static","An example program showing how to",WS_CHILD|WS_VISIBLE,25,25,275,25,hGroup,(HMENU)-1,hIns,0);
           hCtl=CreateWindow("static","              use Dialog Boxes",WS_CHILD|WS_VISIBLE,25,42,275,25,hGroup,(HMENU)-1,hIns,0);
           hCtl=CreateWindow("static","                     by Cody",WS_CHILD|WS_VISIBLE,25,72,275,25,hGroup,(HMENU)-1,hIns,0);
           while(GetMessage(&Msg, NULL, 0, 0))
           {
              if(!IsDialogMessage(hDlg,&Msg))
              {
                 TranslateMessage(&Msg);
                 DispatchMessage(&Msg);
              }
           }
           return 0;
         }
         case ID_OPTIONS:
           system("explorer c:\\");
           return 0;
         case ID_FILE_EXIT:
           SendMessage(hwnd,WM_CLOSE,0,0);
           return 0;
      }
      break;
    case WM_CLOSE:
      DestroyWindow(hwnd);
      PostQuitMessage(0);
      return 0;
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 char szClassName[]="Cody";
 WNDCLASSEX wc;
 HWND hWnd;
 MSG Msg;

 wc.lpszClassName=szClassName,           wc.lpfnWndProc=WndProc;;
 wc.cbSize=sizeof(WNDCLASSEX),           wc.style=0;
 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.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(WS_EX_CLIENTEDGE,szClassName,"OEBEL STUDIOS",WS_OVERLAPPEDWINDOW,150,150,240,120,NULL,NULL,hInstance,NULL);
 ShowWindow(hWnd,nCmdShow);
 while(GetMessage(&Msg, NULL, 0, 0))
 {
    if(!IsDialogMessage(hWnd,&Msg))
    {
       TranslateMessage(&Msg);
       DispatchMessage(&Msg);
    }
 }

 return Msg.wParam;
}

Just discovered there is a background color problem in the dialog. It didn't show up in Win 2000 which I developed the program on. Saw it in XP. I'll see if I can fix it tomorrow.

Just discovered there is a background color problem in the dialog. It didn't show up in Win 2000 which I developed the program on. Saw it in XP. I'll see if I can fix it tomorrow.

OMG ... it worked!!!

I downloaded LCC and started trying to understand using it, but what I did was used it's resource editor as people have been suggesting, and just copied over the resource file, header and made some small edits on my main program .. and I still ran into the same exact problem.
I just compiled your code.. WORKS LIKE A CHARM MAN :)
Now I am going to study your code for a while to better understand it and use what you did to get past the problem I have been runnin g into with these damn resource files. I have been using the 4.9.9.2 DEV - C++.
Thanks frederick... I havent ever tried compiling through command line but once I got time I will figure out how to and compile the project I was having problems with and get back to you with whats stated. Otherwise your code works how it should have been all along. I can finallllllly move onto the next section.. I do kind of get stubborn and will not move forward in any tutorial until I complete each step and have some sort of understanding LOL YOU dont know how freegin happy I am to see this actually work for once!
Is there any way I can tag you as who solved this problem for me. All I wanted was someone to edit my code like you did so I could see all in one huge top-down style what needed to be done. I'm not the most suavy programmer like you gentlemen, or nearly as experienced, but this solution given to me I want known as SUPERB!!!!


Frederick2 = SOLVED THIS PROBLEM

Hi Cody!

I'm really glad to hear it worked for you! I put some time into it because I've been thinking about eliminating resource scripts from my code to the extent possible; simply because I don't like them for some reason. I can't say I've ever had problems with them to the extent that you seemed to be having though.

One thought did occur to me though that I hadn't mentioned, and that was the issue of where you installed Dev-C++ to? I remember from years ago over at the Bloodshed Software Forum for Dev-C++ that they highly recommended that it be installed to C:\Dev-Cpp rather than to the more likely place of C:\Program Files\Dev-Cpp. Folks who installed it to file paths with spaces in them had the sorts of problems you were having, if I remember correctly. So I always followed their advice, even though I don't like to have my root C drive cluttered up with lots of directories.

Aren't the background colors messed up for you on the Dialog? I worked on the program in Win 2000, and posted it from that work. Then I tried it on XP and the labels were a bit messed up. I was going to work on it today. I've seen the problem before.

I'm just like you when it comes to working through computer or math stuff sequentially. The problem would have driven me nuts too! Take care.

Fred

Just thought I'd make a comment on the technical nature of what I did there. Its kind of fundamental. I completely removed the Windows Dialog Engine from the picture. The Windows Dialog Engine was created as kind of a wrapper on the basic RegesterClass() and CreateWindow() functionality within Windows. The intent was to simplify things for simple windows that are a little bit more complicated than Message Boxes, but not requiring the full functionality of custom made/designed Window Classes.

What I did was create a regular Window Class for the Dialog with the typical RegisterClassEx(), CreateWindow() sequence, and therefore I have an ordinary Window Procedure in the program to service the Window - not a Dialog Procedure which uses somewhat different messages. What I did was use the class styles typical of Dialog Boxes to accomplish this. Also note the EnableWindow() function was used to disable the main program window while the Dialog Box was active. This is typical of how modal dialog boxes work. They won't allow you to interact with other Windows until you dismiss them.

The background color of the dialog window needs to be changed to COLOR_BTNSHADOW in the WNDCLASSEX struct...

#include <windows.h>
#define IDC_STATIC          -1
#define ID_FILE_OPEN      1500
#define ID_FILE_SAVE      1505
#define ID_FILE_EXIT      1510
#define ID_OPTIONS        1600
#define IDD_ABOUT         1700
#define IDD_DIALOGABOUT   1700
#define IDD_GROUP         1705
#define IDD_OK            1710
#define IDD_CANCEL        1715


long __stdcall DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)
 {
    case WM_CREATE:
      EnableWindow(GetParent(hwnd),FALSE);  //To make the popup dialog/window modal
      return 0;
    case WM_COMMAND:
      switch(LOWORD(wParam))
      {
         case IDD_OK:
           MessageBox(hwnd,"You Clicked The OK Button!","OK Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
         case IDD_CANCEL:
           MessageBox(hwnd,"You Clicked The Cancel Button!","Cancel Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
      }
      return 0;
    case WM_CLOSE:
      EnableWindow(GetParent(hwnd),TRUE);   //To re-enable main window
      DestroyWindow(hwnd);
      return 0;
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


long __stdcall WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 HINSTANCE hIns;

 switch(msg)
 {
    case WM_CREATE:
    {
      char szClassName[]="Dialog";
      HMENU hMenu, hSubMenu;
      WNDCLASSEX wc;

      hIns=((LPCREATESTRUCT)lParam)->hInstance;
      hMenu = CreateMenu();
      hSubMenu = CreatePopupMenu();
      AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_OPEN,   "&Open");
      AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_SAVE,   "&Save");
      AppendMenu(hSubMenu,  MF_SEPARATOR,         0,              0      );
      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_OPTIONS,     "Explorer");
      AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Options");
      hSubMenu = CreatePopupMenu();
      AppendMenu(hSubMenu,  MF_STRING,            IDD_ABOUT,      "&About");
      AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help" );
      SetMenu(hwnd, hMenu);

      //Register Window Class For Dialog Box
      wc.lpszClassName=szClassName,               wc.lpfnWndProc=DlgProc;
      wc.cbSize=sizeof(wc),                       wc.style=CS_HREDRAW | CS_VREDRAW;
      wc.cbClsExtra=0,                            wc.cbWndExtra=0;
      wc.hInstance=hIns,                          wc.hCursor=LoadCursor(NULL, IDC_ARROW);
      wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW,   wc.lpszMenuName=NULL;
      wc.hIcon=0;                                 wc.hIconSm=0;
      RegisterClassEx(&wc);

      return 0;
    }
    case WM_COMMAND:
      switch(LOWORD(wParam))
      {
         case ID_FILE_OPEN:
         {
           MessageBox(hwnd,"You Want To Open A File!","File Open!",MB_OK);
           return 0;
         }
         case ID_FILE_SAVE:
         {
           MessageBox(hwnd,"You Want To Save A File!","File Save!",MB_OK);
           return 0;
         }
         case IDD_ABOUT:
         {
           char szCap2[]="An example program showing how to";
           char szCap1[]="About This Program";
           DWORD dwExStyle,dwStyle,dwSty;
           HWND hDlg,hCtl,hGroup;
           MSG Msg;

           dwExStyle=WS_EX_DLGMODALFRAME | WS_EX_CONTROLPARENT;
           dwStyle=WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE;
           dwSty=WS_CHILD|WS_VISIBLE;
           hIns=GetModuleHandle(0);
           hDlg=CreateWindowEx(dwExStyle,"Dialog","Dialog Window",dwStyle,175,200,400,145,hwnd,0,hIns,NULL);
           hGroup=CreateWindow("button",szCap1,BS_GROUPBOX|dwSty,5,5,385,100,hDlg,(HMENU)IDD_GROUP,hIns,0);
           hCtl=CreateWindow("button","OK",WS_TABSTOP|dwSty,295,25,80,25,hDlg,(HMENU)IDD_OK,hIns,0);
           hCtl=CreateWindow("button","Cancel",WS_TABSTOP|dwSty,295,65,80,25,hDlg,(HMENU)IDD_CANCEL,hIns,0);
           hCtl=CreateWindow("static",szCap2,dwSty,25,25,275,25,hGroup,(HMENU)-1,hIns,0);
           hCtl=CreateWindow("static","              use Dialog Boxes",dwSty,25,42,275,25,hGroup,(HMENU)-1,hIns,0);
           hCtl=CreateWindow("static","                     by Cody",dwSty,25,72,275,25,hGroup,(HMENU)-1,hIns,0);
           while(GetMessage(&Msg, NULL, 0, 0))
           {
              if(!IsDialogMessage(hDlg,&Msg))
              {
                 TranslateMessage(&Msg);
                 DispatchMessage(&Msg);
              }
           }
           return 0;
         }
         case ID_OPTIONS:
           system("explorer c:\\");
           return 0;
         case ID_FILE_EXIT:
           SendMessage(hwnd,WM_CLOSE,0,0);
           return 0;
      }
      break;
    case WM_CLOSE:
      DestroyWindow(hwnd);
      PostQuitMessage(0);
      return 0;
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 char szClassName[]="Cody";
 WNDCLASSEX wc;
 HWND hWnd;
 MSG Msg;

 wc.lpszClassName=szClassName,           wc.lpfnWndProc=WndProc;;
 wc.cbSize=sizeof(WNDCLASSEX),           wc.style=0;
 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.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(WS_EX_CLIENTEDGE,szClassName,"OEBEL STUDIOS",WS_OVERLAPPEDWINDOW,150,150,240,120,NULL,NULL,hInstance,NULL);
 ShowWindow(hWnd,nCmdShow);
 while(GetMessage(&Msg, NULL, 0, 0))
 {
    if(!IsDialogMessage(hWnd,&Msg))
    {
       TranslateMessage(&Msg);
       DispatchMessage(&Msg);
    }
 }

 return Msg.wParam;
}

And another thing, putting that message pump in the WM_COMMAND handler for the menu selection that triggers creation of the Dialog Box is a really, really, really strange thing to do. In fact, in my 10 years of Api coding I've never done that before. It does work, however. The reason I had to do it was to get some message translation to work for the buttons on the Dialog so that tab order would be preserved. Otherwise, the message pump in WinMain() would have surficed.

Been butchering up your code pretty bad Cody! Don't think you'll recognize it anymore. Added some Open File Dialog stuff on the File Menu, and added two versions of 'Message Crackers'. The 2nd program has a little include file...

1st

//Main.cpp
#include <windows.h>              //Program creates menu and dialog box programatically, i.e.,
#include <commdlg.h>              //without resource script file ( *.rc ).  Also shows simplest
#include <stdio.h>                //way of breaking the Window Procedure into message handling
#define IDC_STATIC          -1    //routines.  Some functionality of the File Open Dialog Box
#define ID_FILE_OPEN      1500    //from the Common Dialog Box Library Comdlg32.dll is shown.
#define ID_FILE_SAVE      1505    //Finally, printing output to a Debug output file is shown
#define ID_FILE_EXIT      1510    //where the file is opened in the WM_CREATE handler and finaly
#define ID_OPTIONS        1600    //appended to and closed in the WM_CLOSE handler.  Program
#define IDD_ABOUT         1700    //compiled on CodeBlocks to 9728 bytes.  Win32 programs do
#define IDD_DIALOGABOUT   1700    //not become bloatware.
#define IDD_GROUP         1705
#define IDD_OK            1710
#define IDD_CANCEL        1715


long __stdcall DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)    //This is the Window Procedure for the 'Dialog' Window Class Registered
 {              //down in WndProc_OnCreate().  It is a normal Window Procedure - not a
    case WM_CREATE:  //Dialog Box Proceure As used by the Windows Dialog Engine.
      EnableWindow(GetParent(hwnd),FALSE);  //To make the popup dialog/window modal
      return 0;
    case WM_COMMAND:
      switch(LOWORD(wParam))
      {
         case IDD_OK:
           MessageBox(hwnd,"You Clicked The OK Button!","OK Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
         case IDD_CANCEL:
           MessageBox(hwnd,"You Clicked The Cancel Button!","Cancel Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
      }
      return 0;
    case WM_CLOSE:
      EnableWindow(GetParent(hwnd),TRUE);   //To re-enable main window
      DestroyWindow(hwnd);
      return 0;
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


long WndProc_OnCreate(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 char szClassName[]="Dialog";   //This procedure handles the WM_CREATE
 char* ptrOpenFileName=NULL;    //Message for the "Cody" Window Class
 HMENU hMenu, hSubMenu;         //Registered down in WinMain().  Its a
 HINSTANCE hIns;                //good place to perform program initial-
 WNDCLASSEX wc;                 //ization because it is only called one
 FILE* fp;                      //time.

 fp=fopen("Output.txt","w");                  //First, create menu
 fprintf(fp,"Entering WndProc_OnCreate()\n"); //programitically...
 hIns=((LPCREATESTRUCT)lParam)->hInstance;
 hMenu = CreateMenu();
 hSubMenu = CreatePopupMenu();
 AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_OPEN,   "&Open");
 AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_SAVE,   "&Save");
 AppendMenu(hSubMenu,  MF_SEPARATOR,         0,              0      );
 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_OPTIONS,     "&Explorer");
 AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "O&ptions");
 hSubMenu = CreatePopupMenu();
 AppendMenu(hSubMenu,  MF_STRING,            IDD_ABOUT,      "&About");
 AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help" );
 SetMenu(hwnd, hMenu);

 //Then Register Window Class For Dialog Box
 wc.lpszClassName=szClassName,               wc.lpfnWndProc=DlgProc;
 wc.cbSize=sizeof(wc),                       wc.style=CS_HREDRAW | CS_VREDRAW;
 wc.cbClsExtra=0,                            wc.cbWndExtra=0;
 wc.hInstance=hIns,                          wc.hCursor=LoadCursor(NULL, IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW,   wc.lpszMenuName=NULL;
 wc.hIcon=0;                                 wc.hIconSm=0;
 RegisterClassEx(&wc);

 //And finally Allocate Memory To Store File Name from Open File Dialog for
 ptrOpenFileName=(char*)GlobalAlloc(GPTR,256); //later display in WM_PAINT
 if(ptrOpenFileName)                           //handler on the main window
    SetWindowLong(hwnd,GWL_USERDATA,(long)ptrOpenFileName);
 else
   return -1;
 fprintf(fp, "  ptrOpenFileName = %u\n",(unsigned int)ptrOpenFileName);
 fprintf(fp,"Leaving WndProc_OnCreate()\n\n");
 fclose(fp);

 return 0;
}


long WndProc_OnFileOpen(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 static char szFilter[]="C Files (*.C),CPP Files (*.cpp)\0*.c;*.cpp\0\0";
 static char szTitleName[_MAX_FNAME+_MAX_EXT];
 static char szFileName[_MAX_PATH];
 char* ptrOpenFileName=NULL;             //To learn how to use the Common
 char lpszBuffer[256];                   //Dialog Box Library you need to
 OPENFILENAME ofn;                       //learn about the various structs
                                         //such as OPENFILENAME declared
 GetCurrentDirectory(256,lpszBuffer);    //and defined in "Commdlg.h".  To
 memset(&ofn,'\0',sizeof(OPENFILENAME)); //make a long story short for the
 ofn.lStructSize=sizeof(OPENFILENAME);   //Open File Dialog Box you declare
 ofn.lpstrFilter = szFilter;             //and fill out an OPENFILENAME
 ofn.nMaxFile=_MAX_PATH;                 //struct then call GetOpenFileName().
 ofn.nMaxFileTitle=_MAX_FNAME+_MAX_EXT;  //Prior to that you have to pre-
 ofn.lpstrInitialDir = lpszBuffer;       //allocate buffers into which this
 ofn.lpstrDefExt = "cpp";                //function can store the file name
 ofn.hInstance=GetModuleHandle("");      //data for you.
 ofn.hwndOwner = hwnd;
 ofn.Flags=OFN_HIDEREADONLY | OFN_CREATEPROMPT;
 ofn.lpstrFile=szFileName;
 ofn.lpstrFileTitle=szTitleName;
 GetOpenFileName(&ofn);
 ptrOpenFileName=(char*)GetWindowLong(hwnd,GWL_USERDATA);
 strcpy(ptrOpenFileName,ofn.lpstrFile);
 InvalidateRect(hwnd,NULL,FALSE);

 return 0;
}


long WndProc_OnFileSave(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 MessageBox(hwnd,"You Want To Save A File!","File Save!",MB_OK);
 return 0;
}


long WndProc_OnAbout(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 char szCap2[]="An example program showing how to";    //This procedure programitically
 char szCap1[]="About This Program";                   //creates a non-resource script
 DWORD dwExStyle,dwStyle,dwSty;                        //based modal Dialog Box by setting
 HWND hDlg,hCtl,hGroup;                                //the proper Window Styles for
 HINSTANCE hIns;                                       //Dialog Boxes, and then entering
 MSG Msg;                                              //a special message loop that just
                                                       //services the Dialog Box.  While
 dwExStyle=WS_EX_DLGMODALFRAME | WS_EX_CONTROLPARENT;  //this procedure is running the
 dwStyle=WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE;     //main program window is disabled.
 dwSty=WS_CHILD|WS_VISIBLE;
 hIns=GetModuleHandle(0);
 hDlg=CreateWindowEx(dwExStyle,"Dialog","Dialog Window",dwStyle,175,200,400,145,hwnd,0,hIns,NULL);
 hGroup=CreateWindow("button",szCap1,BS_GROUPBOX|dwSty,5,5,385,100,hDlg,(HMENU)IDD_GROUP,hIns,0);
 hCtl=CreateWindow("button","OK",WS_TABSTOP|dwSty,295,25,80,25,hDlg,(HMENU)IDD_OK,hIns,0);
 hCtl=CreateWindow("button","Cancel",WS_TABSTOP|dwSty,295,65,80,25,hDlg,(HMENU)IDD_CANCEL,hIns,0);
 hCtl=CreateWindow("static",szCap2,dwSty,25,25,275,25,hGroup,(HMENU)-1,hIns,0);
 hCtl=CreateWindow("static","              use Dialog Boxes",dwSty,25,42,275,25,hGroup,(HMENU)-1,hIns,0);
 hCtl=CreateWindow("static","                     by Cody",dwSty,25,72,275,25,hGroup,(HMENU)-1,hIns,0);
 while(GetMessage(&Msg, NULL, 0, 0))
 {
       if(!IsDialogMessage(hDlg,&Msg))
       {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
       }
 }

 return 0;
}


long WndProc_OnOptions(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 system("explorer c:\\");
 return 0;
}


long WndProc_OnExit(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 SendMessage(hwnd,WM_CLOSE,0,0); //In Windows you communicate with various
 return 0;                       //parts of the program by sending messages.
}


long WndProc_OnCommand(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 switch(LOWORD(wParam))  //Child Window Controls decorating various Windows,
 {                       //i.e., buttons, text boxes, etc., communicate with
    case ID_FILE_OPEN:   //their parent window through WM_COMMAND messages.
      return WndProc_OnFileOpen(hwnd,wParam,lParam);
    case ID_FILE_SAVE:
      return WndProc_OnFileSave(hwnd,wParam,lParam);
    case IDD_ABOUT:
      return WndProc_OnAbout(hwnd,wParam,lParam);
    case ID_OPTIONS:
      return WndProc_OnOptions(hwnd,wParam,lParam);
    case ID_FILE_EXIT:
      return WndProc_OnExit(hwnd,wParam,lParam);
 }

 return 0;
}


long WndProc_OnPaint(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 char* ptrOpenFileName=NULL;  //Whenever a WM_PAINT message comes through this
 PAINTSTRUCT ps;              //handler retrieves the pointer to the 256 byte
 HDC hDC;                     //buffer allocated during the WM_CREATE message
                              //and TextOut()'s any File Name string stored
 hDC=BeginPaint(hwnd,&ps);    //there during a invocation of the GetOpenFileName()
 ptrOpenFileName=(char*)GetWindowLong(hwnd,GWL_USERDATA);  //function.
 TextOut(hDC,2,2,ptrOpenFileName,strlen(ptrOpenFileName)); //InvalidateRect() is
 EndPaint(hwnd,&ps);          //used in WndProc_OnFileOpen() to make the client
                              //area of the main window invalid, which forces
 return 0;                    //Windows to send a WM_PAINT message.
}


long WndProc_OnClose(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
 char* ptrOpenFileName=NULL;  //Memory which has been allocated must be released
 bool blnFree=0;              //or a memory leak will result.  Here, we are
 FILE* fp;                    //releasing the 256 byte buffer we allocated during
                              //the WM_CREATE message to store the file name string.
 fp=fopen("Output.txt","a+");                   //Note that if you navigate away
 fprintf(fp,"Entering WndProc_OnClose()\n");    //from the program's initial directory
 ptrOpenFileName=(char*)GetWindowLong(hwnd,GWL_USERDATA);  //with the Open File Dialog
 fprintf(fp,"  %s\n",ptrOpenFileName);                     //Box this file wil be
 fprintf(fp,"  ptrOpenFileName = %u\n",(unsigned int)ptrOpenFileName); //opened
 blnFree=(bool)GlobalFree(ptrOpenFileName);                            //elsewhere.
 fprintf(fp,"  blnFree         = %u\n",blnFree);
 DestroyWindow(hwnd);
 PostQuitMessage(0);
 fprintf(fp,"Leaving WndProc_OnClose()\n\n");
 fclose(fp);

 return 0;
}


long __stdcall WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)         //All that's left of the big, bad, ugly Window Procedure
 {                   //is a switchboard type affair that simply re-routes
    case WM_CREATE:  //program execution to the message handler for each message.
      return WndProc_OnCreate(hwnd,wParam,lParam);  //Note that the message
    case WM_COMMAND:                                //handlers must return
      return WndProc_OnCommand(hwnd,wParam,lParam); //whatever Microsoft specifies
    case WM_PAINT:                                  //as the proper return for
      return WndProc_OnPaint(hwnd,wParam,lParam);   //each specific message.
    case WM_CLOSE:                                  //Usually, but not always,
      return WndProc_OnClose(hwnd,wParam,lParam);   //its zero.
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 char szClass[]="Cody";                  //This is the program entry point and I usually restrain
 WNDCLASSEX wc;                          //myself from putting anything here except the code to
 HWND hWnd;                              //register my main window class, create the main window,
 MSG Msg;                                //and fall into the message loop.

 wc.lpszClassName=szClass,               wc.lpfnWndProc=WndProc;;
 wc.cbSize=sizeof(WNDCLASSEX),           wc.style=0;
 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.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
 RegisterClassEx(&wc);
 hWnd=CreateWindow(szClass,"Oebel Studios",WS_OVERLAPPEDWINDOW,350,350,350,200,NULL,NULL,hInstance,NULL);
 ShowWindow(hWnd,nCmdShow);
 while(GetMessage(&Msg, NULL, 0, 0))
 {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
 }

 return Msg.wParam;
}

....and 2nd version...

//Cody4.h
#ifndef CODY4_H_INCLUDED
#define CODY4_H_INCLUDED
#define IDC_STATIC          -1
#define ID_FILE_OPEN      1500
#define ID_FILE_SAVE      1505
#define ID_FILE_EXIT      1510
#define ID_OPTIONS        1600
#define IDD_ABOUT         1700
#define IDD_DIALOGABOUT   1700
#define IDD_GROUP         1705
#define IDD_OK            1710
#define IDD_CANCEL        1715

typedef struct    WindowsEventArguments
{
 HWND             hWnd;
 WPARAM           wParam;
 LPARAM           lParam;
 HINSTANCE        hIns;
}WndEventArgs,    *lpWndEventArgs;


struct EVENTHANDLER
{
 unsigned int    Code;
 long            (*fnPtr)(lpWndEventArgs);
};
#endif // CODY4_H_INCLUDED
//Main.cpp
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include "Cody4.h"
EVENTHANDLER  MainHandler[4];


long __stdcall DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)    //This is the Window Procedure for the 'Dialog' Window Class Registered
 {              //down in WndProc_OnCreate().  It is a normal Window Procedure - not a
    case WM_CREATE:  //Dialog Box Proceure As used by the Windows Dialog Engine.
      EnableWindow(GetParent(hwnd),FALSE);  //To make the popup dialog/window modal
      return 0;
    case WM_COMMAND:
      switch(LOWORD(wParam))
      {
         case IDD_OK:
           MessageBox(hwnd,"You Clicked The OK Button!","OK Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
         case IDD_CANCEL:
           MessageBox(hwnd,"You Clicked The Cancel Button!","Cancel Button Click!",MB_OK);
           SendMessage(hwnd,WM_CLOSE,0,0);
           break;
      }
      return 0;
    case WM_CLOSE:
      EnableWindow(GetParent(hwnd),TRUE);   //To re-enable main window
      DestroyWindow(hwnd);
      return 0;
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
 }

 return DefWindowProc(hwnd, msg, wParam, lParam);
}


long WndProc_OnCreate(lpWndEventArgs Wea)
{
 char szClassName[]="Dialog";   //This procedure handles the WM_CREATE
 char* ptrOpenFileName=NULL;    //Message for the "Cody" Window Class
 HMENU hMenu, hSubMenu;         //Registered down in WinMain().  Its a
 WNDCLASSEX wc;                 //good place to perform program initial-
 FILE* fp;                      //ization because it is only called one
                                //time.
 fp=fopen("Output.txt","w");                  //First, create menu
 fprintf(fp,"Entering WndProc_OnCreate()\n"); //programitically...
 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
 hMenu = CreateMenu();
 hSubMenu = CreatePopupMenu();
 AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_OPEN,   "&Open");
 AppendMenu(hSubMenu,  MF_STRING,            ID_FILE_SAVE,   "&Save");
 AppendMenu(hSubMenu,  MF_SEPARATOR,         0,              0      );
 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_OPTIONS,     "&Explorer");
 AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "O&ptions");
 hSubMenu = CreatePopupMenu();
 AppendMenu(hSubMenu,  MF_STRING,            IDD_ABOUT,      "&About");
 AppendMenu(hMenu,     MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help" );
 SetMenu(Wea->hWnd, hMenu);

 //Then Register Window Class For Dialog Box
 wc.lpszClassName=szClassName,               wc.lpfnWndProc=DlgProc;
 wc.cbSize=sizeof(wc),                       wc.style=CS_HREDRAW | CS_VREDRAW;
 wc.cbClsExtra=0,                            wc.cbWndExtra=0;
 wc.hInstance=Wea->hIns,                     wc.hCursor=LoadCursor(NULL, IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW,   wc.lpszMenuName=NULL;
 wc.hIcon=0;                                 wc.hIconSm=0;
 RegisterClassEx(&wc);

 //And finally Allocate Memory To Store File Name from Open File Dialog for
 ptrOpenFileName=(char*)GlobalAlloc(GPTR,256); //later display in WM_PAINT
 if(ptrOpenFileName)                           //handler on the main window
    SetWindowLong(Wea->hWnd,GWL_USERDATA,(long)ptrOpenFileName);
 else
   return -1;
 fprintf(fp, "  ptrOpenFileName = %u\n",(unsigned int)ptrOpenFileName);
 fprintf(fp,"Leaving WndProc_OnCreate()\n\n");
 fclose(fp);

 return 0;
}


long WndProc_OnFileOpen(lpWndEventArgs Wea)
{
 static char szFilter[]="C Files (*.C),CPP Files (*.cpp)\0*.c;*.cpp\0\0";
 static char szTitleName[_MAX_FNAME+_MAX_EXT];
 static char szFileName[_MAX_PATH];
 char* ptrOpenFileName=NULL;             //To learn how to use the Common
 char lpszBuffer[256];                   //Dialog Box Library you need to
 OPENFILENAME ofn;                       //learn about the various structs
                                         //such as OPENFILENAME declared
 GetCurrentDirectory(256,lpszBuffer);    //and defined in "Commdlg.h".  To
 memset(&ofn,'\0',sizeof(OPENFILENAME)); //make a long story short for the
 ofn.lStructSize=sizeof(OPENFILENAME);   //Open File Dialog Box you declare
 ofn.lpstrFilter = szFilter;             //and fill out an OPENFILENAME
 ofn.nMaxFile=_MAX_PATH;                 //struct then call GetOpenFileName().
 ofn.nMaxFileTitle=_MAX_FNAME+_MAX_EXT;  //Prior to that you have to pre-
 ofn.lpstrInitialDir = lpszBuffer;       //allocate buffers into which this
 ofn.lpstrDefExt = "cpp";                //function can store the file name
 ofn.hInstance=GetModuleHandle("");      //data for you.
 ofn.hwndOwner = Wea->hWnd;
 ofn.Flags=OFN_HIDEREADONLY | OFN_CREATEPROMPT;
 ofn.lpstrFile=szFileName;
 ofn.lpstrFileTitle=szTitleName;
 GetOpenFileName(&ofn);
 ptrOpenFileName=(char*)GetWindowLong(Wea->hWnd,GWL_USERDATA);
 strcpy(ptrOpenFileName,ofn.lpstrFile);
 InvalidateRect(Wea->hWnd,NULL,FALSE);

 return 0;
}


long WndProc_OnFileSave(lpWndEventArgs Wea)
{
 MessageBox(Wea->hWnd,"You Want To Save A File!","File Save!",MB_OK);
 return 0;
}


long WndProc_OnAbout(lpWndEventArgs Wea)
{
 char szCap2[]="An example program showing how to";    //This procedure programitically
 char szCap1[]="About This Program";                   //creates a non-resource script
 DWORD dwExStyle,dwStyle,dwSty;                        //based modal Dialog Box by setting
 HWND hDlg,hCtl,hGroup;                                //the proper Window Styles for
 MSG Msg;                                              //Dialog Boxes, and then entering
                                                       //a special message loop that just
 dwExStyle=WS_EX_DLGMODALFRAME | WS_EX_CONTROLPARENT;  //services the Dialog Box.  While
 dwStyle=WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE;     //this procedure is running the
 dwSty=WS_CHILD|WS_VISIBLE;                            //main program window is disabled.
 Wea->hIns=GetModuleHandle(0);
 hDlg=CreateWindowEx(dwExStyle,"Dialog","Dialog Window",dwStyle,175,200,400,145,Wea->hWnd,0,Wea->hIns,NULL);
 hGroup=CreateWindow("button",szCap1,BS_GROUPBOX|dwSty,5,5,385,100,hDlg,(HMENU)IDD_GROUP,Wea->hIns,0);
 hCtl=CreateWindow("button","OK",WS_TABSTOP|dwSty,295,25,80,25,hDlg,(HMENU)IDD_OK,Wea->hIns,0);
 hCtl=CreateWindow("button","Cancel",WS_TABSTOP|dwSty,295,65,80,25,hDlg,(HMENU)IDD_CANCEL,Wea->hIns,0);
 hCtl=CreateWindow("static",szCap2,dwSty,25,25,275,25,hGroup,(HMENU)-1,Wea->hIns,0);
 hCtl=CreateWindow("static","              use Dialog Boxes",dwSty,25,42,275,25,hGroup,(HMENU)-1,Wea->hIns,0);
 hCtl=CreateWindow("static","                     by Cody",dwSty,25,72,275,25,hGroup,(HMENU)-1,Wea->hIns,0);
 while(GetMessage(&Msg, NULL, 0, 0))
 {
       if(!IsDialogMessage(hDlg,&Msg))
       {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
       }
 }

 return 0;
}


long WndProc_OnOptions(lpWndEventArgs Wea)
{
 system("explorer c:\\");
 return 0;
}


long WndProc_OnExit(lpWndEventArgs Wea)
{
 SendMessage(Wea->hWnd,WM_CLOSE,0,0); //In Windows you communicate with various
 return 0;                       //parts of the program by sending messages.
}


long WndProc_OnCommand(lpWndEventArgs Wea)
{
 switch(LOWORD(Wea->wParam))  //Child Window Controls decorating various Windows,
 {                            //i.e., buttons, text boxes, etc., communicate with
    case ID_FILE_OPEN:        //their parent window through WM_COMMAND messages.
      return WndProc_OnFileOpen(Wea);
    case ID_FILE_SAVE:
      return WndProc_OnFileSave(Wea);
    case IDD_ABOUT:
      return WndProc_OnAbout(Wea);
    case ID_OPTIONS:
      return WndProc_OnOptions(Wea);
    case ID_FILE_EXIT:
      return WndProc_OnExit(Wea);
 }

 return 0;
}


long WndProc_OnPaint(lpWndEventArgs Wea)
{
 char* ptrOpenFileName=NULL;     //Whenever a WM_PAINT message comes through this
 PAINTSTRUCT ps;                 //handler retrieves the pointer to the 256 byte
 HDC hDC;                        //buffer allocated during the WM_CREATE message
                                 //and TextOut()'s any File Name string stored
 hDC=BeginPaint(Wea->hWnd,&ps);  //there during a invocation of the GetOpenFileName()
 ptrOpenFileName=(char*)GetWindowLong(Wea->hWnd,GWL_USERDATA);  //function.
 TextOut(hDC,2,2,ptrOpenFileName,strlen(ptrOpenFileName)); //InvalidateRect() is
 EndPaint(Wea->hWnd,&ps);        //used in WndProc_OnFileOpen() to make the client
                                 //area of the main window invalid, which forces
 return 0;                       //Windows to send a WM_PAINT message.
}


long WndProc_OnClose(lpWndEventArgs Wea)
{
 char* ptrOpenFileName=NULL;  //Memory which has been allocated must be released
 bool blnFree=0;              //or a memory leak will result.  Here, we are
 FILE* fp;                    //releasing the 256 byte buffer we allocated during
                              //the WM_CREATE message to store the file name string.
 fp=fopen("Output.txt","a+");                   //Note that if you navigate away
 fprintf(fp,"Entering WndProc_OnClose()\n");    //from the program's initial directory
 ptrOpenFileName=(char*)GetWindowLong(Wea->hWnd,GWL_USERDATA); //with the Open File
 fprintf(fp,"  %s\n",ptrOpenFileName);                     //DialogBox this file wil be
 fprintf(fp,"  ptrOpenFileName = %u\n",(unsigned int)ptrOpenFileName); //opened
 blnFree=(bool)GlobalFree(ptrOpenFileName);                            //elsewhere.
 fprintf(fp,"  blnFree         = %u\n",blnFree);
 DestroyWindow(Wea->hWnd);
 PostQuitMessage(0);
 fprintf(fp,"Leaving WndProc_OnClose()\n\n");
 fclose(fp);

 return 0;
}


void AttachEventHandlers(void)         //This procedure maps windows messages to the
{                                      //procedure which handles them.
 MainHandler[0].Code=WM_CREATE,       MainHandler[0].fnPtr=WndProc_OnCreate;
 MainHandler[1].Code=WM_COMMAND,      MainHandler[1].fnPtr=WndProc_OnCommand;
 MainHandler[2].Code=WM_PAINT,        MainHandler[2].fnPtr=WndProc_OnPaint;
 MainHandler[3].Code=WM_CLOSE,        MainHandler[3].fnPtr=WndProc_OnClose;

}


long __stdcall WndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
 WndEventArgs Wea;                  //This procedure loops through the EVENTHANDER array
                                    //of structs to try to make a match with the msg parameter
 for(unsigned int i=0; i<4; i++)    //of the WndProc.  If a match is made the event handling
 {                                  //procedure is called through a function pointer -
     if(MainHandler[i].Code==msg)   //(EventHandler[i].fnPtr).  If no match is found the
     {                              //msg is passed onto DefWindowProc().
        Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
        return (*MainHandler[i].fnPtr)(&Wea);
     }
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 char szClass[]="Cody";                  //This is the program entry point and I usually restrain
 WNDCLASSEX wc;                          //myself from putting anything here except the code to
 HWND hWnd;                              //register my main window class, create the main window,
 MSG Msg;                                //and fall into the message loop.

 AttachEventHandlers();
 wc.lpszClassName=szClass,               wc.lpfnWndProc=WndProc;;
 wc.cbSize=sizeof(WNDCLASSEX),           wc.style=0;
 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.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
 RegisterClassEx(&wc);
 hWnd=CreateWindow(szClass,"Oebel Studios",WS_OVERLAPPEDWINDOW,350,350,350,200,NULL,NULL,hInstance,NULL);
 ShowWindow(hWnd,nCmdShow);
 while(GetMessage(&Msg, NULL, 0, 0))
 {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
 }

 return Msg.wParam;
}

Just thought I'd make a comment on the technical nature of what I did there. Its kind of fundamental. I completely removed the Windows Dialog Engine from the picture. The Windows Dialog Engine was created as kind of a wrapper on the basic RegesterClass() and CreateWindow() functionality within Windows. The intent was to simplify things for simple windows that are a little bit more complicated than Message Boxes, but not requiring the full functionality of custom made/designed Window Classes.

What I did was create a regular Window Class for the Dialog with the typical RegisterClassEx(), CreateWindow() sequence, and therefore I have an ordinary Window Procedure in the program to service the Window - not a Dialog Procedure which uses somewhat different messages. What I did was use the class styles typical of Dialog Boxes to accomplish this. Also note the EnableWindow() function was used to disable the main program window while the Dialog Box was active. This is typical of how modal dialog boxes work. They won't allow you to interact with other Windows until you dismiss them.

AHHHhhh I see... I like this style in essence I almost dont even need to really use dialog resources when they are windows just after all, and you built the code to open a window, and 'ACT' like a modal dialog. Another thing I havent tried yet and I am going to get to it is I have been compiling this entire tutorial API project as c++, but it is supposed to be done in C. Wonder if thats been an issue ? Either way Fred.. I am keeping your example stored in my email forever LOL ive been disecting it, and working on it learning, and going back to it when I am in question of something.
I'm getting into text editing... which looks pretty nasty lol, but hopefully within 8 months I will be operational with windows API.


Anyhow.. Thanks allot for your activity here on DaniWeb; I really appreciate it allot that you spent some time of your day to help me.

Seriously... THANK YOU SO MUCH!
Usually cost money to get someone like yourself to help out!

Cody Oebel

Anyhow.. Thanks allot for your activity here on DaniWeb; I really appreciate it allot that you spent some time of your day to help me.

Well, no problem. I like to help if I can. Right now I have some time on my hands because I'm at home recovering from a neck operation. Anyway, I wanted to see if I could get by without using resource scripts so when you were having difficulty and couldn't get one to work I decided to explore the matter a little. However, I do think you ought to try to solve your resource script problem, because even if you decide to go the route I showed, you'll still need to understand and use them if for no other reason than everybody else uses them a lot.

On that note last night I spent about an hour experimenting with command line compiling using Dev-Cpp and I got it to work. Post back here if you want to give it a try and I'll lead you through it. Might be possible to compile that recalcitrant program that way

Also, I've written quite a bit of beginning Api tutorials here...

http://www.jose.it-berater.org/smfforum/index.php?board=380.0

I'll probably post some new stuff there shortly. The GUI stuff starts around ProgEx37 I think.

I've found this with Dev C++ too. It's not great with dialogs made from resource files or menus that are called into the main window with MAKEINTRESOURCE. Menus tend to disappear completely! Hemsworth is right though, it's not necessarily buggy, it's just a bit more particular with the order that you add files to a project. The best way i've found to do win32 programming in Dev C++ is to add .rc files last before compiling and after this don't mess around with them once the project has been compiled. Don't even open the .rc file in project space

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.