📄 dlgdemo.cpp
字号:
//======================================================================
// DlgDemo - Dialog box demonstration
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 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
#include <aygshell.h> // Add extended shell includes
#pragma comment( lib, "aygshell" ) // Link extended shell API
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
#define WINMOBILE 1
#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
HWND hwndMain; // Handle to main window
#ifdef WINMOBILE
typedef BOOL (APIENTRY* LFPRINTDLG) (LPPRINTDLG lppsd);
LFPRINTDLG lpfnPrintDlg = 0; // Ptr to print common dialog fn
typedef BOOL (APIENTRY* LFCHOOSEFONTPROC) (PVOID);
FARPROC lpfnChooseFontDlg = 0; // Choose Font
#else
typedef BOOL (APIENTRY* LFPAGESETUPDLG)( LPPAGESETUPDLGW );
LFPAGESETUPDLG lpfnPrintDlg = 0; // Ptr to print common dialog fn
typedef BOOL (APIENTRY* LFCHOOSEFONTPROC) (LPCHOOSEFONT);
LFCHOOSEFONTPROC lpfnChooseFontDlg = 0; // Choose Font
#endif
typedef BOOL (APIENTRY* LFCHOOSECOLORPROC) (LPCHOOSECOLOR );
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_SHOWSCROLLABLE, DoMainCommandScrollable,
IDM_SHOWMODELESS, DoMainCommandModeless,
IDM_COLOR, DoMainCommandColor,
IDM_PRINT, DoMainCommandPrint,
IDM_FONT, DoMainCommandFont,
IDM_EXIT, DoMainCommandExit,
IDM_ABOUT, DoMainCommandAbout,
};
//
// Labels for WM_NOTIFY notifications
//
NOTELABELS nlPropPage[] = {{TEXT ("PSN_SETACTIVE "), PSN_SETACTIVE },
{TEXT ("PSN_KILLACTIVE "), PSN_KILLACTIVE },
{TEXT ("PSN_APPLY "), PSN_APPLY },
{TEXT ("PSN_RESET "), PSN_RESET },
{TEXT ("PSN_HELP "), PSN_HELP },
{TEXT ("PSN_WIZBACK "), PSN_WIZBACK },
{TEXT ("PSN_WIZNEXT "), PSN_WIZNEXT },
{TEXT ("PSN_WIZFINISH "), PSN_WIZFINISH },
{TEXT ("PSN_QUERYCANCEL"), PSN_QUERYCANCEL},
{TEXT ("NM_OUTOFMEMORY "), NM_OUTOFMEMORY },
{TEXT ("NM_CLICK "), NM_CLICK },
{TEXT ("NM_DBLCLK "), NM_DBLCLK },
{TEXT ("NM_RETURN "), NM_RETURN },
{TEXT ("NM_RCLICK "), NM_RCLICK },
{TEXT ("NM_RDBLCLK "), NM_RDBLCLK },
{TEXT ("NM_SETFOCUS "), NM_SETFOCUS },
{TEXT ("NM_KILLFOCUS "), NM_KILLFOCUS },
{TEXT ("NM_CUSTOMDRAW "), NM_CUSTOMDRAW },
{TEXT ("NM_HOVER "), NM_HOVER },
{TEXT ("NM_NCHITTEST "), NM_NCHITTEST },
{TEXT ("NM_KEYDOWN "), NM_KEYDOWN },
};
//======================================================================
// Program entry point
//
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;
INITCOMMONCONTROLSEX iccx;
// Save program instance handle in global variable.
hInst = hInstance;
memset (&iccx, 0, sizeof (iccx));
iccx.dwSize = sizeof (iccx);
iccx.dwICC = ICC_DATE_CLASSES | ICC_WIN95_CLASSES | ICC_COOL_CLASSES;
InitCommonControlsEx (&iccx);
#ifdef WINMOBILE
// For Windows Mobile devices, allow only one instance of the app
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"));
#ifdef WINMOBILE
lpfnPrintDlg = (LFPRINTDLG)GetProcAddress (hLib, TEXT ("PrintDlg"));
#else
lpfnPrintDlg = (LFPAGESETUPDLG)GetProcAddress (hLib,
TEXT ("PageSetupDlgW"));
lpfnChooseFontDlg = (LFCHOOSEFONTPROC)GetProcAddress (hLib,
TEXT ("ChooseFontW"));
#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.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HWND hwndChild;
INT i, nHeight = 0;
LPCREATESTRUCT lpcs;
HMENU hMenu;
#ifdef WINMOBILE
SHMENUBARINFO mbi; // For WinMobile, 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);
if (!lpfnChooseFontDlg)
EnableMenuItem (hMenu, IDM_FONT, 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.
wsprintf (szOut, TEXT ("%s"), (LPTSTR)lParam);
} else {
// If no ID val, ignore that field.
if (LOWORD (wParam) == 0xffff)
// Print prop page and message.
wsprintf (szOut, TEXT (" \t %s"),
(LPTSTR)lParam);
else
// Print property page, control ID, and message.
wsprintf (szOut, TEXT ("%3d \t %s"),
wParam, (LPTSTR)lParam);
}
i = SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)szOut);
if (i != LB_ERR)
SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_SETTOPINDEX, i,
(LPARAM)(LPCTSTR)szOut);
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
//----------------------------------------------------------------------
// DoMainCommandOpen - Process File Open command
//
LPARAM DoMainCommandOpen (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
OPENFILENAME of;
TCHAR szFileName [MAX_PATH] = {0};
const LPTSTR pszOpenFilter = TEXT ("All Documents (*.*)\0*.*\0\0");
INT rc;
szFileName[0] = '\0'; // Initialize filename.
memset (&of, 0, sizeof (of)); // Initialize File Open structure.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -