C++ 怎么获取最前端的焦点窗口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 怎么获取最前端的焦点窗口相关的知识,希望对你有一定的参考价值。
比如我开着我的软件在后台,怎么判断当前焦点是浏览器然后就开始调用我代码
//1HWND hWnd = GetForegroundWindow(void);//获取窗口句柄
//2
DWORD dwProcessID = 0;
GetWindowThreadProcessId( hWnd , &dwProcessID );
//保证有SeDebugPrivilege优先权,
HANDLE hToken;
if ( !OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES, &hToken))
//GetLastError( );...出错处理
return;
LUID uID;
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &uID);
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = uID;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
CloseHandle(hToken);
//3
HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS ,
FALSE , dwProcessID );
//4
TCHAR pszProcessName[50] = 0 ;
GetModuleFileName( hProcess , pszProcessName , 50 );//获取进程名
//6
CloseHandle( hProcess );
//7
//假设浏览器是ie,
CString strName(_T("iexplore.exe") );
if( !strName.CompareNoCase( pszProcessName ) )
//如果当前在操作ie浏览器
//注意要保证后台自己程序运行,而不是挂起
//如果是浏览器窗口有名字,可以使用
HWND hOneWnd = FindWindow( NULL , _T("窗口名") );
//直接比较句柄就行就不用上面的获取进程名的方式
//但有些程序没窗口名字,或窗口名字动态变化,这种方式就不行了
MSDN上这样说GetActiveWindow:
The GetActiveWindow function retrieves the window handle to the
active window attached to the calling thread's message queue.
他获取的是当前进程中当前正在使用的(激活的)窗口
参考技术A GetActiveWindow追问怎么用??
C++ Directx 鼠标输入在失去焦点后表现怪异
【中文标题】C++ Directx 鼠标输入在失去焦点后表现怪异【英文标题】:C++ Directx mouse input acting weird after focus lost 【发布时间】:2014-06-11 17:12:39 【问题描述】:您好,在我失去窗口焦点(alt+tab / windows 键)后,我的鼠标输入出现了一个小问题。
焦点回到游戏窗口后,鼠标(偏航)动作缓慢且没有响应(由于某种原因,俯仰工作正常)。有时它会动,有时它根本不动。
以下是部分代码:
bool Game::initDirectInput(HINSTANCE hInstance)
hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInput, NULL);
hr = directInput->CreateDevice(GUID_SysKeyboard, &DIKeyboard, NULL);
hr = directInput->CreateDevice(GUID_SysMouse, &DIMouse, NULL);
hr = DIKeyboard->SetDataFormat(&c_dfDIKeyboard);
hr = DIKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
hr = DIMouse->SetDataFormat(&c_dfDIMouse2);
hr = DIMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE | DISCL_NOWINKEY);
return true;
void Game::detectInput(double delta)
DIMOUSESTATE2 mouseCurrState;
BYTE keyboardState[256];
DIKeyboard->Acquire();
DIMouse->Acquire();
DIMouse->GetDeviceState(sizeof(DIMOUSESTATE2), &mouseCurrState);
DIKeyboard->GetDeviceState(sizeof(keyboardState),(LPVOID)&keyboardState);
camera.detectInput(delta, &keyboardState[0], &mouseCurrState);
void Camera::detectInput(double delta, BYTE *keyboardState, DIMOUSESTATE2 *mouseCurrState)
float speed = 16.0f * delta;
...
if((mouseCurrState->lX != mouseLastState.lX) || (mouseCurrState->lY != mouseLastState.lY))
camYaw += mouseCurrState->lX * 0.002f;
camPitch += mouseCurrState->lY * 0.002f;
if(camPitch > 0.85f)
camPitch = 0.85f;
if(camPitch < -0.85f)
camPitch = -0.85f;
mouseLastState = *mouseCurrState;
update();
return;
void Camera::update()
camRotationMatrix = XMMatrixRotationRollPitchYaw(camPitch, camYaw, 0);
camTarget = XMVector3TransformCoord(DefaultForward, camRotationMatrix );
camTarget = XMVector3Normalize(camTarget);
XMMATRIX RotateYTempMatrix;
RotateYTempMatrix = XMMatrixRotationY(camYaw);
camRight = XMVector3TransformCoord(DefaultRight, RotateYTempMatrix);
camForward = XMVector3TransformCoord(DefaultForward, RotateYTempMatrix);
camUp = XMVector3Cross(camForward, camRight);
camPosition += moveLeftRight*camRight;
camPosition += moveBackForward*camForward;
camPosition += moveUpDown*DefaultUp;
moveLeftRight = 0.0f;
moveBackForward = 0.0f;
moveUpDown = 0.0f;
camTarget = camPosition + camTarget;
camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );
对于那些想知道的人,我刚刚开始使用本教程学习 c++ 和 directx:http://www.braynzarsoft.net/index.php?p=DX11Lessons (虽然我有相当多的使用 OpenGL + java 的经验。)
【问题讨论】:
不要将 DirectInput 用于鼠标或键盘。 在 Windows XP 或更高版本上,DirectInput 是对现有 Win32 消息WM_MOUSE*
、@987654324 之上的一点破解@,使用挂钩 API,因此只需自己使用 Win32 消息。目前 DirectInput 的唯一用途是使用 HID 操纵杆/控制器。
【参考方案1】:
我通过添加一个简单的 if 语句解决了这个问题:
if(mouseLastState.lX > -10000 && mouseLastState.lX < 10000)
camYaw += mouseLastState.lX * 0.002f;
问题是当窗口重新获得焦点时,它会返回一个如此高的值,以至于偏航不再有效。
【讨论】:
以上是关于C++ 怎么获取最前端的焦点窗口的主要内容,如果未能解决你的问题,请参考以下文章