(译)win32asm实例-5

类别:编程语言 点击:0 评论:0 推荐:
  5.0 - Adding a menu添加菜单

Our program should have a menu to set some options. Firstly we need to create our menu in the resource file:

我们的程序应该有一个菜单来设置一些选项。首先,我们需要在资源文件中创建我们的菜单。

5.1 - Creating a menu创建菜单

A menu is fairly easy to make in your resource file. If you take a look at the resource text below you will see it's self explaining. Popup menus are menus that have submenus, a menuitem is just a menuitem with an associated ID. These IDs will be defined and can be used in your program to find out which menu item the user clicked on. Finally, the ampersands (&) will underline the character the precede, indicating a shortkey can be used.

在你的资源文件中,很容易就可以创建一个菜单。如果你看一看下面的资源文本,你会看到它是其意自明的。Popup(弹出)菜单是有子菜单的菜单,一个菜单项仅是一个有关联ID的菜单项。这些ID将被定义而且可以被用于你的程序来找出用户点击的是哪个菜单项。最后,&号会下划线首字母,表示它可以使用快捷键。

Add this below the ICON definitions:

添加下面的ICON 定义:

MAINMENU    MENU DISCARDABLE
BEGIN
    POPUP   "&File"
    BEGIN
        MENUITEM "&New Game", MI_NEWGAME
        MENUITEM "&Open Bitmap ", MI_OPENBITMAP
        POPUP "&Difficulty"
        BEGIN
            MENUITEM "&Easy", MI_EASY
            MENUITEM "&Medium", MI_MEDIUM
            MENUITEM "&Hard", MI_HARD
        END
    END
    POPUP "&Picture"
    BEGIN
        MENUITEM "Use &standard picture", MI_USESTANDARD
        MENUITEM "Use &numbers", MI_USENUMBERS
        MENUITEM "Use &bitmap file", MI_USEFILE
    END
    POPUP "&Colors"
    BEGIN
        MENUITEM "&Blue color scheme", MI_COLORBLUE
        MENUITEM "&Red color scheme", MI_COLORRED
        MENUITEM "&Green color scheme", MI_COLORGREEN
        MENUITEM "&Ugly color scheme", MI_COLORUGLY
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About", MI_ABOUT
    END
END

Add this below the icon defines (before the code above!):

添加下面的菜单项定义(在前面的代码前!)

#define         MAINMENU            301

#define         MI_NEWGAME          101
#define         MI_OPENBITMAP       102
#define         MI_EASY             103
#define         MI_MEDIUM           104
#define         MI_HARD             105
#define         MI_USESTANDARD      106
#define         MI_USENUMBERS       107
#define         MI_USEFILE          108
#define         MI_COLORBLUE        109
#define         MI_COLORRED         110
#define         MI_COLORGREEN       111
#define         MI_COLORUGLY        112
#define         MI_ABOUT            113

5.2 - Defining the Ids定义ID

Now you'll have to convert the C definitions to asm equates:

现在我们必须把C定义翻译为asm:

MAINMENU        equ 301

MI_NEWGAME      equ 101
MI_OPENBITMAP   equ 102
MI_EASY         equ 103
MI_MEDIUM       equ 104
MI_HARD         equ 105
MI_USESTANDARD  equ 106
MI_USENUMBERS   equ 107
MI_USEFILE      equ 108
MI_COLORBLUE    equ 109
MI_COLORRED     equ 110
MI_COLORGREEN   equ 111
MI_COLORUGLY    equ 112
MI_ABOUT        equ 113

When you've converted them, place them in mosaic.inc.

在你翻译完它们后,把它们放入mosaic.inc中。

The resource file and mosaic inc should now look like this: res1.zip

资源和mosaic.inc文件应该和这个一样:res1.zip

5.3 - Adding the menu to your window把菜单加入你的窗口


[in your .data? section]

[在你的.data?部分]
hMenu           dd      ?

[in your .code section]

[在你的.code部分]
    ......
    invoke  RegisterClassEx, addr wc      ;AFTER THIS LINE!
    invoke  LoadMenu, hInstance, MAINMENU ;load menu
    mov     hMenu, eax                    ;store handle
    INVOKE  CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
            WS_OVERLAPPEDWINDOW-WS_MAXIMIZEBOX-WS_SIZEBOX,\
            CW_USEDEFAULT, CW_USEDEFAULT,258,350,NULL,hMenu,\ ;!!!!
            hInst,NULL
            ;NOTE: notice addition of the hMenu parameter
            ;      in the CreateWindowEx call.
    ......

The hMenu is added to the uninitialized data section to hold the menu handle when it's loaded. LoadMenu will load a menu resource from the instance hInstance (mosaic.exe) with a certain ID (MAINMENU). The handle is stored with 'mov hMenu, eax', then the handle is passed to CreateWindowEx to create a window with a menu. After you have run make.bat you should have a main window with a menu (that does nothing):

HMenu被加入未初始化data部分来在菜单载入后保存菜单句柄。LoadMenu会从实例hInstance(mosaic.exe)中用某个ID(MAINMENU)载入菜单。句柄用‘mov  hMenu, eax’保存,然后句柄传递给CreateWindowEx来创建一个有菜单的窗口。在你运行了make.bat之后,你应当有了一个带菜单的主窗口(它什么也不干):

   

   

 

Your source file should look like this now: mosaic.asm

现在你的资源文件应该是这样:mosaic.asm

本文地址:http://com.8s8s.com/it/it29706.htm