📄 d3dapplication.cpp
字号:
/*
TCHAR strKey[100];
//DWORD monIdx;
SMonitorInfo* monInfo;
DWORD adapterIdx;
SD3DAdapterInfo* adapterInfo;
HKEY hkey;
DWORD type = REG_DWORD;
DWORD dwLength;
GUID guidAdapterID;
GUID guidZero;
ZeroMemory( &guidAdapterID, sizeof(GUID) );
ZeroMemory( &guidZero, sizeof(GUID) );
dwLength = sizeof(DWORD);
for( monIdx = 0; monIdx < mMonitorCount; monIdx++ ) {
monInfo = &mMonitors[monIdx];
adapterIdx = monInfo->adapterIdx;
if( adapterIdx == NO_ADAPTER )
continue;
adapterInfo = mAdapters[adapterIdx];
wsprintf( strKey, TEXT("Screen %d"), monIdx + 1 );
if( ERROR_SUCCESS == RegCreateKeyEx( hkeyParent, strKey,
0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ) )
{
dwLength = sizeof(GUID);
RegQueryValueEx( hkey, TEXT("Adapter ID"), NULL, &type,
(BYTE*)&guidAdapterID, &dwLength);
dwLength = sizeof(DWORD);
RegQueryValueEx( hkey, TEXT("Leave Black"), NULL, &type,
(BYTE*)&adapterInfo->leaveBlack, &dwLength);
if( guidAdapterID == adapterInfo->adapterID.DeviceIdentifier ||
guidAdapterID == guidZero )
{
dwLength = sizeof(DWORD);
RegQueryValueEx( hkey, TEXT("Disable Hardware"), NULL, &type,
(BYTE*)&adapterInfo->disableHAL, &dwLength);
dwLength = sizeof(DWORD);
RegQueryValueEx( hkey, TEXT("width"), NULL, &type,
(BYTE*)&adapterInfo->userPrefWidth, &dwLength);
dwLength = sizeof(DWORD);
RegQueryValueEx( hkey, TEXT("height"), NULL, &type,
(BYTE*)&adapterInfo->userPrefHeight, &dwLength);
dwLength = sizeof(DWORD);
RegQueryValueEx( hkey, TEXT("Format"), NULL, &type,
(BYTE*)&adapterInfo->userPrefFormat, &dwLength);
}
RegCloseKey( hkey);
}
}
*/
}
// Write the registry settings that affect how the screens are set up and used.
void CD3DApplication::ssWriteSettings()
{
// TBD
/*
TCHAR strKey[100];
//DWORD monIdx;
SMonitorInfo* monInfo;
DWORD adapterIdx;
SD3DAdapterInfo* adapterInfo;
HKEY hkey;
for( monIdx = 0; monIdx < mMonitorCount; monIdx++ ) {
monInfo = &mMonitors[monIdx];
adapterIdx = monInfo->adapterIdx;
if( adapterIdx == NO_ADAPTER )
continue;
adapterInfo = mAdapters[adapterIdx];
wsprintf( strKey, TEXT("Screen %d"), monIdx + 1 );
if( ERROR_SUCCESS == RegCreateKeyEx( hkeyParent, strKey,
0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ) )
{
RegSetValueEx( hkey, TEXT("Leave Black"), NULL, REG_DWORD,
(BYTE*)&adapterInfo->leaveBlack, sizeof(DWORD) );
RegSetValueEx( hkey, TEXT("Disable Hardware"), NULL, REG_DWORD,
(BYTE*)&adapterInfo->disableHAL, sizeof(DWORD) );
RegSetValueEx( hkey, TEXT("width"), NULL, REG_DWORD,
(BYTE*)&adapterInfo->userPrefWidth, sizeof(DWORD) );
RegSetValueEx( hkey, TEXT("height"), NULL, REG_DWORD,
(BYTE*)&adapterInfo->userPrefHeight, sizeof(DWORD) );
RegSetValueEx( hkey, TEXT("Format"), NULL, REG_DWORD,
(BYTE*)&adapterInfo->userPrefFormat, sizeof(DWORD) );
RegSetValueEx( hkey, TEXT("Adapter ID"), NULL, REG_BINARY,
(BYTE*)&adapterInfo->adapterID.DeviceIdentifier, sizeof(GUID) );
RegCloseKey( hkey);
}
}
*/
}
eSaverMode CD3DApplication::ssParseCmdLine( TCHAR* cmdLine )
{
mSSHwndParent = NULL;
// Skip the first part of the command line, which is the full path
// to the exe. If it contains spaces, it will be contained in quotes.
if( *cmdLine == TEXT('\"') ) {
cmdLine++;
while( *cmdLine != TEXT('\0') && *cmdLine != TEXT('\"') )
cmdLine++;
if( *cmdLine == TEXT('\"') )
cmdLine++;
} else {
while( *cmdLine != TEXT('\0') && *cmdLine != TEXT(' ') )
cmdLine++;
if( *cmdLine == TEXT(' ') )
cmdLine++;
}
// Skip along to the first option delimiter "/" or "-"
while( *cmdLine != TEXT('\0') && *cmdLine != TEXT('/') && *cmdLine != TEXT('-') )
cmdLine++;
// If there wasn't one, then must be config mode
if( *cmdLine == TEXT('\0') )
return SM_CONFIG;
// Otherwise see what the option was
switch( *(++cmdLine) ) {
case 'c':
case 'C':
cmdLine++;
while ( *cmdLine && !isdigit(*cmdLine) )
cmdLine++;
if( isdigit(*cmdLine) ) {
#ifdef _WIN64
CHAR strCommandLine[2048];
convertGenericStringToAnsiCb( strCommandLine, cmdLine, sizeof(strCommandLine) );
mSSHwndParent = (HWND)(_atoi64(strCommandLine));
#else
mSSHwndParent = (HWND)LongToHandle(_ttol(cmdLine));
#endif
} else {
mSSHwndParent = NULL;
}
return SM_CONFIG;
case 't':
case 'T':
return SM_TEST;
case 'p':
case 'P':
// Preview-mode, so option is followed by the parent HWND in decimal
cmdLine++;
while( *cmdLine && !isdigit(*cmdLine) )
cmdLine++;
if( isdigit(*cmdLine) ) {
#ifdef _WIN64
CHAR strCommandLine[2048];
convertGenericStringToAnsiCb(strCommandLine, cmdLine, sizeof(strCommandLine));
mSSHwndParent = (HWND)(_atoi64(strCommandLine));
#else
mSSHwndParent = (HWND)LongToHandle(_ttol(cmdLine));
#endif
}
return SM_PREVIEW;
case 'a':
case 'A':
// Password change mode, so option is followed by parent HWND in decimal
cmdLine++;
while( *cmdLine && !isdigit(*cmdLine) )
cmdLine++;
if ( isdigit(*cmdLine) ) {
#ifdef _WIN64
CHAR strCommandLine[2048];
convertGenericStringToAnsiCb(strCommandLine, cmdLine, sizeof(strCommandLine));
mSSHwndParent = (HWND)(_atoi64(strCommandLine));
#else
mSSHwndParent = (HWND)LongToHandle(_ttol(cmdLine));
#endif
}
return SM_PASSCHANGE;
default:
// All other options => run the screensaver (typically this is "/s")
return SM_FULL;
}
}
// --------------------------------------------------------------------------
/** Message handling function. */
LRESULT CD3DApplication::msgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg ) {
case WM_USER:
// Screensaver:
if( mSSMode != SM_NONE ) {
// All initialization messages have gone through. Allow
// 500ms of idle time, then proceed with initialization.
SetTimer( hWnd, 1, 500, NULL );
}
break;
case WM_TIMER:
// Screensaver:
if( mSSMode != SM_NONE ) {
// Initial idle time is done, proceed with initialization.
mSSInStartingPause = false;
KillTimer( hWnd, 1 );
}
break;
case WM_PAINT:
// TBD: saver?
// handle paint messages when the app is paused
if( mD3DDevice && !mActive && mWindowed && mDeviceObjectsInited && mDeviceObjectsRestored ) {
performOneTime();
mD3DDevice->Present( NULL, NULL, NULL, NULL );
}
break;
case WM_ERASEBKGND:
// Erase background if checking password
if( mSSMode != SM_NONE && !mSSCheckingPassword )
return true; // don't erase this window
break;
case WM_GETMINMAXINFO:
((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
break;
case WM_ENTERSIZEMOVE:
// halt movement while the app is sizing or moving
pause( true );
break;
case WM_SIZE:
// pick up possible changes to window style due to maximize, etc.
if( mWindowed && mHWnd != NULL )
mWindowStyle = GetWindowLong( mHWnd, GWL_STYLE );
if( SIZE_MINIMIZED == wParam ) {
if( mClipCursorWhenFullscreen && !mWindowed )
ClipCursor( NULL );
pause( true ); // pause while we're minimized
mMinimized = true;
mMaximized = false;
} else if( SIZE_MAXIMIZED == wParam ) {
if( mMinimized )
pause( false ); // unpause since we're no longer minimized
mMinimized = false;
mMaximized = true;
handlePossibleSizeChange();
} else if( SIZE_RESTORED == wParam ) {
if( mMaximized ) {
mMaximized = false;
handlePossibleSizeChange();
} else if( mMinimized ) {
pause( false ); // unpause since we're no longer minimized
mMinimized = false;
handlePossibleSizeChange();
} else {
// If we're neither maximized nor minimized, the window size
// is changing by the user dragging the window edges. In this
// case, we don't reset the device yet -- we wait until the
// user stops dragging, and a WM_EXITSIZEMOVE message comes.
}
}
break;
case WM_EXITSIZEMOVE:
pause( false );
handlePossibleSizeChange();
break;
case WM_SETCURSOR:
// screensaver
if( mSSMode != SM_NONE ) {
if( mSSMode == SM_FULL && !mSSCheckingPassword ) {
// Hide cursor
SetCursor( NULL );
return true;
}
} else {
// turn off Windows cursor in fullscreen mode
if( mActive && !mWindowed ) {
SetCursor( NULL );
if( mShowCursorWhenFullscreen )
mD3DDevice->ShowCursor( true );
return true;
}
}
break;
case WM_MOUSEMOVE:
if( mSSMode != SM_NONE ) {
if( mSSMode != SM_TEST ) {
static INT xPrev = -1;
static INT yPrev = -1;
INT xCur = GET_X_LPARAM(lParam);
INT yCur = GET_Y_LPARAM(lParam);
if( xCur != xPrev || yCur != yPrev )
{
xPrev = xCur;
yPrev = yCur;
mSSMouseMoveCount++;
if( mSSMouseMoveCount > 5 )
ssInterrupt();
}
}
} else {
if( mActive && mD3DDevice != NULL ) {
POINT ptCursor;
GetCursorPos( &ptCursor );
if( !mWindowed )
ScreenToClient( mHWnd, &ptCursor );
mD3DDevice->SetCursorPosition( ptCursor.x, ptCursor.y, 0 );
}
}
break;
case WM_KEYDOWN:
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
if( mSSMode != SM_NONE && mSSMode != SM_TEST )
ssInterrupt();
break;
case WM_ACTIVATEAPP:
if( wParam == FALSE && mSSMode != SM_NONE && mSSMode != SM_TEST )
ssInterrupt();
break;
case WM_ENTERMENULOOP:
// pause the app when menus are displayed
pause( true );
break;
case WM_EXITMENULOOP:
pause( false );
break;
case WM_NCHITTEST:
// prevent the user from selecting the menu in fullscreen mode
if( !mWindowed )
return HTCLIENT;
break;
case WM_POWERBROADCAST:
if( mSSMode != SM_NONE ) {
if( wParam == PBT_APMSUSPEND && mSSVerifyPasswordProc == NULL )
ssInterrupt();
} else {
switch( wParam ) {
#ifndef PBT_APMQUERYSUSPEND
#define PBT_APMQUERYSUSPEND 0x0000
#endif
case PBT_APMQUERYSUSPEND:
// At this point, the app should save any data for open
// network connections, files, etc., and prepare to go into
// a suspended mode.
return true;
#ifndef PBT_APMRESUMESUSPEND
#define PBT_APMRESUMESUSPEND 0x0007
#endif
case PBT_APMRESUMESUSPEND:
// At this point, the app should recover any data, network
// connections, files, etc., and resume running from when
// the app was suspended.
return true;
}
}
break;
case WM_SYSCOMMAND:
switch( wParam ) {
case SC_NEXTWINDOW:
case SC_PREVWINDOW:
case SC_SCREENSAVE:
case SC_CLOSE:
if( mSSMode == SM_FULL )
return FALSE;
break;
// prevent moving/sizing and power loss in fullscreen mode
case SC_MOVE:
case SC_SIZE:
case SC_MAXIMIZE:
case SC_KEYMENU:
case SC_MONITORPOWER:
if( false == mWindowed )
return 1;
break;
}
break;
case WM_COMMAND:
if( mSSMode == SM_NONE ) {
switch( LOWORD(wParam) ) {
case IDM_CHANGEDEVICE:
// prompt the user to select a new device or mode
pause( true );
userSelectNewDevice();
pause( false );
return 0;
case IDM_TOGGLEFULLSCREEN:
// toggle the fullscreen/window mode
pause( true );
if( FAILED( toggleFullscreen() ) )
displayErrorMsg( (HRESULT)RESETFAILED, APPMUSTEXIT );
pause( false );
return 0;
case IDM_EXIT:
// recieved key/menu command to exit app
SendMessage( hWnd, WM_CLOSE, 0, 0 );
return 0;
}
}
break;
case WM_CLOSE:
{
static boolean closing = false;
if( !closing ) {
closing = true;
close();
HMENU hMenu = GetMenu(hWnd);
if( hMenu != NULL )
DestroyMenu( hMenu );
DestroyWindow( hWnd );
PostQuitMessage(0);
mHWnd = NULL;
}
}
return 0;
case WM_DESTROY:
// TBD: need something?
//shutdownSaver();
break;
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
// A message was received (mouse move, keydown, etc.) that may mean
// the screen saver should show the password dialog and/or shut down.
void CD3DApplication::ssInterrupt()
{
HRESULT hr;
bool passwordOK = false;
if( mSSMode == SM_TEST || mSSMode == SM_FULL && !mSSCheckingPassword ) {
if( mSSIsWin9x && mSSMode == SM_FULL ) {
// If no VerifyPassword function, then no password is set
// or we're not on 9x.
if( mSSVerifyPasswordProc != NULL ) {
// Shut down D3D device so we can show a Windows dialog
cleanup3DEnvironment();
// Make sure window covers the whole screen,
// even after deleting D3D device (which may have caused
// mode changes)
ShowWindow( mHWnd, SW_RESTORE );
ShowWindow( mHWnd, SW_MAXIMIZE );
mSSCheckingPassword = true;
passwordOK = mSSVerifyPasswordProc( mHWnd ) ? true : false;
mSSCheckingPassword = false;
if( passwordOK ) {
// all ok...
} else {
// Back to screen saving...
SetCursor( NULL );
mSSMouseMoveCount = 0;
// Recreate D3D device
hr = initialize3DEnvironment();
return;
}
}
}
doClose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -