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

📄 dlgdemo.cpp

📁 wince 6 dilag example based on vs2005
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//======================================================================
// DlgDemo - Dialog box demonstration
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include <windows.h>                 // For all that Windows stuff
#include <commctrl.h>                // Command bar includes
#include <commdlg.h>                 // Common dialog box includes
#include <prsht.h>                   // Property sheet includes

#include "DlgDemo.h"                 // Program-specific stuff
#if defined(WIN32_PLATFORM_PSPC)
#include <aygshell.h>                 // Add Pocket PC includes
#pragma comment( lib, "aygshell" )     // Link Pocket PC lib for menu bar
#endif
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("DlgDemo");
HINSTANCE hInst;                     // Program instance handle
HWND g_hwndMlDlg = 0;                // Handle to modeless dialog box

HINSTANCE hLib = 0;                  // Handle to CommDlg lib
typedef BOOL (APIENTRY* LFCHOOSECOLORPROC) (LPCHOOSECOLOR );

#ifndef WIN32_PLATFORM_PSPC
typedef BOOL (APIENTRY* LFPAGESETUPDLG)( LPPAGESETUPDLGW );
LFPAGESETUPDLG lpfnPrintDlg = 0;        // Ptr to print common dialog fn
#else
typedef BOOL (APIENTRY* LFPRINTDLG) (LPPRINTDLG lppsd);
LFPRINTDLG lpfnPrintDlg = 0;        // Ptr to print common dialog fn
#endif
LFCHOOSECOLORPROC lpfnChooseColor = 0;  // Ptr to color common dialog fn

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
    WM_CREATE, DoCreateMain,
    WM_COMMAND, DoCommandMain,
    MYMSG_ADDLINE, DoAddLineMain,
    WM_DESTROY, DoDestroyMain,
};

// Command message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
    IDM_OPEN, DoMainCommandOpen,
    IDM_SAVE, DoMainCommandSave,
    IDM_SHOWPROPSHEET, DoMainCommandShowProp,
    IDM_SHOWMODELESS, DoMainCommandModeless,
    IDM_COLOR, DoMainCommandColor,
    IDM_PRINT, DoMainCommandPrint,
    IDM_EXIT, DoMainCommandExit,
    IDM_ABOUT, DoMainCommandAbout,
};
//
// Labels for WM_NOTIFY notifications
//
NOTELABELS nlPropPage[] = {{TEXT ("PSN_SETACTIVE  "), (PSN_FIRST-0)},
                           {TEXT ("PSN_KILLACTIVE "), (PSN_FIRST-1)},
                           {TEXT ("PSN_APPLY      "), (PSN_FIRST-2)},
                           {TEXT ("PSN_RESET      "), (PSN_FIRST-3)},
                           {TEXT ("PSN_HASHELP    "), (PSN_FIRST-4)},
                           {TEXT ("PSN_HELP       "), (PSN_FIRST-5)},
                           {TEXT ("PSN_WIZBACK    "), (PSN_FIRST-6)},
                           {TEXT ("PSN_WIZNEXT    "), (PSN_FIRST-7)},
                           {TEXT ("PSN_WIZFINISH  "), (PSN_FIRST-8)},
                           {TEXT ("PSN_QUERYCANCEL"), (PSN_FIRST-9)},
};
int nPropPageSize = dim(nlPropPage);

// Labels for the property pages
TCHAR *szPages[] = {TEXT ("Btn "),
                    TEXT ("Edit"),
                    TEXT ("List"),
                    TEXT ("Stat"),
                    TEXT ("Scrl"),
};
//======================================================================
// Program entry point
//
HWND hwndMain;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPWSTR lpCmdLine, int nCmdShow) {
    MSG msg;
    int rc = 0;

    // Initialize application.
    hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
    if (hwndMain == 0) return 0x10;
    // Application message loop
    while (GetMessage (&msg, NULL, 0, 0)) {
        // If modeless dialog box is created, let it have
        // the first crack at the message.
        if ((g_hwndMlDlg == 0) ||
            (!IsDialogMessage (g_hwndMlDlg, &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;

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

#if defined(WIN32_PLATFORM_PSPC)
    // If Pocket PC, allow only one instance of the application.
    hWnd = FindWindow (szAppName, NULL);
    if (hWnd) {
        SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));    
        return 0;
    }
#endif
    // Register application main window class.
    wc.style = 0;                             // 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;

    // Get the Color and print dialog function pointers.
    hLib = LoadLibrary (TEXT ("COMMDLG.DLL"));
    if (hLib) {
        lpfnChooseColor = (LFCHOOSECOLORPROC)GetProcAddress (hLib, 
		                                          TEXT ("ChooseColor"));
#if defined(WIN32_PLATFORM_PSPC)
        lpfnPrintDlg = (LFPRINTDLG)GetProcAddress (hLib, TEXT ("PrintDlg"));
#else
        lpfnPrintDlg = (LFPAGESETUPDLG)GetProcAddress (hLib, 
		                                            TEXT ("PageSetupDlgW"));
#endif
    }
    // Create main window.
    hWnd = CreateWindow (szAppName, TEXT ("Dialog Demo"), WS_VISIBLE,
                         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                         CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    // Return fail code if window not created.
    if (!IsWindow (hWnd)) return 0;

    // Standard show and update calls
    ShowWindow (hWnd, nCmdShow);
    UpdateWindow (hWnd);
    return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
    if (hLib)
        FreeLibrary (hLib);
    return nDefRC;
}
//======================================================================
// Message-handling procedures for MainWindow
//
//----------------------------------------------------------------------
// 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.
//
//#define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), 
//    SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU);
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                      LPARAM lParam) {
    HWND hwndChild;
    INT i, nHeight = 0;
    LPCREATESTRUCT lpcs;
    HMENU hMenu;

#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
    SHMENUBARINFO mbi;                      // For Pocket PC, create
    memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we
    mbi.cbSize = sizeof(SHMENUBARINFO);     // have a sip button
    mbi.hwndParent = hWnd;
    mbi.nToolBarId = ID_MENU;
    mbi.hInstRes = hInst;
    SHCreateMenuBar(&mbi);
    hMenu = (HMENU)SendMessage(mbi.hwndMB, SHCMBM_GETSUBMENU, 0, 100);
#else
    // Create a command bar. Add a menu and an exit button.
    HWND hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
    CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);
    CommandBar_AddAdornments (hwndCB, 0, 0);
    nHeight = CommandBar_Height (hwndCB);
    hMenu = CommandBar_GetMenu (hwndCB, 0);
#endif
    // Convert lParam to pointer to create structure.
    lpcs = (LPCREATESTRUCT) lParam;

    // See color and print functions not found; disable menus.
    if (!lpfnChooseColor)
        EnableMenuItem (hMenu, IDM_COLOR, MF_BYCOMMAND | MF_GRAYED);
    if (!lpfnPrintDlg)
        EnableMenuItem (hMenu, IDM_PRINT, MF_BYCOMMAND | MF_GRAYED);
    //
    // Create report window. Size it so that it fits under
    // the command bar and fills the remaining client area.
    //
    hwndChild = CreateWindowEx (0, TEXT ("listbox"),
                       TEXT (""), WS_VISIBLE | WS_CHILD | WS_VSCROLL |
                       LBS_USETABSTOPS | LBS_NOINTEGRALHEIGHT, 0,
                       nHeight, lpcs->cx, lpcs->cy - nHeight,
                       hWnd, (HMENU)IDC_RPTLIST, lpcs->hInstance, 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);
    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;

    // 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;
}
//----------------------------------------------------------------------
// DoAddLineMain - Process MYMSG_ADDLINE message for window.
//
LRESULT DoAddLineMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    TCHAR szOut[128];
    INT i;

    // If nothing in wParam, just fill in spaces.
    if (wParam == -1) {
        // Print message only.
        lstrcpy (szOut, (LPTSTR)lParam);
    } else {
        // If no ID val, ignore that field.
        if (LOWORD (wParam) == 0xffff)
            // Print prop page and message.
            wsprintf (szOut, TEXT ("%s      \t %s"),
                      szPages[HIWORD (wParam) - ID_BTNPAGE],
                      (LPTSTR)lParam);
        else

⌨️ 快捷键说明

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