Заставка

Программирование на старых и современных языках, а так-же дизайн

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.



Меню Dev-C++

Сообщений 1 страница 2 из 2

1

Я поставил wxDev-C++, а Dev-C++ 5.11 испортилась. Меню буду создавать в Visual C++ 6.0. И так начнём:
1. Запустим Visual C++ 6.0, и выберем File->New:
https://i.ibb.co/GPML86j/menu-c-vcpp60-00001-min.jpg

2. Выбереме вкладку Files и выберем файл Resource Script:
https://i.ibb.co/c89g8jj/menu-c-vcpp60-00002-min.jpg

3. Выберем из меню Insert:
https://i.ibb.co/pyNzwZ8/menu-c-vcpp60-00004-min.jpg

4. Выбереме Menu:
https://i.ibb.co/zP7VChw/menu-c-vcpp60-00003-min.jpg

5. Введём меню:
https://i.ibb.co/yB7dCfk/menu-c-vcpp60-00005-min.jpg

https://i.ibb.co/vsdFYfG/menu-c-vcpp60-00006-min.jpg

6. Создадим новый проект Windows Application:
https://i.ibb.co/jW4F369/menu-c-devcpp-00008-min.jpg

7. Добавим файл ресурсов:
https://i.ibb.co/VgphPH8/menu-c-devcpp-00009-min.jpg

https://i.ibb.co/TYG1cN1/menu-c-devcpp-00010-min.jpg

8. Изменим код:
Чтобы отобразить меню, нужно следующе сделать:
1. Подключить resource.h, в котором находятся определения менюшки, меню, и подменю:

Код:
#include "resource.h"

2. Загрузить меню из ресурсов:

Код:
wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);

Файл ресурсов menu.rc:

Код:
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// German (Germany) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_DEU)
#ifdef _WIN32
LANGUAGE LANG_GERMAN, SUBLANG_GERMAN
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE DISCARDABLE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE DISCARDABLE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDR_MENU1 MENU DISCARDABLE 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&Exit",                       ID_FILE_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About",                      ID_HELP_ABOUT
    END
END

#endif    // German (Germany) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

Файл resource.h:

Код:
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by menu.rc
//
#define IDR_MENU1                       101
#define ID_HELP_ABOUT                   40001
#define ID_FILE_EXIT                    40002

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40003
#define _APS_NEXT_CONTROL_VALUE         1000
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

Код main.c:

Код:
#include <windows.h>
#include "resource.h"

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

9. Откомпилируем, результат должен быть таким:
https://i.ibb.co/YQxNNZ2/menu-c-result-00011-min.jpg

0

2

Улучшим код добавим в main.c обработчик WM_COMMAND:

Код:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_COMMAND:
            if(LOWORD(wParam) == ID_FILE_EXIT) {
              SendMessage(hwnd, WM_DESTROY, NULL, NULL);
            }
            else if(LOWORD(wParam) == ID_HELP_ABOUT) {
              MessageBox(hwnd, "Copyright(c) 2022", "About", MB_ICONINFORMATION | MB_OK);
            }
        break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

0