当前位置:编程学习 > VC++ >>

VC创建模态对话框

模态窗口的创建由DialogBox()函数。
首先定义几个id:
[cpp]
#define ID_FILE_EXIT 900 
#define ID_STUFF_GO 9002 
#define ID_HELP_ABOUT 9003 
#define IDD_ABOUT     101 
对话框的资源文件如下:
[cpp] 
///////////////////////////////////////////////////////////////////////////// 
// 
// Dialog 
// 
 
IDD_ABOUT DIALOGEX 0, 0, 316, 183 
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 wql", 
                    IDC_STATIC,16,18,144,33 
END 
 
 
///////////////////////////////////////////////////////////////////////////// 

开始创建对话框:
[cpp] 
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; 

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); 
            hIcon = (HICON)LoadImage(NULL, "icon1.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 = (HICON)LoadImage(NULL, "icon1.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 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); 
        &nbs

补充:软件开发 , Vc ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,