Click here to Skip to main content
15,887,917 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C++
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";


// defining dialog box
    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 theForger",
                        IDC_STATIC,16,18,144,33
    END

// about us dialog procedure
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;
            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_STUFF_GO, "&Go");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
            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_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;
            case ID_FILE_EXIT:
                PostMessage(hwnd, WM_CLOSE, 0, 0);
            break;
            case ID_STUFF_GO:

            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,
        "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);

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





on compilation , it gives me error:


IDD_ABOUT does not name a type


please let me know whats the problem
Posted
Comments
Stefan_Lang 11-Jun-15 8:49am    
This is just a wild guess, as I haven't programmed MFC in decades, but shouldn't it be IDD_ABOUT_DIALOG (two underscores) rather than IDD_ABOUT DIALOG (one underscore one space) in your code?
bhawin parkeria 11-Jun-15 8:57am    
nopes , i am also a newbie but i am damm sure thats not right

IDD_ABOUT is a variable identifier and DIALOG is a structure
Stefan_Lang 12-Jun-15 2:15am    
That may well be, but if you recognize this much, it should also be obvious that the entire block of code is not valid C/C++ code. If we need to explain why, then you better get back to your books and articles and start learning the language from scratch. I am serious: windows programming does require at least a basic understanding and some experience, unless you are guided by some tutorial (and there are lots - just google for them).

It would also help to use VisualStudio - there are free versions available. While other compilers such as gcc do have their advantages, if you are programming windows GUI, VisualStudio has all the tools built-in, and it's got help files and wizards that help you sort just this sort of stuff!
enhzflep 11-Jun-15 22:59pm    
Is the code you show in a single file? (it shouldn't be)

You need to put the dialogue template stuff in an rc file, compile that file with mingw's resource compiler and then link the produced file along with the object code of your c source code to produce the final .exe file.
Philippe Mori 12-Jun-15 12:34pm    
If you are a newbie, then it might be easier for you to create a Winforms application in C#.

This thread on StackOverflow seems to have the answer:

Create an include file called resource.h and add this line to it:
C++
#define IDD_ABOUT   100

Then include it at the top of your .cpp and .rc files:
C++
#include "resource.h" // add this line


It looks like you've already included the resource.h file; have you double-checked that you've defined the IDD_ABOUT symbol in it?
 
Share this answer
 
Comments
bhawin parkeria 11-Jun-15 9:34am    
i did it , thnx , but now it gives error: expected unqualified-id before numeric constant
Sergey Alexandrovich Kryukov 11-Jun-15 14:25pm    
This looks (sigh...) hopeless. You seemingly don't understand where is the C or C++ code and where it is not.
—SA
Sergey Alexandrovich Kryukov 11-Jun-15 14:26pm    
5, but please see my comments.
—SA
The entire code block under //defining dialog box looks very odd:
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 theForger",
                    IDC_STATIC,16,18,144,33
END


Seems like you copy-pasted this with a tool that somehow eliminated lots of non-alpha characters such as parenthesis and commas at random.

There are a whole lot of symbols that should be separated, but are not, so even if you manage to fix that IDD_ABOUT error, you'll just run into the next unknown symbol or missing separator.

Either double check your source that you got the code from and make sure you really have the complete and correct code, or just do it yourself - it's not that difficult.

On a sidenote, nothing in that code is C++ - this is pure C.
 
Share this answer
 
Comments
bhawin parkeria 11-Jun-15 9:28am    
i copied this code from http://www.winprog.org/tutorial/dialogs.html

u can check please
Sergey Alexandrovich Kryukov 11-Jun-15 14:23pm    
I checked it up. No, your "code" is not what's written in this page. Do you even have to explain you why? That is .rc code.

Your whole question makes no sense. It does not seem that you understand what you are asking about. If you need some results, stop copying the code you don't understand, start writing your own based on knowledge you really have and learn more.

—SA
Stefan_Lang 12-Jun-15 2:09am    
Ah, you're right, I should have recognized it's rc code, but as I said, it's been decades - ok, only about two, but still ;)
Sergey Alexandrovich Kryukov 12-Jun-15 2:13am    
:-)
Stefan_Lang 12-Jun-15 2:18am    
The text above that code block clearly states:

"The first step is to create the dialog resource."

Why, then, did you put it in your code?
My first guess is that the IDD_ABOUT isnt defined in the resource.h with some number.

The complete dialog template stuff belongs in a rc-file. It can be opened in a Resource View Window in Visual Studio. It may complain about other bugs.
 
Share this answer
 
Comments
bhawin parkeria 11-Jun-15 9:31am    
suppose i put all the code in rc file ,


how will i include rc file


no , i dont want to use visual c++ , i am just doing it with gcc and sbulime text
Sergey Alexandrovich Kryukov 11-Jun-15 14:27pm    
gcc and Windows, really? But why?
Note that if you are the one asking a question, your "I don't want to..." cannot be enough.
—SA
Stefan_Lang 12-Jun-15 2:28am    
If you have no experience working with rc files and programming a GUI on windows, there are only ttwo sensible ways:

1. Use VisualStudio (any version)
2. Use a GUI library such as QT

Judging by the level of understanding you exhibit, my advice is clearly the first: QT is really only for advanced programmers.

Alternately you can try your luck with another GUI library that is a bit more limited and easier to use than QT. But forget about "trying to run" by manually programming rc files before "learning to walk"!
bhawin parkeria 11-Jun-15 9:35am    
i defined IDD_ABOUT as 100 in resource.h now the programme gives another error on the same line
:
expected unqualified-id before numeric constant
Stefan_Lang 12-Jun-15 2:30am    
Just forget it - rc files are not that hard to understand and manipulate, but you clearly don't even understand the basics of C/C++. Just try learning the language before adding another layer of complexity! Or leave the GUI to VisualStudio.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900