vc 窗体隐藏的情况下不能hook键盘
我用的是VS2010里面的VC,我写了一个DLL,代码如下://cpp文件:#include<Windows.h>HWNDDll_hWindow=NULL;LRESULT...
我用的是VS2010里面的VC,我写了一个DLL,代码如下:
//cpp文件:
#include<Windows.h>
HWND Dll_hWindow=NULL;
LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
switch(wParam)
{
case VK_F1:
if(!(lParam&0x40000000))
{
ShowWindow(Dll_hWindow,SW_HIDE);
}
break;
case VK_F2:
if(!(lParam&0x40000000))
{
ShowWindow(Dll_hWindow,SW_SHOW);
}
break;
}
return 0;
}
HHOOK SetHook(HWND _hWindow)
{
Dll_hWindow=_hWindow;
return SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("Hook.dll"),0);
}
//def文件:
LIBRARY Hook
EXPORTS
SetHook
使用DLL来监测键盘按键,如果按F1,则隐藏窗口,如果按F2,则显示窗口。
程序的cpp文件:
#include<Windows.h>
HHOOK hHook=NULL;
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg, WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
UnhookWindowsHookEx(hHook);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASS appclass;
appclass.cbClsExtra =0;
appclass.cbWndExtra =0;
appclass.hbrBackground =HBRUSH(COLOR_WINDOW);
appclass.hCursor =LoadCursor(NULL,MAKEINTRESOURCE(IDC_APPSTARTING));
appclass.hIcon =LoadIcon(NULL,MAKEINTRESOURCE(IDI_APPLICATION));
appclass.hInstance =hInstance;
appclass.lpfnWndProc =WindowProc;
appclass.lpszClassName ="app";
appclass.lpszMenuName =NULL;
appclass.style =CS_HREDRAW | CS_VREDRAW;
RegisterClass(&appclass);
RECT rDesktop;
GetWindowRect(GetDesktopWindow(),&rDesktop);
int x=rDesktop.right /2-250,y=rDesktop.bottom /2-150;
HWND hWindow=CreateWindow("app","旭迅一键隐藏",WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,x,y,500,300,NULL,NULL,hInstance,NULL);
UpdateWindow(hWindow);
typedef HHOOK(*_SetHook)(HWND);
_SetHook SetHook=_SetHook(GetProcAddress(LoadLibrary("Hook.dll"),"SetHook"));
hHook=SetHook(hWindow);
MSG mWindow;
while(GetMessage(&mWindow,NULL,0,0))
{
TranslateMessage(&mWindow);
DispatchMessage(&mWindow);
}
}
问题是我按了F1能隐藏窗口,但按F2就不能显示窗口了,请问这是怎么回事? 展开
//cpp文件:
#include<Windows.h>
HWND Dll_hWindow=NULL;
LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
switch(wParam)
{
case VK_F1:
if(!(lParam&0x40000000))
{
ShowWindow(Dll_hWindow,SW_HIDE);
}
break;
case VK_F2:
if(!(lParam&0x40000000))
{
ShowWindow(Dll_hWindow,SW_SHOW);
}
break;
}
return 0;
}
HHOOK SetHook(HWND _hWindow)
{
Dll_hWindow=_hWindow;
return SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("Hook.dll"),0);
}
//def文件:
LIBRARY Hook
EXPORTS
SetHook
使用DLL来监测键盘按键,如果按F1,则隐藏窗口,如果按F2,则显示窗口。
程序的cpp文件:
#include<Windows.h>
HHOOK hHook=NULL;
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg, WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
UnhookWindowsHookEx(hHook);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASS appclass;
appclass.cbClsExtra =0;
appclass.cbWndExtra =0;
appclass.hbrBackground =HBRUSH(COLOR_WINDOW);
appclass.hCursor =LoadCursor(NULL,MAKEINTRESOURCE(IDC_APPSTARTING));
appclass.hIcon =LoadIcon(NULL,MAKEINTRESOURCE(IDI_APPLICATION));
appclass.hInstance =hInstance;
appclass.lpfnWndProc =WindowProc;
appclass.lpszClassName ="app";
appclass.lpszMenuName =NULL;
appclass.style =CS_HREDRAW | CS_VREDRAW;
RegisterClass(&appclass);
RECT rDesktop;
GetWindowRect(GetDesktopWindow(),&rDesktop);
int x=rDesktop.right /2-250,y=rDesktop.bottom /2-150;
HWND hWindow=CreateWindow("app","旭迅一键隐藏",WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,x,y,500,300,NULL,NULL,hInstance,NULL);
UpdateWindow(hWindow);
typedef HHOOK(*_SetHook)(HWND);
_SetHook SetHook=_SetHook(GetProcAddress(LoadLibrary("Hook.dll"),"SetHook"));
hHook=SetHook(hWindow);
MSG mWindow;
while(GetMessage(&mWindow,NULL,0,0))
{
TranslateMessage(&mWindow);
DispatchMessage(&mWindow);
}
}
问题是我按了F1能隐藏窗口,但按F2就不能显示窗口了,请问这是怎么回事? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询