📄 menubartest.cpp
字号:
// MENUBARTEST.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "MENUBARTEST.h"
#include <commctrl.h>
#include <aygshell.h>
#include <sipapi.h>
#include "DeviceResolutionAware.h"
#include "WaveFile.h"
#include "ScktApp.h"
#define MAX_LOADSTRING 100
#define FIRSTPAGE 101
#define MIDPAGE 102
#define FINISHPAGE 103
#define MAX_ERRMSG 256
#define FILENAME_LEN 100
// WAV File Name List
#define BEGIN_FILE TEXT("Begin.wav")
#define INPROGRAM_FILE TEXT("Rotate.wav")
#define NEXT_FILE TEXT("Next.wav")
#define BACK_FILE TEXT("Back.wav")
DWORD g_threadId;
// Global Variables:
extern "C" HINSTANCE g_hInstance = NULL;
extern "C" HINSTANCE g_hExeInst = NULL;
HINSTANCE g_hInst; // The current instance
HWND g_hwndCB; // The command bar handle
HWND g_hSipWnd;
int pageFlag = FIRSTPAGE; //The flag for proper page!
TCHAR g_PlayWavFile[FILENAME_LEN];
int g_nFactor=1; //Set 2 if in VGA screen.
TCHAR g_szWavFile[FILENAME_LEN];
HANDLE g_MusicThread; //Background Music Thread
static SHACTIVATEINFO s_sai;
// 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);
HWND CreateRpCommandBar(HWND);
MMRESULT PlayWave(LPCTSTR szWaveFile);
DWORD WINAPI PlayWaveProc(void* param);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
if(DRA::IsHiResScreen())
g_nFactor=2;
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MENUBARTEST);
g_hExeInst = hInstance;
g_hInstance = hInstance;
// Inint ScktApp System
Sckt_InitApp(g_hExeInst, &g_hInstance, TEXT(""), FALSE);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Deinit ScktApp system
Sckt_DeinitApp();
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_MENUBARTEST));
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 = NULL;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name
g_hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
LoadString(hInstance, IDC_MENUBARTEST, szWindowClass, MAX_LOADSTRING);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
//If it is already running, then focus on the window
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x01" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}
MyRegisterClass(hInstance, szWindowClass);
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hwndCB)
{
RECT rc;
RECT rcMenuBar;
GetWindowRect(hWnd, &rc);
GetWindowRect(g_hwndCB, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}
// 隐藏SIP
g_hSipWnd = FindWindow(_T("MS_SIPBUTTON"),NULL);
if(g_hSipWnd != NULL)
{
ShowWindow(g_hSipWnd, SW_HIDE);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
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];
DWORD lpExitCode;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_OK:
CommandBar_Destroy(g_hwndCB);
PostQuitMessage(0);
break;
case IDM_NEXT:
pageFlag = FINISHPAGE;
Sckt_DialogBox(g_hExeInst, TEXT("IDD_ABOUTBOX"), hWnd, (DLGPROC) About);
break;
case IDM_BACK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
g_hwndCB = CreateRpCommandBar(hWnd);
// Initialize the shell activate info structure
memset (&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
// Initialize the path of WAV files
SHGetSpecialFolderPath(NULL, g_szWavFile, CSIDL_PERSONAL, FALSE);
// Initialize the WAV file need play now
lstrcpy(g_PlayWavFile, g_szWavFile);
_tcscat(g_PlayWavFile, TEXT("\\"));
_tcscat(g_PlayWavFile, BEGIN_FILE);
g_MusicThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlayWaveProc, NULL, 0, &g_threadId);
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
LoadString(g_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:
GetExitCodeThread(g_MusicThread, &lpExitCode);
TerminateThread(g_MusicThread,lpExitCode);
CommandBar_Destroy(g_hwndCB);
PostQuitMessage(0);
break;
case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LPCTSTR ResStr(int nStrID)
{
return (LPCTSTR)LoadString(g_hInst, nStrID, NULL, 0);
}
static TBBUTTON tbSimpleButtonFIRST[] =
{
{2, ID_OK, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
{0, 0, 0, TBSTYLE_SEP, 0, -1},
{0, IDM_BACK, TBSTATE_INDETERMINATE, TBSTYLE_BUTTON, 0, -1},
{1, IDM_NEXT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
{0, 0, 0, TBSTYLE_SEP, 0, -1},
};
static TBBUTTON tbSimpleButton[] =
{
{2, ID_OK, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
{0, 0, 0, TBSTYLE_SEP, 0, -1},
{0, IDM_BACK, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
{1, IDM_NEXT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
{0, 0, 0, TBSTYLE_SEP, 0, -1},
};
HWND CreateRpCommandBar(HWND hwnd)
{
SHMENUBARINFO mbi;
TBBUTTONINFO tbbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hwnd;
mbi.nToolBarId = IDM_MENU;
mbi.hInstRes = g_hInst;
mbi.nBmpId = IDB_BUTTONICON;
mbi.cBmpImages = 3;
if (!SHCreateMenuBar(&mbi))
return NULL;
if(pageFlag == FIRSTPAGE)
CommandBar_AddButtons(mbi.hwndMB, 4, tbSimpleButtonFIRST);
else
CommandBar_AddButtons(mbi.hwndMB, 4, tbSimpleButton);
tbbi.cbSize = sizeof(TBBUTTONINFO);
tbbi.dwMask = TBIF_TEXT | TBIF_SIZE;
tbbi.cx = 66 * g_nFactor;
tbbi.pszText = (LPTSTR)ResStr(IDS_WZBTNCANCEL); //TEXT("Cancel");
SendMessage(mbi.hwndMB, TB_SETBUTTONINFO, (WPARAM)(INT) ID_OK, (LPARAM)(LPTBBUTTONINFO) &tbbi);
tbbi.pszText = (LPTSTR)ResStr(IDS_WZBTNBACK); //TEXT("Back");
SendMessage(mbi.hwndMB, TB_SETBUTTONINFO, (WPARAM)(INT) IDM_BACK, (LPARAM)(LPTBBUTTONINFO) &tbbi);
tbbi.dwMask |= TBIF_STYLE;
tbbi.fsStyle = (BYTE)BS_LEFT;
if(pageFlag == FINISHPAGE)
tbbi.pszText = (LPTSTR)ResStr(IDS_WZBTNFINISH); //TEXT("Finish");
else
tbbi.pszText = (LPTSTR)ResStr(IDS_WZBTNNEXT); //TEXT("NEXT");
SendMessage(mbi.hwndMB, TB_SETBUTTONINFO, (WPARAM)(INT) IDM_NEXT, (LPARAM)(LPTBBUTTONINFO) &tbbi);
return mbi.hwndMB;
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
SHINITDLGINFO shidi;
switch (message)
{
case WM_INITDIALOG:
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
g_hwndCB = CreateRpCommandBar(hDlg);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
CommandBar_Destroy(g_hwndCB);
PostQuitMessage(0);
break;
}
if (LOWORD(wParam) == ID_OK)
{
CommandBar_Destroy(g_hwndCB);
PostQuitMessage(0);
return TRUE;
}
if (LOWORD(wParam) == IDM_BACK)
{
EndDialog(hDlg, LOWORD(wParam));
pageFlag = FIRSTPAGE;
return TRUE;
}
if (LOWORD(wParam) == IDM_NEXT)
{
if(pageFlag == FINISHPAGE)
{
CommandBar_Destroy(g_hwndCB);
PostQuitMessage(0);
}
return TRUE;
}
break;
}
return FALSE;
}
MMRESULT PlayWave(LPCTSTR szWavFile)
{
HWAVEOUT hwo;
WAVEHDR whdr;
MMRESULT mmres;
CWaveFile waveFile;
HANDLE hDoneEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("DONE_EVENT"));
UINT devId;
DWORD dwOldVolume;
// Open wave file
if (!waveFile.Open(szWavFile)) {
TCHAR szErrMsg[MAX_ERRMSG];
_stprintf(szErrMsg,
TEXT("Unable to open file: %s\n\n"),
szWavFile);
MessageBox(NULL, szErrMsg, TEXT("File I/O Error"), MB_OK);
return MMSYSERR_NOERROR;
}
// Open audio device
for (devId = 0; devId < waveOutGetNumDevs(); devId++) {
mmres = waveOutOpen(&hwo, devId, waveFile.GetWaveFormat(), (DWORD)hDoneEvent,
0, CALLBACK_EVENT);
if (mmres == MMSYSERR_NOERROR) {
break;
}
}
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
// Set volume
mmres = waveOutGetVolume(hwo, &dwOldVolume);
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
waveOutSetVolume(hwo, 0xFFFFFFFF);
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
// Initialize wave header
ZeroMemory(&whdr, sizeof(WAVEHDR));
whdr.lpData = new char[waveFile.GetLength()];
whdr.dwBufferLength = waveFile.GetLength();
whdr.dwUser = 0;
whdr.dwFlags = 0;
whdr.dwLoops = 0;
whdr.dwBytesRecorded = 0;
whdr.lpNext = 0;
whdr.reserved = 0;
// Play buffer
waveFile.Read(whdr.lpData, whdr.dwBufferLength);
mmres = waveOutPrepareHeader(hwo, &whdr, sizeof(WAVEHDR));
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
mmres = waveOutWrite(hwo, &whdr, sizeof(WAVEHDR));
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
// Wait for audio to finish playing
while (!(whdr.dwFlags & WHDR_DONE)) {
WaitForSingleObject(hDoneEvent, INFINITE);
}
// Clean up
mmres = waveOutUnprepareHeader(hwo, &whdr, sizeof(WAVEHDR));
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
waveOutSetVolume(hwo, dwOldVolume);
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
mmres = waveOutClose(hwo);
if (mmres != MMSYSERR_NOERROR) {
return mmres;
}
delete [] whdr.lpData;
waveFile.Close();
return MMSYSERR_NOERROR;
}
DWORD WINAPI PlayWaveProc(void* param)
{
MMRESULT mmres;
TCHAR szErrMsg[MAX_ERRMSG];
while(true)
{
mmres = PlayWave(g_PlayWavFile);
if (mmres != MMSYSERR_NOERROR)
{
waveOutGetErrorText(mmres, (LPTSTR) szErrMsg, 256);
MessageBox(NULL, szErrMsg, TEXT("WaveOut Error"), MB_OK);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -