⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menubar.cpp

📁 《Windows CE 6.0开发者参考》(《Programming Windows Embedded CE 6.0 Developer Reference》)第四版书中的源代码
💻 CPP
字号:
//======================================================================
// MenuBar - Demonstrates a menu bar control
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h>                 // For all that Windows stuff
#include <commctrl.h>                // Common Control includes
#include <aygshell.h>                // Extended shell includes
#include "MenuBar.h"                 // Program-specific stuff

#pragma comment( lib, "aygshell" )   // Link extended shell API
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("MenuBar");
HINSTANCE hInst;                     // Program instance handle

HWND hwndMenuBar = NULL;             // Handle of menu bar control
SHACTIVATEINFO sai;                  // Used to adjust window for SIP

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
    WM_CREATE, DoCreateMain,
    WM_SIZE, DoSizeMain,
    WM_COMMAND, DoCommandMain,
    WM_NOTIFY, DoNotifyMain,
    WM_SETTINGCHANGE, DoSettingChangeMain,
    WM_ACTIVATE, DoActivateMain,
    WM_DESTROY, DoDestroyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
    IDM_EXIT, DoMainCommandExit,
    IDM_BARONE, DoMainCommandCreateBar1,
    IDM_BARTWO, DoMainCommandCreateBar2,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPWSTR lpCmdLine, int nCmdShow) {
    MSG msg;
    int rc = 0;
    HWND hwndMain;
    HACCEL hAccel;

    // Initialize application.
    hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
    if (hwndMain == 0) return 0x10;

    hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE (ID_ACCEL));

    // Application message loop
    while (GetMessage (&msg, NULL, 0, 0)) {

        // Translate accelerator keys.
        if (!TranslateAccelerator(hwndMain, hAccel, &msg)) {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }
    // Instance cleanup
    return TermInstance (hInstance, msg.wParam);
 }
//------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
    HWND hWnd;
    WNDCLASS wc;

    // Allow only one instance of the application.
    hWnd = FindWindow (szAppName, NULL);
    if (hWnd) {
        SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));    
        return 0;
    }
    // Register application main window class.
    wc.style = CS_VREDRAW | CS_HREDRAW;       // Window style
    wc.lpfnWndProc = MainWndProc;             // Callback function
    wc.cbClsExtra = 0;                        // Extra class data
    wc.cbWndExtra = 0;                        // Extra window data
    wc.hInstance = hInstance;                 // Owner handle
    wc.hIcon = NULL,                          // Application icon
    wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Default cursor
    wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wc.lpszMenuName =  NULL;                  // Menu name
    wc.lpszClassName = szAppName;             // Window class name

    if (RegisterClass (&wc) == 0) return 0;

    // Save program instance handle in global variable.
    hInst = hInstance;

    // Create main window.
    hWnd = CreateWindow (szAppName, TEXT("Menu Bar"),  WS_VISIBLE,
                         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                         CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    if (!IsWindow (hWnd)) return 0;           // Fail if not created.

    // Standard show and update calls
    ShowWindow (hWnd, nCmdShow);
    UpdateWindow (hWnd);
    return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
    return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                              LPARAM lParam) {
    INT i;
    //
    // Search message list to see if we need to handle this
    // message. If in list, call procedure.
    //
    for (i = 0; i < dim(MainMessages); i++) {
        if (wMsg == MainMessages[i].Code)
            return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
    }
    return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                      LPARAM lParam) {
    SIPINFO si;
    HWND hwndChild;
    int i, cx, cy;

    // Initialize the shell to activate info structure.
    memset (&sai, 0, sizeof (sai));
    sai.cbSize = sizeof (sai);

    // Create menu bar and check for errors.
    hwndMenuBar = MyCreateMenuBar (hWnd, ID_TOOLBAR1);
    if (!hwndMenuBar) {
        MessageBox (hWnd, TEXT("Couldn\'t create MenuBar"), 
                    szAppName, MB_OK);
        DestroyWindow (hWnd);
        return 0;
    }
    // Set menu check mark.
    MyCheckMenu (IDM_BARONE);          

    // Create report window.  It will be sized in the WM_SIZE handler.
    hwndChild = CreateWindowEx (0, TEXT ("listbox"), TEXT (""), 
                                WS_VISIBLE | WS_CHILD | WS_VSCROLL |
                                LBS_USETABSTOPS | LBS_NOINTEGRALHEIGHT,
                                0, 0, 0, 0, hWnd, (HMENU)IDC_RPTLIST, 
                                hInst, NULL);
    // Destroy frame if window not created.
    if (!IsWindow (hwndChild)) {
        DestroyWindow (hWnd);
        return 0;
    }
    // Initialize tab stops for display list box.
    i = 8;
    SendMessage (hwndChild, LB_SETTABSTOPS, 1, (LPARAM)&i);

    // Query the sip state and size our window appropriately.
    memset (&si, 0, sizeof (si));
    si.cbSize = sizeof (si);
    SHSipInfo(SPI_GETSIPINFO, 0, (PVOID)&si, FALSE); 
    cx = si.rcVisibleDesktop.right - si.rcVisibleDesktop.left;
    cy = si.rcVisibleDesktop.bottom - si.rcVisibleDesktop.top;

    // If the sip is not shown, or is showing but not docked, the
    // desktop rect doesn't include the height of the menu bar.
    if (!(si.fdwFlags & SIPF_ON) ||
        ((si.fdwFlags & SIPF_ON) && !(si.fdwFlags & SIPF_DOCKED))) {
        RECT rectMB;
        GetWindowRect (hwndMenuBar, &rectMB);
        cy -= (rectMB.bottom - rectMB.top);  
    }    
    SetWindowPos (hWnd, NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER);
    return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){
    RECT rect;

    GetClientRect (hWnd, &rect);
    SetWindowPos (GetDlgItem (hWnd, IDC_RPTLIST), NULL, 0, 0,
                  rect.right - rect.left, rect.bottom - rect.top,
                  SWP_NOZORDER);
    return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    WORD    idItem, wNotifyCode;
    HWND hwndCtl;
    INT  i;

    // Parse the parameters.
    idItem = (WORD) LOWORD (wParam);
    wNotifyCode = (WORD) HIWORD (wParam);
    hwndCtl = (HWND) lParam;

    Add2List (hWnd, TEXT ("WM_COMMAND \tid:%d \tcode:%d"), idItem, 
              wNotifyCode);
    // Call routine to handle control message.
    for (i = 0; i < dim(MainCommandItems); i++) {
        if (idItem == MainCommandItems[i].Code)
            return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
                                              wNotifyCode);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoNotifyMain - Process WM_NOTIFY message for window.
//
LRESULT DoNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                      LPARAM lParam) {
    LPNMHDR lpnhr = (LPNMHDR)lParam;

    Add2List (hWnd, TEXT ("WM_NOTIFY \t\tid:%d \tevent:%d"), 
	          lpnhr->idFrom, lpnhr->code);
   return 0;
}
//----------------------------------------------------------------------
// DoSettingChangeMain - Process WM_SETTINGCHANGE message for window.
//
LRESULT DoSettingChangeMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                             LPARAM lParam) {

    // Notify shell of our WM_SETTINGCHANGE message.
    SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
    return 0;
}
//----------------------------------------------------------------------
// DoActivateMain - Process WM_ACTIVATE message for window.
//
LRESULT DoActivateMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                        LPARAM lParam) {

    // Notify shell of our activate message.
    SHHandleWMActivate(hWnd, wParam, lParam, &sai, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                       LPARAM lParam) {
    PostQuitMessage (0);
    return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    SendMessage (hWnd, WM_CLOSE, 0, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandCreateBar1 - Create a menu bar control
//
LPARAM DoMainCommandCreateBar1 (HWND hWnd, WORD idItem, HWND hwndCtl,
                               WORD wNotifyCode) {
    // Delete the old menu bar.
    if (IsWindow (hwndMenuBar))
        DestroyWindow (hwndMenuBar);

    // Create the menu bar.
    hwndMenuBar = MyCreateMenuBar (hWnd, ID_TOOLBAR1);

    MyCheckMenu (IDM_BARONE);          // Set menu checkmark.
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandCreateBar2 - Create a more complex menu bar.
//
LPARAM DoMainCommandCreateBar2 (HWND hWnd, WORD idItem, HWND hwndCtl,
                               WORD wNotifyCode) {
    if (IsWindow (hwndMenuBar))
        DestroyWindow (hwndMenuBar);

    // Create a menu bar.
    hwndMenuBar = MyCreateMenuBar (hWnd, ID_TOOLBAR2);
    // Add the standard view bitmap.
    CommandBar_AddBitmap (hwndMenuBar, HINST_COMMCTRL, 
                          IDB_STD_SMALL_COLOR, STD_PRINT, 16, 16);
    MyCheckMenu (IDM_BARTWO);
    return 0;
}
//----------------------------------------------------------------------
// MyCreateMenuBar - Creates a menu bar
//
HWND MyCreateMenuBar (HWND hWnd, int idToolbar) {
    SHMENUBARINFO mbi;

    // Create a menu bar.
    memset(&mbi, 0, sizeof(SHMENUBARINFO)); // Zero structure
    mbi.cbSize = sizeof(SHMENUBARINFO);     // Size field
    mbi.hwndParent = hWnd;                  // Parent window
    mbi.nToolBarId = idToolbar;             // ID of toolbar resource
    mbi.hInstRes = hInst;                   // Inst handle of app
    mbi.nBmpId = ID_TOOLBMPS;               // ID of bitmap resource
    mbi.cBmpImages = 3;                     // Num of images in bitmap 

    SHCreateMenuBar(&mbi);
    return mbi.hwndMB;                      // Return the menu bar handle.
}
//----------------------------------------------------------------------
// MyCheckMenu - Places a check next to a menu item
//
void MyCheckMenu (int idMenu) {
    HMENU hSubMenu;

    // The handle for the view menu
    hSubMenu = (HMENU)SendMessage (hwndMenuBar, SHCMBM_GETMENU, 0, 0);
    if (idMenu == IDM_BARTWO) {
        CheckMenuItem (hSubMenu, IDM_BARTWO, MF_BYCOMMAND | 
                       MFS_CHECKED);
        CheckMenuItem (hSubMenu, IDM_BARONE, MF_BYCOMMAND | 
                       MFS_UNCHECKED);
    } else {
        CheckMenuItem (hSubMenu, IDM_BARTWO, MF_BYCOMMAND | 
                       MFS_UNCHECKED);
        CheckMenuItem (hSubMenu, IDM_BARONE, MF_BYCOMMAND | 
                       MFS_CHECKED);
    }
    return;
}
//----------------------------------------------------------------------
// Add2List - Add string to the report list box. 
//
void Add2List (HWND hWnd, LPTSTR lpszFormat, ...) {
    int i;
    TCHAR szBuffer[512];

    va_list args;
    va_start(args, lpszFormat);

	StringCchVPrintf(szBuffer, dim (szBuffer),lpszFormat, args);

    i = SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_ADDSTRING, 0, 
                            (LPARAM)(LPCTSTR)szBuffer);

    if (i != LB_ERR)
        SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_SETTOPINDEX, i, 
                            (LPARAM)(LPCTSTR)szBuffer);
    va_end(args);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -