📄 ctlview.cpp
字号:
//======================================================================
// CtlView - Control message demo application.
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include "CtlView.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("CtlView");
HINSTANCE hInst; // Program instance handle
// Message dispatch table for FrameWindowProc
const struct decodeUINT FrameMessages[] = {
WM_CREATE, DoCreateFrame,
WM_COMMAND, DoCommandFrame,
WM_SIZE, DoSizeFrame,
MYMSG_ADDLINE, DoAddLineFrame,
WM_DESTROY, DoDestroyFrame,
};
typedef struct {
TCHAR *szTitle;
INT nID;
TCHAR *szCtlWnds;
HWND hWndClient;
} RBTNDATA;
// Text for main window radio buttons
TCHAR *szBtnTitle[] = {TEXT ("Buttons"), TEXT ("Edit"), TEXT ("List"),
TEXT ("Static"), TEXT ("Scroll")};
// Class names for child windows containing controls
TCHAR *szCtlWnds[] = {BTNWND, EDITWND, LISTWND, STATWND, SCROLLWND};
INT nWndSel = 0;
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
HWND hwndFrame;
// Initialize application.
hwndFrame = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndFrame == 0)
return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc;
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// 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 frame window class.
wc.style = 0; // Window style
wc.lpfnWndProc = FrameWndProc; // 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) GetSysColorBrush (COLOR_STATIC);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Initialize client window classes
if (InitBtnWnd (hInstance) != 0) return 0;
if (InitEditWnd (hInstance) != 0) return 0;
if (InitListWnd (hInstance) != 0) return 0;
if (InitStatWnd (hInstance) != 0) return 0;
if (InitScrollWnd (hInstance) != 0) return 0;
// Create frame window.
hWnd = CreateWindowEx (WS_EX_NODRAG, szAppName,
TEXT ("Control View"), WS_VISIBLE |
WS_CAPTION | WS_SYSMENU,
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) {
return nDefRC;
}
//======================================================================
// Message handling procedures for FrameWindow
//
//----------------------------------------------------------------------
// FrameWndProc - Callback function for application window
//
LRESULT CALLBACK FrameWndProc (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(FrameMessages); i++) {
if (wMsg == FrameMessages[i].Code)
return (*FrameMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateFrame - Process WM_CREATE message for window.
//
LRESULT DoCreateFrame (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HWND hwndChild;
INT i;
// Set currently viewed window
nWndSel = 0;
// Create the radio buttons.
for (i = 0; i < dim(szBtnTitle); i++) {
hwndChild = CreateWindow (TEXT ("BUTTON"),
szBtnTitle[i], BS_AUTORADIOBUTTON |
WS_VISIBLE | WS_CHILD, 0,
0, 80, 20, hWnd,
(HMENU)(IDC_RADIOBTNS+i), hInst, NULL);
// Destroy frame if window not created.
if (!IsWindow (hwndChild)) {
DestroyWindow (hWnd);
break;
}
}
// Create report window. Size it so that it fits either on the right
// or below the control windows, depending on the size of the screen.
hwndChild = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("listbox"),
TEXT (""), WS_VISIBLE | WS_CHILD | WS_VSCROLL |
LBS_USETABSTOPS | LBS_NOINTEGRALHEIGHT, 0, 0,
100, 100,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 = 24;
SendMessage (hwndChild, LB_SETTABSTOPS, 1, (LPARAM)&i);
// Create the child windows. Size them so that they fit under
// the command bar and fill the left side of the child area.
for (i = 0; i < dim(szCtlWnds); i++) {
hwndChild = CreateWindowEx (WS_EX_CLIENTEDGE, szCtlWnds[i],
TEXT (""), WS_CHILD, 0, 0, 200, 200, hWnd,
(HMENU)(IDC_WNDSEL+i), hInst, NULL);
// Destroy frame if client window not created.
if (!IsWindow (hwndChild)) {
DestroyWindow (hWnd);
return 0;
}
}
// Check one of the auto radio buttons.
SendDlgItemMessage (hWnd, IDC_RADIOBTNS+nWndSel, BM_SETCHECK, 1, 0);
hwndChild = GetDlgItem (hWnd, IDC_WNDSEL+nWndSel);
ShowWindow (hwndChild, SW_SHOW);
return 0;
}
//----------------------------------------------------------------------
// DoSizeFrame - Process WM_SIZE message for window.
//
LRESULT DoSizeFrame (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
int nWidth, nHeight;
int i, x, y, cx, cy;
BOOL bWide = TRUE;
RECT rect;
GetWindowRect (hWnd, &rect);
GetClientRect (hWnd, &rect);
// These arrays are used to adjust between wide and narrow screens.
POINT ptRBtnsN[] = {{5,0}, {90,0}, {180,0}, {5,20}, {90,20}};
POINT ptRBtnsW[] = {{5,0}, {90,0}, {180,0}, {270,0}, {360,0}};
LPPOINT pptRbtns = ptRBtnsW;
nWidth = LOWORD (lParam);
nHeight = HIWORD (lParam);
// Use different layouts for narrow (Pocket PC) screens.
if (GetSystemMetrics (SM_CXSCREEN) < 480) {
pptRbtns = ptRBtnsN;
bWide = FALSE;
}
// Move the radio buttons.
for (i = 0; i < dim(szBtnTitle); i++)
SetWindowPos (GetDlgItem (hWnd, IDC_RADIOBTNS+i), 0,
pptRbtns[i].x, pptRbtns[i].y,
0, 0, SWP_NOSIZE | SWP_NOZORDER);
// Size report window so that it fits either on the right or
// below the control windows, depending on the size of the screen.
x = bWide ? nWidth/2 : 0;
y = bWide ? 20 : (nHeight)/2 + 40;
cx = bWide ? nWidth/2 : nWidth;
cy = nHeight - y;
SetWindowPos (GetDlgItem (hWnd, IDC_RPTLIST), 0, x, y, cx, cy,
SWP_NOZORDER);
// Size the child windows so that they fit under
// the command bar and fill the left side of the child area.
x = 0;
y = bWide ? 20 : 40;
cx = bWide ? nWidth/2 : nWidth;
cy = bWide ? nHeight : (nHeight)/2+40;
for (i = 0; i < dim(szCtlWnds); i++)
SetWindowPos (GetDlgItem (hWnd, IDC_WNDSEL+i), 0, x, y, cx, cy,
SWP_NOZORDER);
return 0;
}
//----------------------------------------------------------------------
// DoCommandFrame - Process WM_COMMAND message for window.
//
LRESULT DoCommandFrame (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HWND hwndTemp;
int nBtn;
// Don't look at list box messages.
if (LOWORD (wParam) == IDC_RPTLIST)
return 0;
nBtn = LOWORD (wParam) - IDC_RADIOBTNS;
if (nWndSel != nBtn) {
// Hide the currently visible window.
hwndTemp = GetDlgItem (hWnd, IDC_WNDSEL+nWndSel);
ShowWindow (hwndTemp, SW_HIDE);
// Save the current selection.
nWndSel = nBtn;
// Show the window selected via the radio button.
hwndTemp = GetDlgItem (hWnd, IDC_WNDSEL+nWndSel);
ShowWindow (hwndTemp, SW_SHOW);
}
return 0;
}
//----------------------------------------------------------------------
// DoAddLineFrame - Process MYMSG_ADDLINE message for window.
//
LRESULT DoAddLineFrame (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
TCHAR szOut[128];
INT i;
if (LOWORD (wParam) == 0xffff)
wsprintf (szOut, TEXT (" \t %s"), (LPTSTR)lParam);
else
wsprintf (szOut, TEXT ("id:%3d \t %s"), LOWORD (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;
}
//----------------------------------------------------------------------
// DoDestroyFrame - Process WM_DESTROY message for window.
//
LRESULT DoDestroyFrame (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
//----------------------------------------------------------------------
// PrintCmdMessage - Prints command message data to report window
//
LRESULT PrintCmdMessage (HWND hWnd, WPARAM wParam, LPARAM lParam,
PNOTELABELS lpStruct, int nDim) {
int i;
for (i = 0; i < nDim; i++) {
if (HIWORD (wParam) == lpStruct[i].wNotification) {
RptMessage (hWnd, wParam, TEXT("%s"), lpStruct[i].pszLabel);
break;
}
}
if (i == nDim)
RptMessage (hWnd, wParam, TEXT("notification: %x"),
HIWORD (wParam));
return 0;
}
//----------------------------------------------------------------------
// RptMessage - Add string to the report list box.
//
void RptMessage (HWND hWnd, DWORD id, LPTSTR lpszFormat, ...) {
TCHAR szBuffer[512];
va_list args;
va_start(args, lpszFormat);
StringCchVPrintf(szBuffer, dim (szBuffer),lpszFormat, args);
va_end(args);
SendMessage (GetParent (hWnd), MYMSG_ADDLINE, id,(LPARAM)szBuffer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -