📄 gapishow.cpp
字号:
#include "stdafx.h" // Wizard includes
#include <aygshell.h> // Pocket PC includes
#include <gx.h> // GAPI includes
#include "GapiShow.h"
#include "resource.h" // Tools generated equates
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("GapiShow");
HINSTANCE hInst; // Program instance handle
// Pocket PC globals
HWND hwndMenuBar = NULL; // Handle of menu bar control
BOOL fHibernated = FALSE; // Indicates hibernated state
BOOL fPlaying = FALSE; // Indicates Gapi access active
BOOL fResuming = FALSE; // Used when regaining focus
SHACTIVATEINFO sai; // Used to adjust window for SIP
RECT rectNorm;
int cyFont = 0;
int nSpeed = 2, ndX = 0, ndY = 0;
int CxScreen, CyScreen;
int nCnt = 0;
STARINFO ptStars[MAX_STARS]; // Star field info
GXDisplayProperties gxdp; // GAPI display info structure
GXKeyList gxkl; // GAPI keyboard info structure
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_TIMER, DoTimerMain,
WM_KEYDOWN, DoKeyDownMain,
WM_CREATE, DoCreateMain,
WM_PAINT, DoPaintMain,
WM_LBUTTONDOWN, DoLButtonDownMain,
WM_SETFOCUS, DoSetFocusMain,
WM_KILLFOCUS, DoKillFocusMain,
WM_COMMAND, DoCommandMain,
WM_SETTINGCHANGE, DoSettingChangeMain,
WM_ACTIVATE, DoActivateMain,
WM_HIBERNATE, DoHibernateMain,
WM_DESTROY, DoDestroyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
ID_GAME_EXIT, DoMainCommandExit,
ID_GAME_PLAY, DoMainCommandPlay,
ID_TOOLS_ABOUT, DoMainCommandAbout,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
HWND hwndMain;
HACCEL hAccel;
// Initialize application.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0) return 0x10;
hAccel = LoadAccelerators(hInstance,
MAKEINTRESOURCE (IDR_ACCELERATOR1));
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
// Translate accelerator keys
if (!TranslateAccelerator(hwndMain, hAccel, &msg)) {
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;
// Allow only one instance of the application.
hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return (HWND)-1;
}
// Register application main window class.
wc.style = CS_VREDRAW | CS_HREDRAW; // Window style
wc.lpfnWndProc = MainWndProc; // 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) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Clear GAPI info structures.
memset (&gxdp, 0, sizeof (gxdp));
memset (&gxkl, 0, sizeof (gxkl));
CxScreen = GetSystemMetrics (SM_CXSCREEN);
CyScreen = GetSystemMetrics (SM_CYSCREEN);
// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT("GAPI Show"), // Window title
WS_VISIBLE, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
if (!IsWindow (hWnd)) return 0; // Fail if not created.
// Query GAPI parameters.
if (GXOpenDisplay(hWnd, GX_FULLSCREEN)) {
gxdp = GXGetDisplayProperties();
gxkl = GXGetDefaultKeys(GX_NORMALKEYS);
} else
MessageBox (hWnd, TEXT ("GXOpenDisplay failed"),
szAppName, MB_OK);
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
GXCloseDisplay();
return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (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(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
SIPINFO si;
int cx, cy;
TEXTMETRIC tm;
HDC hdc;
// Query the height of the default font.
hdc = GetDC (hWnd);
GetTextMetrics (hdc, &tm);
cyFont = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC (hWnd, hdc);
// Initialize the shell to activate info structure.
memset (&sai, 0, sizeof (sai));
sai.cbSize = sizeof (sai);
// Create menu bar and check for errors.
hwndMenuBar = MyCreateMenuBar (hWnd);
if (!hwndMenuBar) {
MessageBox (hWnd, TEXT("Couldn\'t create menu bar"),
szAppName, MB_OK);
DestroyWindow (hWnd);
}
// Query the sip state and size our window appropriately.
memset (&si, 0, sizeof (si));
si.cbSize = sizeof (si);
SHSipInfo(SPI_GETSIPINFO, 0, (PVOID)&si, FALSE);
cx = si.rcVisibleDesktop.right - si.rcVisibleDesktop.left;
cy = si.rcVisibleDesktop.bottom - si.rcVisibleDesktop.top;
// If the sip is not shown, or is showing but not docked, the
// desktop rect doesn't include the height of the menu bar.
if (!(si.fdwFlags & SIPF_ON) ||
((si.fdwFlags & SIPF_ON) && !(si.fdwFlags & SIPF_DOCKED)))
cy -= 26; // Height of menu bar control
SetWindowPos (hWnd, NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER);
return 0;
}
//----------------------------------------------------------------------
// DoTimerMain - Process WM_TIMER message for window.
//
LRESULT DoTimerMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PBYTE lpBuff;
lpBuff = (PBYTE) GXBeginDraw();
if (lpBuff) {
DrawScreen_16 (lpBuff, ndX, ndY, nSpeed);
if (fResuming)
ClearScreen_16 (lpBuff, RGB (0, 0, 0));
GXEndDraw();
} else {
KillTimer (hWnd, ID_TIMER);
}
return 0;
}
//----------------------------------------------------------------------
// DoKeyDownMain - Process WM_KEYDOWN message for window.
//
LRESULT DoKeyDownMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
if (fPlaying) {
// Up
if (wParam == (DWORD)gxkl.vkUp) {
if (ndY > -100) ndY -= 10;
// Down
} else if (wParam == (DWORD)gxkl.vkDown) {
if (ndY < 100) ndY += 10;
// Left
} else if (wParam == (DWORD)gxkl.vkLeft) {
if (ndX > -100) ndX -= 10;
// Right
} else if (wParam == (DWORD)gxkl.vkRight) {
if (ndX < 100) ndX += 10;
// Fast
} else if (wParam == (DWORD)gxkl.vkA) {
if (nSpeed < 10) nSpeed += 1;
// Slow
} else if (wParam == (DWORD)gxkl.vkB) {
if (nSpeed > 1) nSpeed -= 1;
else nSpeed = 1;
} else if (wParam == (DWORD)gxkl.vkC) {
ndX = 0;
ndY = 0;
nSpeed = 2;
}
}
return 0;
}
//----------------------------------------------------------------------
// DoLButtonDownMain - Process WM_LBUTTONDOWN message for window.
//
LRESULT DoLButtonDownMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
// If playing, stop the game.
if (fPlaying)
SendMessage (hWnd, WM_COMMAND, MAKELONG (ID_GAME_PLAY, 0), 0);
return 0;
}
//----------------------------------------------------------------------
// DoSetFocusMain - Process WM_SETFOCUS message for window.
//
LRESULT DoSetFocusMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
if (fPlaying) {
GXResume();
fResuming = TRUE;
// Start a very fast timer.
SetTimer (hWnd, ID_TIMER, 10, NULL);
}
return 0;
}
//----------------------------------------------------------------------
// DoKillFocusMain - Process WM_KILLFOCUS message for window.
//
LRESULT DoKillFocusMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
if (fPlaying)
SetWindowPos (hWnd, HWND_NOTOPMOST, 0, 0, CxScreen, CyScreen, 0);
KillTimer (hWnd, ID_TIMER);
GXSuspend();
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
INT i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
TCHAR szTxt[128];
HDC hdc;
int i, y = 5;
static hOldPlaying;
hdc = BeginPaint (hWnd, &ps);
GetClientRect (hWnd, &rect);
// If not playing, display the GAPI information about the device.
if (!fPlaying) {
wsprintf (szTxt, TEXT ("Gapi values:"));
ExtTextOut (hdc, 5, y, 0, &rect, szTxt, lstrlen (szTxt), 0);
y += cyFont;
wsprintf (szTxt, TEXT ("cxWidth: %d"), gxdp.cxWidth);
ExtTextOut (hdc, 10, y, 0, &rect, szTxt, lstrlen (szTxt), 0);
y += cyFont;
wsprintf (szTxt, TEXT ("cyHeight: %d"), gxdp.cyHeight);
ExtTextOut (hdc, 10, y, 0, &rect, szTxt, lstrlen (szTxt), 0);
y += cyFont;
wsprintf (szTxt, TEXT ("cbxPitch: %d"), gxdp.cbxPitch);
ExtTextOut (hdc, 10, y, 0, &rect, szTxt, lstrlen (szTxt), 0);
y += cyFont;
wsprintf (szTxt, TEXT ("cbyPitch: %d"), gxdp.cbyPitch);
ExtTextOut (hdc, 10, y, 0, &rect, szTxt, lstrlen (szTxt), 0);
y += cyFont;
wsprintf (szTxt, TEXT ("cBPP: %d"), gxdp.cBPP);
ExtTextOut (hdc, 10, y, 0, &rect, szTxt, lstrlen (szTxt), 0);
y += cyFont;
wsprintf (szTxt, TEXT ("ffFormat: %08x"), gxdp.ffFormat);
ExtTextOut (hdc, 10, y, 0, &rect, szTxt, lstrlen (szTxt), 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -