?? uidemo.cpp
字號:
// UIdemo.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "UIdemo.h"
#include <commctrl.h>
// Vincent added code here
#include <Commdlg.h>
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE, LPTSTR);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
// Vincent added code here
typedef struct {
TCHAR *pszLabel;
DWORD wNotification;
} NOTELABELS;
LRESULT CALLBACK DialogWndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ModelessDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
void Print(TCHAR *pFormat, ...);
void MenuCommandOpen(HWND hWnd);
HWND gHwndMain;
NOTELABELS nlCombo[] = {{TEXT ("CBN_ERRSPACE "), (-1)},
{TEXT ("CBN_SELCHANGE "), 1},
{TEXT ("CBN_DBLCLK "), 2},
{TEXT ("CBN_SETFOCUS "), 3},
{TEXT ("CBN_KILLFOCUS "), 4},
{TEXT ("CBN_EDITCHANGE "), 5},
{TEXT ("CBN_EDITUPDATE "), 6},
{TEXT ("CBN_DROPDOWN "), 7},
{TEXT ("CBN_CLOSEUP "), 8},
{TEXT ("CBN_SELENDOK "), 9},
{TEXT ("CBN_SELENDCANCEL"), 10},
};
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_UIDEMO);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// It is important to call this function so that the application
// will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_UIDEMO));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name
hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
LoadString(hInstance, IDC_UIDEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance, szWindowClass);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (hwndCB)
CommandBar_Show(hwndCB, TRUE);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
INT nHeight=0;
LPCREATESTRUCT lpcs;
INT i;
HWND hwndChild;
gHwndMain = hWnd;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
Print(TEXT("AboutBox is launched"));
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
Print(TEXT("About Box is Closed"));
break;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
// Vincent added code here
case IDM_OPEN:
Print(TEXT("GetFileDialog is Launched"));
MenuCommandOpen(hWnd);
break;
case IDM_DIALOG:
Print(TEXT("Main dialog is launched"));
DialogBox(hInst, (LPCTSTR)IDD_DIALOG, hWnd, (DLGPROC)DialogWndProc);
Print(TEXT("Main dialog is Closed"));
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
hwndCB = CommandBar_Create(hInst, hWnd, 1);
CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
CommandBar_AddAdornments(hwndCB, 0, 0);
// Vincent added code here
nHeight = CommandBar_Height (hwndCB);
lpcs = (LPCREATESTRUCT) lParam;
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);
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
DrawText(hdc, szHello, _tcslen(szHello), &rt,
DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(hwndCB);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hDlg, &rt1)) {
GetClientRect(GetParent(hDlg), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
//========================================================================================
//=============================== Vincent added code here ================================
//========================================================================================
//=============================== Vincent added code here for callback functions ================================
LRESULT CALLBACK DialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
TCHAR tchBuffer[128];
switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hDlg, &rt1)) {
GetClientRect(GetParent(hDlg), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
SendDlgItemMessage(hDlg, IDC_COMBO1, CB_INSERTSTRING, 0, (LPARAM)(LPCSTR)TEXT("Item 3"));
SendDlgItemMessage(hDlg, IDC_COMBO1, CB_INSERTSTRING, 0, (LPARAM)(LPCSTR)TEXT("Item 2"));
SendDlgItemMessage(hDlg, IDC_COMBO1, CB_INSERTSTRING, 0, (LPARAM)(LPCSTR)TEXT("Item 1"));
SendDlgItemMessage(hDlg, IDC_COMBO1, CB_INSERTSTRING, 0, (LPARAM)(LPCSTR)TEXT("Item 0"));
SendDlgItemMessage(hDlg, IDC_COMBO1, CB_SETCURSEL, 0, 0);
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
else if (LOWORD(wParam) == IDC_BUTTON1)
{
Print(TEXT("Button [Read] is pressed"));
GetDlgItemText(hDlg, IDC_EDIT1, tchBuffer, sizeof(tchBuffer));
SetDlgItemText(hDlg, IDC_STATIC1, tchBuffer);
Print(TEXT("Contents of [Edit box] : %s"), tchBuffer);
}
else if (LOWORD(wParam) == IDC_COMBO1)
{
for (int i = 0; i < dim(nlCombo); i++)
{
if(HIWORD (wParam) == nlCombo[i].wNotification)
{
Print(TEXT("Notification : %s, Combo box [Item %d] is selected"),
nlCombo[i].pszLabel, SendDlgItemMessage (hDlg, IDC_COMBO1, CB_GETCURSEL, 0, 0) );
break;
}
}
}
else if (LOWORD(wParam) == IDC_RADIO1)
{
if (HIWORD (wParam) == BN_CLICKED)
Print(TEXT("Radio 1"));
}
else if (LOWORD(wParam) == IDC_RADIO2)
{
if (HIWORD (wParam) == BN_CLICKED)
Print(TEXT("Radio 2"));
}
else if (LOWORD(wParam) == IDC_RADIO3)
{
if (HIWORD (wParam) == BN_CLICKED)
Print(TEXT("Radio 3"));
}
else if (LOWORD(wParam) == IDC_CHECK1)
{
if (HIWORD (wParam) == BN_CLICKED)
if (IsDlgButtonChecked(hDlg, IDC_CHECK1) == BST_CHECKED)
Print(TEXT("Check1 is checked"));
else
Print(TEXT("Check1 is unchecked"));
}
else if (LOWORD(wParam) == IDC_CHECK2)
{
if (HIWORD (wParam) == BN_CLICKED)
if (IsDlgButtonChecked(hDlg, IDC_CHECK2) == BST_CHECKED)
Print(TEXT("Check2 is checked"));
else
Print(TEXT("Check2 is unchecked"));
}
else if (LOWORD(wParam) == IDC_CHECK3)
{
if (HIWORD (wParam) == BN_CLICKED)
if (IsDlgButtonChecked(hDlg, IDC_CHECK3) == BST_CHECKED)
Print(TEXT("Check3 is checked"));
else
Print(TEXT("Check3 is unchecked"));
}
else
{
}
break;
}
return FALSE;
}
//=============================== Vincent added code here for local functions ================================
void Print(TCHAR *pFormat, ...)
{
va_list ArgList;
TCHAR Buffer[256];
INT i=0;
va_start (ArgList, pFormat);
(void)wvsprintf (Buffer, pFormat, ArgList);
i = SendDlgItemMessage(gHwndMain, IDC_RPTLIST, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)Buffer);
if (i != LB_ERR)
SendDlgItemMessage (gHwndMain, IDC_RPTLIST, LB_SETTOPINDEX, i, 0);
va_end(ArgList);
}
void MenuCommandOpen(HWND hWnd)
{
OPENFILENAME of;
TCHAR szFileName [100] = {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.
of.lStructSize = sizeof (of);
of.hwndOwner = hWnd;
of.lpstrFile = szFileName;
of.nMaxFile = 100;
of.lpstrFilter = pszOpenFilter;
of.Flags = 0;
rc = GetOpenFileName (&of);
Print(TEXT("GetOpenFileName returned: %x, filename: %s"), rc, szFileName);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -