• Advertisement
Sign in to follow this  

Problem with menu *SOLVED*

This topic is 4928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I just now began working on graphic editor. I wanted to add menu but.. it is invisible and I don't know why. My code:
/* resources.h */
#define IDR_MENU1   101
#define ID_QUIT   40001

/* resources.rc */
#include "resources.h"
IDR_MENU1 MENU DISCARDABLE
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "QUIT",  ID_QUIT
    END
END // maybe this is wrong



/* main.cpp */
#define CLASS_NAME "ClassName"
#define TITLE "Window title"

HWND      hMainWnd = NULL;
HMENU     hMainMenu = NULL;
HINSTANCE hMainInst = NULL;
int       iWndWidth;
int       iWndHeight;

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    switch (Msg)
    {
        case WM_PAINT:
            {
                BeginPaint(hMainWnd,&ps);
                EndPaint(hMainWnd,&ps);
            } break;
        case WM_CLOSE:
        case WM_DESTROY: PostQuitMessage(0); break;
    }
    return DefWindowProc(hWnd,Msg,wParam,lParam);    
}    

bool InitMainWnd(int width, int height)
{
    WNDCLASS wc;
    wc.style         = CS_VREDRAW|CS_HREDRAW;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.lpfnWndProc   = (WNDPROC)WndProc;
    wc.lpszClassName = CLASS_NAME;
    wc.lpszMenuName  = NULL;
    wc.hInstance     = (HINSTANCE)hMainInst;
    wc.hIcon         = (HICON)LoadIcon(NULL,IDI_APPLICATION);
    wc.hCursor       = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    if (!RegisterClass(&wc))
    {
        MessageBox(hMainWnd,"RegisterClass(&wc) ERROR",TITLE,MB_OK|MB_ICONERROR);
        return false;
    }
    
    hMainMenu = LoadMenu((HINSTANCE)hMainInst,MAKEINTRESOURCE(IDR_MENU1));
    
    iWndWidth = width;
    iWndHeight = height;
    hMainWnd = CreateWindow(CLASS_NAME,TITLE,WS_OVERLAPPEDWINDOW,0,0,iWndWidth,                           iWndHeight,NULL,hMainMenu,hMainInst,NULL);
    if (hMainWnd == false)
    {
        MessageBox(hMainWnd,"CreateWindow() ERROR",TITLE,MB_OK|MB_ICONERROR);
        return false;
    }
    
    DoneMainWnd();
    return true;
}

void DoneMainWnd()
{
    if (hMainMenu != NULL) DestroyMenu(hMainMenu);
    UnregisterClass(CLASS_NAME,hMainInst);
}    

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)  
{
    hMainInst = hInstance;
    if (InitMainWnd(640,480) == false) return 0;
    
    ShowWindow(hMainWnd,SW_SHOW);
    UpdateWindow(hMainWnd);
    
    MSG msg;
    ZeroMemory(&msg,sizeof(MSG));
    PeekMessage(&msg,NULL,0,0,PM_REMOVE);
    
    while (1)
    {
        if ((msg.message == WM_QUIT) || (msg.wParam == VK_ESCAPE)) break;
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
        }    
    }
    
    return msg.wParam;
}




I am using Dev-C++. Please, help. [Edited by - RiBi on October 24, 2004 11:26:40 AM]

Share this post


Link to post
Share on other sites
Advertisement
What about this:

in InitMainWnd ():
...
wc.lpszMenuName = MAKEINTRESOURCE (IDR_MENU1);
...


And forget about that LoadMenu call and leave the hMenu parameter of CreateWindow function NULL?

Oxyd

Share this post


Link to post
Share on other sites

/* main.cpp */
#include <windows.h>
#include "resources.h"

#define CLASS_NAME "ClassName"
#define TITLE "Window title"

HWND hMainWnd = NULL;
HINSTANCE hMainInst = NULL;
int iWndWidth;
int iWndHeight;

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch (Msg)
{
case WM_PAINT:
{
BeginPaint(hMainWnd,&ps);
EndPaint(hMainWnd,&ps);
} break;
case WM_CLOSE:
case WM_DESTROY: PostQuitMessage(0); break;
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}

bool InitMainWnd(int width, int height)
{
WNDCLASS wc;
wc.style = CS_VREDRAW|CS_HREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.lpszClassName = CLASS_NAME;
wc.lpszMenuName = MAKEINTRESOURCE (IDR_MENU1);
wc.hInstance = (HINSTANCE)hMainInst;
wc.hIcon = (HICON)LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
if (!RegisterClass(&wc))
{
MessageBox(hMainWnd,"RegisterClass(&wc) ERROR",TITLE,MB_OK|MB_ICONERROR);
return false;
}

iWndWidth = width;
iWndHeight = height;
hMainWnd = CreateWindow(CLASS_NAME,TITLE,WS_OVERLAPPEDWINDOW,0,0,iWndWidth, iWndHeight,NULL,NULL,hMainInst,NULL);
if (hMainWnd == false)
{
MessageBox(hMainWnd,"CreateWindow() ERROR",TITLE,MB_OK|MB_ICONERROR);
return false;
}

//DoneMainWnd(); //what's the point of this, btw?
//doesn't it destroy your window class and menu right after their creation?
return true;
}

void DoneMainWnd()
{
if (hMainMenu != NULL) DestroyMenu(hMainMenu);
UnregisterClass(CLASS_NAME,hMainInst);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
hMainInst = hInstance;
if (InitMainWnd(640,480) == false) return 0;

ShowWindow(hMainWnd,SW_SHOW);
UpdateWindow(hMainWnd);

MSG msg;
ZeroMemory(&msg,sizeof(MSG));
PeekMessage(&msg,NULL,0,0,PM_REMOVE);

while (1)
{
if ((msg.message == WM_QUIT) || (msg.wParam == VK_ESCAPE)) break;
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
}
}

return msg.wParam;
}



This should work...

The resource file looks fine to me. Make sure it's compiled and linked to your app (Get a prog. called Resource Hacker (google) and look if there's a menu resource in your app).

Oxyd

Share this post


Link to post
Share on other sites
I deleted that line (I don't know why I wrote it there), but still nothing happend. I have never had this problem before. Can it be because of Dev-C++?
EDIT: I tried to use Resource Hacker, but every time I open a resource file it write "Out of memory !".

Share this post


Link to post
Share on other sites
Quote:
Original post by RiBi
I deleted that line (I don't know why I wrote it there), but still nothing happend. I have never had this problem before. Can it be because of Dev-C++?
EDIT: I tried to use Resource Hacker, but every time I open a resource file it write "Out of memory !".


No - don't blame your compiler [smile] I've used dev-cpp it for many win32 projects (with menus) with no problems.

I've never experienced resource hacker running out of memory... Wierd...

My guess still is that there's something wrong with the setup of your compiler... Make sure that in Project options at "files" card there is the .rc file listed. And also take look at your makefile (Makefile.win). There should be line looking like:

App_private.res: App_private.rc resource.rc
$(WINDRES) -i App_private.rc -I rc -o App_private.res -O coff


If not - add the .rc file to your project.

Oxyd

Share this post


Link to post
Share on other sites
Problem solved!

I tried to remove my resource file from project and then add it back. And after recompilation it worked!
Probably, it was all because of Dev-C++ 4.9.9.0. It is only a beta yet and it has got some bugs.

Thank you for replying Oxyd. [smile]

Btw., Dev-C++ is not a compiler, it is an IDE using Mingw compiler ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement