[翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续二)
来源:互联网 发布:中信建投证券mac版 编辑:程序博客网 时间:2024/05/16 23:02
清单4-1:CtlView 程序
CtlView.h
//======================================================================
// Header file
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))
//----------------------------------------------------------------------
// Generic defines and data types
//
struct decodeUINT { // Structure associates
UINT Code; // messages
// with a function.
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
};
struct decodeCMD { // Structure associates
UINT Code; // menu IDs with a
LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function.
};
//----------------------------------------------------------------------
// Generic defines used by application
#define IDI_BTNICON 20 // Icon used on button
#define ID_ICON 1 // Icon ID
#define IDC_CMDBAR 2 // Command bar ID
#define IDC_RPTLIST 3 // Report window ID
// Client window IDs go from 5 through 9.
#define IDC_WNDSEL 5 // Starting client
// window IDs
// Radio button IDs go from 10 through 14.
#define IDC_RADIOBTNS 10 // Starting ID of
// radio buttons
// Button window defines
#define IDC_PUSHBTN 100
#define IDC_CHKBOX 101
#define IDC_ACHKBOX 102
#define IDC_A3STBOX 103
#define IDC_RADIO1 104
#define IDC_RADIO2 105
#define IDC_OWNRDRAW 106
// Edit window defines
#define IDC_SINGLELINE 100
#define IDC_MULTILINE 101
#define IDC_PASSBOX 102
// List box window defines
#define IDC_COMBOBOX 100
#define IDC_SNGLELIST 101
#define IDC_MULTILIST 102
// Static control window defines
#define IDC_LEFTTEXT 100
#define IDC_RIGHTTEXT 101
#define IDC_CENTERTEXT 102
#define IDC_ICONCTL 103
#define IDC_BITMAPCTL 104
// Scroll bar window defines
#define IDC_LRSCROLL 100
#define IDC_UDSCROLL 101
// User-defined message to add a line to the window
#define MYMSG_ADDLINE (WM_USER + 10)
typedef struct {
TCHAR *szClass;
int nID;
TCHAR *szTitle;
int x;
int y;
int cx;
int cy;
DWORD lStyle;
} CTLWNDSTRUCT, *PCTLWNDSTRUCT;
typedef struct {
WORD wMsg;
int nID;
WPARAM wParam;
LPARAM lParam;
} CTLMSG, * PCTLMSG;
typedef struct {
TCHAR *pszLabel;
WORD wNotification;
} NOTELABELS, *PNOTELABELS;
//----------------------------------------------------------------------
// Function prototypes
//
HWND InitInstance (HINSTANCE, LPWSTR, int);
int TermInstance (HINSTANCE, int);
// Window procedures
LRESULT CALLBACK FrameWndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ClientWndProc (HWND, UINT, WPARAM, LPARAM);
// Message handlers
LRESULT DoCreateFrame (HWND, UINT, WPARAM, LPARAM);
LRESULT DoSizeFrame (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommandFrame (HWND, UINT, WPARAM, LPARAM);
LRESULT DoAddLineFrame (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyFrame (HWND, UINT, WPARAM, LPARAM);
//----------------------------------------------------------------------
// Window prototypes and defines for BtnWnd
//
#define BTNWND TEXT ("ButtonWnd")
int InitBtnWnd (HINSTANCE);
// Window procedures
LRESULT CALLBACK BtnWndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCreateBtnWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCtlColorBtnWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommandBtnWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDrawItemBtnWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoMeasureItemBtnWnd (HWND, UINT, WPARAM, LPARAM);
//----------------------------------------------------------------------
// Window prototypes and defines for EditWnd
//
#define EDITWND TEXT ("EditWnd")
int InitEditWnd (HINSTANCE);
// Window procedures
LRESULT CALLBACK EditWndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCreateEditWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommandEditWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDrawItemEditWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoMeasureItemEditWnd (HWND, UINT, WPARAM, LPARAM);
//----------------------------------------------------------------------
// Window prototypes and defines for ListWnd
//
#define LISTWND TEXT ("ListWnd")
int InitListWnd (HINSTANCE);
// Window procedures
LRESULT CALLBACK ListWndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCreateListWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommandListWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDrawItemListWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoMeasureItemListWnd (HWND, UINT, WPARAM, LPARAM);
//----------------------------------------------------------------------
// Window prototypes and defines for StatWnd
//
#define STATWND TEXT ("StaticWnd")
int InitStatWnd (HINSTANCE);
// Window procedures
LRESULT CALLBACK StatWndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCreateStatWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommandStatWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDrawItemStatWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoMeasureItemStatWnd (HWND, UINT, WPARAM, LPARAM);
//----------------------------------------------------------------------
// Window prototypes and defines ScrollWnd
//
#define SCROLLWND TEXT ("ScrollWnd")
int InitScrollWnd (HINSTANCE);
// Window procedures
LRESULT CALLBACK ScrollWndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT DoCreateScrollWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoVScrollScrollWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoHScrollScrollWnd (HWND, UINT, WPARAM, LPARAM);
CtlView.cpp
//======================================================================
// CtlView - Lists the available fonts in the system.
//
// 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 "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_SIZE, DoSizeFrame,
WM_COMMAND, DoCommandFrame,
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)
// 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 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;
}
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续二)
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续五)
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续三)
- [翻译]-WinCE 程序设计 (3rd 版)--1.5 Windows应用程序剖析
- [翻译]-Windows CE 程序设计 (3rd 版)--5.2 公共控件(九) (续)
- [翻译]-Windows CE 程序设计 (3rd 版)--5.2 公共控件(二)
- [翻译]-Windows CE 程序设计 (3rd 版)--5.2 公共控件(二)
- [翻译]-WinCE 程序设计 (3rd 版)--3.2 鼠标和触摸屏(续)
- [翻译]-WinCE 程序设计 (3rd 版)--2.2 输出文本(续)
- [翻译]-WinCE 程序设计 (3rd 版)--第4章 窗口、控件和菜单 --概述
- [翻译]-WinCE 程序设计 (3rd 版)--第一章 Hello Windows CE-概述
- [翻译]-WinCE 程序设计 (3rd 版)--1.1 Windows CE有什么不同之处
- [翻译]-WinCE 程序设计 (3rd 版)--1.2 依然是Windows编程
- [翻译]-WinCE 程序设计 (3rd 版)--1.3 第一个Windows CE程序
- [翻译]-WinCE 程序设计 (3rd 版)--1.3 第一个Windows CE程序
- [翻译]-WinCE 程序设计 (3rd 版)--1.4 Hello2
- JSON 入门指南
- 增加 MsFlexGrid 的编辑功能
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续五)
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续三)
- firefox插件 10.17 2008
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续二)
- [翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件
- [翻译]-WinCE 程序设计 (3rd 版)--4.2 窗口管理函数
- C/C++中枚举类型(enum)
- dojo1.1.0学习总结--简单登陆案例
- [翻译]-WinCE 程序设计 (3rd 版)--4.1 子窗口
- dojo1.1.0学习总结--注册案例
- [翻译]-WinCE 程序设计 (3rd 版)--3.2 鼠标和触摸屏(续)
- [翻译]-WinCE 程序设计 (3rd 版)--3.2 鼠标和触摸屏