⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmdbarapp.cpp

📁 《Windows CE 权威指南》(作者:(美)CHRIS MUENCH
💻 CPP
字号:
// CmdBarApp.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "CmdBarApp.h"
#include <commctrl.h>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE			hInst;			// The current instance
HWND				hwndCB;			// The command bar handle

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE hInstance, LPTSTR szWindowClass);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About			(HWND, UINT, WPARAM, LPARAM);

// <BOOK_ADDONS Chapter 7.2.1> ***************************************************
HIMAGELIST hImLst;
#define MAX_BANDS			3
COMMANDBANDSRESTOREINFO rstBand[MAX_BANDS];
// </BOOK_ADDONS Chapter 7.2.1> ***************************************************
// <BOOK_ADDONS Chapter 7.1.1> ***************************************************
#define MAX_CMDBAR_ITEMS	5
static TBBUTTON tbSTDButton[] = {
	{0,              0,       TBSTATE_ENABLED, TBSTYLE_SEP,    0, 0},  
	{STD_FILENEW,	 33,      TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
	{STD_FILEOPEN,	 34,      TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
};
LPTSTR szToolTips[MAX_CMDBAR_ITEMS];

#define JCCMDBAR_OFF	0
#define JCCMDBAR_ON		1
#define JCCMDBAR_TOGGLE	2

int JCCMDBarUp;

void ToggleCMDBar(HWND hWnd,int how)
{
	switch (how)
	{
		case JCCMDBAR_OFF:
					JCCMDBarUp=0;
					CommandBar_Show(hWnd, TRUE);
				break;
		case JCCMDBAR_ON:
					JCCMDBarUp=1;
					CommandBar_Show(hWnd, FALSE);
				break;
		case JCCMDBAR_TOGGLE:
					JCCMDBarUp=1-JCCMDBarUp;
					ToggleCMDBar(hWnd, JCCMDBarUp);
				break;
	}
}
// </BOOK_ADDONS Chapter 7.1.1> ***************************************************

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_CMDBARAPP);

	// 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:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. 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_CMDBARAPP));
    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_CMDBARAPP, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance, szWindowClass);

 // <BOOK_ADDON Chapter 7.2.1> ****************************************************
    INITCOMMONCONTROLSEX icex;
	icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_COOL_CLASSES;
    InitCommonControlsEx (&icex);
// </BOOK_ADDON Chapter 7.2.1> ****************************************************

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		0, 0, 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];

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_HELP_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_FILE_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
// <BOOK_ADDONS Chapter 7.2.1> ***************************************************
		{
			int ii;
			HBITMAP hBmp;
				hImLst=ImageList_Create(16,16,ILC_COLOR,2,0);
				hBmp=LoadBitmap(hInst,MAKEINTRESOURCE(IDR_TOOLBAR));
				ImageList_Add(hImLst,hBmp,NULL);
				DeleteObject(hBmp);
				hwndCB=CommandBands_Create(hInst,hWnd,1,
 											RBS_SMARTLABELS|RBS_VARHEIGHT,hImLst);
				REBARBANDINFO trbi[MAX_BANDS];
				int i;
				HWND thwndCombo;
				HWND thwndCBar;			// The command bar handle
			
				// Zero the RebarInfo Structure or you will get funny effects !
				memset(trbi,0,sizeof(REBARINFO)*MAX_BANDS);
				for (i = 0; i < MAX_BANDS; i++) 
				{
				    trbi[i].cbSize = sizeof (REBARBANDINFO);
				    trbi[i].fMask = RBBIM_ID | RBBIM_SIZE | RBBIM_STYLE;
				    trbi[i].wID = IDR_TOOLBAR+i;
				}
			
				// First Band: The Menu - No Gripper here. we do not want to move it
				trbi[0].fStyle = RBBS_FIXEDBMP | RBBS_NOGRIPPER;  
				// We have to guess the size because there is no measure 
				// method available
				trbi[0].cx = 100;	
				trbi[0].iImage = 0;
				
				// Second Band: The buttons 
				trbi[1].fMask |= RBBIM_TEXT;
				trbi[1].iImage = 0;
				trbi[1].lpText = TEXT ("Buttons");
				// Guessing again. This time we can be more precise (2x16 + border)
				trbi[1].cx = 40;
				//trbi[1].fStyle = RBBS_FIXEDBMP;
				
				// Third Band: A combo Box
				thwndCombo = CreateWindow (TEXT ("COMBOBOX"), TEXT ("MyComboBox"),
				                           WS_VISIBLE | WS_CHILD | WS_BORDER,
				                           0, 0, 10, 5, hWnd, (HMENU)42, hInst, NULL);
				
				trbi[2].fMask |= RBBIM_TEXT | RBBIM_STYLE | 
				                 RBBIM_CHILDSIZE | RBBIM_CHILD;
				trbi[2].hwndChild = thwndCombo;
				trbi[2].cxMinChild = 0;
				trbi[2].cyMinChild = 23;
				trbi[2].cyChild = 55;
				trbi[2].iImage = 0;
				trbi[2].lpText = TEXT ("My ComboBox");
				trbi[2].cx = 130;
				trbi[2].fStyle = RBBS_BREAK|RBBS_CHILDEDGE;
				
				// Add the bands.
				CommandBands_AddBands (hwndCB, hInst, 3, trbi);
			    thwndCBar = CommandBands_GetCommandBar (hwndCB, 0);
			    CommandBar_InsertMenubarEx(thwndCBar, hInst,
			                              MAKEINTRESOURCE(IDM_MENU), 0);
			
			// Now getting it from the second Band (Index 1)
			    thwndCBar = CommandBands_GetCommandBar (hwndCB, 1);
			    CommandBar_AddBitmap(thwndCBar, hInst, IDR_TOOLBAR, 2, 16, 16);                                     
			    CommandBar_AddButtons(thwndCBar,
			                         sizeof(tbSTDButton)/sizeof(TBBUTTON),
			                         tbSTDButton);   
			
			    szToolTips[0]=(LPTSTR)LocalAlloc(LMEM_ZEROINIT, sizeof(TCHAR));
			    wsprintf(szToolTips[0],TEXT(""));
			    for (ii=1;ii<MAX_CMDBAR_ITEMS;ii++)
				{
			       LoadString(hInst,IDS_TOOLTIPSTART+ii,
			                   szHello,MAX_LOADSTRING);
							szToolTips[ii]=(LPTSTR)LocalAlloc(LMEM_ZEROINIT,
			                                       sizeof(TCHAR)*lstrlen(szHello));
			      wsprintf(szToolTips[ii],szHello);
				}
			   CommandBar_AddToolTips(thwndCBar, MAX_CMDBAR_ITEMS,szToolTips);   
			   // Not needed anymore. Band Three holds a combo box already
			   // You can still use this function but the method above is much 
			   // more flexible
			   //CommandBar_InsertComboBox(hwndCB,hInst, 50, NULL, 8, 1);
			   CommandBands_AddAdornments(hwndCB, hInst, CMDBAR_OK, NULL);
		}
// <BOOK_ADDONS Chapter 7.2.1> ***************************************************

			
			//hwndCB = CommandBar_Create(hInst, hWnd, 1);			
// <BOOK_ADDONS Chapter 7.1.1> ***************************************************
			//CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
			//CommandBar_AddAdornments(hwndCB, 0, 0);
			/*{
				int ii;
				CommandBar_InsertMenubarEx(hwndCB, hInst, MAKEINTRESOURCE(IDM_MENU), 0);
		
				CommandBar_AddBitmap(hwndCB, HINST_COMMCTRL, IDB_STD_SMALL_COLOR, 2, 16, 16);                                     
				CommandBar_AddButtons(hwndCB, sizeof(tbSTDButton)/sizeof(TBBUTTON),tbSTDButton);   
				CommandBar_InsertComboBox(hwndCB,hInst, 50, NULL, 8, 1); 
		
				szToolTips[0]=(LPTSTR)LocalAlloc(LMEM_ZEROINIT, sizeof(TCHAR));
				wsprintf(szToolTips[0],TEXT(""));
				for (ii=1;ii<MAX_CMDBAR_ITEMS;ii++)
				{
					LoadString(hInst,IDS_TOOLTIPSTART+ii,szHello,MAX_LOADSTRING);
					szToolTips[ii]=(LPTSTR)LocalAlloc(LMEM_ZEROINIT, sizeof(TCHAR)*lstrlen(szHello));
					wsprintf(szToolTips[ii],szHello);
				}
				CommandBar_AddToolTips(hwndCB, MAX_CMDBAR_ITEMS,szToolTips);   
				CommandBar_AddAdornments(hwndCB, CMDBAR_OK, 0);
			}*/
// </BOOK_ADDONS Chapter 7.1.1> ***************************************************
			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;
// <BOOK_ADDONS Chapter 7.1.1> ****************************************************
		case WM_LBUTTONDOWN: 
			ToggleCMDBar(hwndCB,JCCMDBAR_TOGGLE);
			break;
// </BOOK_ADDONS Chapter 7.1.1> ***************************************************
		case WM_DESTROY:
// <BOOK_ADDONS Chapter 7.1.1> ****************************************************
			{
				int ii;
				for (ii=0;ii<MAX_CMDBAR_ITEMS;ii++)
					LocalFree(szToolTips[ii]);
			}
// </BOOK_ADDONS Chapter 7.1.1> ***************************************************
			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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -