📄 dlgdemo.c
字号:
//======================================================================
// DlgDemo - Dialog box demonstration
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 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
//----------------------------------------------------------------------
// 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
FARPROC lpfnChooseColor = 0; // Ptr to color common dialog fn
FARPROC lpfnPrintDlg = 0; // Ptr to print 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 ("Button"),
TEXT ("Edit "),
TEXT ("List "),
TEXT ("Static"),
TEXT ("Scroll"),
};
//======================================================================
// Program entry point
//
HWND hwndMain;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
// Initialize application.
rc = InitApp (hInstance);
if (rc) return rc;
// Initialize this instance.
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);
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
// 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 = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 1;
// Get the Color and print dialog function pointers.
hLib = LoadLibrary (TEXT ("COMMDLG.DLL"));
if (hLib) {
lpfnChooseColor = GetProcAddress (hLib, TEXT ("ChooseColor"));
lpfnPrintDlg = GetProcAddress (hLib, TEXT ("PrintDlg"));
}
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT ("Dialog Demo"), // Window title
WS_VISIBLE, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
// 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 hwndCB, hwndChild;
INT i, nHeight;
LPCREATESTRUCT lpcs;
HMENU hMenu;
// Convert lParam into pointer to create structure.
lpcs = (LPCREATESTRUCT) lParam;
// Create a command bar.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Add the menu.
CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
// See color and print functions not found, disable menus.
hMenu = CommandBar_GetMenu (hwndCB, 0);
if (!lpfnChooseColor)
EnableMenuItem (hMenu, IDM_COLOR, MF_BYCOMMAND | MF_GRAYED);
if (!lpfnPrintDlg)
EnableMenuItem (hMenu, IDM_PRINT, MF_BYCOMMAND | MF_GRAYED);
nHeight = CommandBar_Height (hwndCB);
//
// 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 = 40;
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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -