//---------------------------------------------------------------------------#include <vcl.h>#include <windows.h>#pragma hdrstop#pragma argsused//---------------------------------------------------------------------------//三个输出函数 extern "C" { __declspec(dllexport) __stdcall void ShowForm(void); __declspec(dllexport) __stdcall bool SetHook(HWND hwindow); __declspec(dllexport) __stdcall bool RemoveHook(void); }HWND hwndmu;HHOOK hhook;HINSTANCE hinstance;bool bs=false;FARPROC oldproc;LRESULT CALLBACK HookProc(int ,WPARAM ,LPARAM);LRESULT CALLBACK WINPROC(HWND hwnd,UINT umsg,WPARAM wparam,LPARAM lparam); bool __stdcall winmu(void);//--------------------------------------------------------------------------int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved){hinstance=hinst ; return 1;}//---------------------------------------------------------------------------//安装WH_GETMESSAGE钩子到指定的线程中bool __stdcall SetHook(HWND hwindow){DWORD thid;if(hhook==NULL)//获取前端窗口句炳,当然也可以用FindWindow(NULL,"MU")来替代thid=GetWindowThreadProcessId(hwindow,NULL);hhook=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)HookProc,hinstance,thid);if(hhook==NULL){ShowMessage("Set Hook fails");return false;}return true;}//---------------------------------------------------------//卸载钩子bool __stdcall RemoveHook(void){ if(hhook==NULL) { ShowMessage("Hook Removed"); return false; } UnhookWindowsHookEx(hhook); return true;}//-------------------------------------------------------//钩子回调函数LRESULT CALLBACK HookProc(int ncode,WPARAM wparam,LPARAM lparam){ if(ncode<0) return CallNextHookEx(hhook,ncode,wparam,lparam);//用一个bool变量让我们的winmu()只调用一次 if (!bs) winmu(); bs=true; return CallNextHookEx(hhook,ncode,wparam,lparam); } //-------------------------------------------------------bool __stdcall winmu(void) { long style; long exstyle; TDeviceMode devmode; hwndmu=GetForegroundWindow(); if(EnumDisplaySettings(0, 0, &devmode)) { devmode.dmPelsWidth=1024; devmode.dmPelsHeight=768; devmode.dmBitsPerPel=32; devmode.dmDisplayFrequency=75; ChangeDisplaySettings(&devmode,0); }//修改窗体的style属性style= GetWindowLong(hwndmu,GWL_STYLE);style=style | WS_CAPTION ;SetWindowLong(hwndmu,GWL_STYLE,style);//修改窗体的exstyle属性exstyle=GetWindowLong(hwndmu,GWL_EXSTYLE);exstyle=exstyle | WS_EX_APPWINDOW | WS_EX_WINDOWEDGE ;SetWindowLong(hwndmu,GWL_EXSTYLE,exstyle);//设置窗体的位置,取消其最前端显示,为图简单807,632是我自己随便设的//当然最好是先用AdjustWindowRect函数调整一下大小SetWindowPos(hwndmu,HWND_NOTOPMOST,0,0,807,632,SWP_SHOWWINDOW);ShowWindow(hwndmu,SW_SHOWNORMAL);//修改窗体的回调函数地址到我们自己定义的回调函数oldproc=(FARPROC)GetWindowLong(hwndmu,GWL_WNDPROC);if(SetWindowLong(hwndmu,GWL_WNDPROC,(long)WINPROC)==0)return false;return true;}//---------------------------------------------------------//自己定义的回调函数LRESULT CALLBACK WINPROC(HWND hwnd,UINT umsg,WPARAM wparam,LPARAM lparam){//消息过滤 switch (umsg) { case WM_ACTIVATEAPP: case WM_ACTIVATE: case WM_KILLFOCUS: case WM_SETFOCUS: case WM_CLOSE: return 0;//这里是个关键,把这个计时器kill case WM_TIMER: if(wparam==0x3e9) KillTimer(hwnd,wparam); break; } return CallWindowProc(oldproc,hwnd,umsg,wparam,lparam); }//----------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop#pragma argsused
//---------------------------------------------------------------------------
//三个输出函数 extern "C"
{
__declspec(dllexport) __stdcall void ShowForm(void);
__declspec(dllexport)
__stdcall bool SetHook(HWND hwindow);
__declspec(dllexport) __stdcall bool RemoveHook(void);
}
HWND hwndmu;
HHOOK hhook;
HINSTANCE hinstance;
bool bs=false;
FARPROC oldproc;
LRESULT CALLBACK HookProc(int ,WPARAM ,LPARAM);
LRESULT CALLBACK WINPROC(HWND hwnd,UINT umsg,WPARAM wparam,LPARAM lparam);
bool __stdcall winmu(void);
//--------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hinstance=hinst ;
return 1;
}
//---------------------------------------------------------------------------
//安装WH_GETMESSAGE钩子到指定的线程中bool __stdcall SetHook(HWND hwindow)
{DWORD thid;
if(hhook==NULL)//获取前端窗口句炳,当然也可以用FindWindow(NULL,"MU")来替代thid=GetWindowThreadProcessId(hwindow,NULL);
hhook=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)HookProc,hinstance,thid);
if(hhook==NULL)
{
ShowMessage("Set Hook fails");
return false;}return true;
}
//---------------------------------------------------------
//卸载钩子bool __stdcall RemoveHook(void)
{
if(hhook==NULL)
{
ShowMessage("Hook Removed");
return false;
}
UnhookWindowsHookEx(hhook);
return true;
}
//-------------------------------------------------------
//钩子回调函数
LRESULT CALLBACK HookProc(int ncode,WPARAM wparam,LPARAM lparam)
{
if(ncode<0)
return CallNextHookEx(hhook,ncode,wparam,lparam);
//用一个bool变量让我们的winmu()只调用一次
if (!bs) winmu();
bs=true;
return CallNextHookEx(hhook,ncode,wparam,lparam);
}
//-------------------------------------------------------
bool __stdcall winmu(void)
{
long style;
long exstyle;
TDeviceMode devmode; hwndmu=GetForegroundWindow();
if(EnumDisplaySettings(0, 0, &devmode))
{
devmode.dmPelsWidth=1024; devmode.dmPelsHeight=768;
devmode.dmBitsPerPel=32;
devmode.dmDisplayFrequency=75;
ChangeDisplaySettings(&devmode,0); }
//修改窗体的style属性
style= GetWindowLong(hwndmu,GWL_STYLE);
style=style | WS_CAPTION ;
SetWindowLong(hwndmu,GWL_STYLE,style);
//修改窗体的exstyle属性exstyle=GetWindowLong(hwndmu,GWL_EXSTYLE);
exstyle=exstyle | WS_EX_APPWINDOW | WS_EX_WINDOWEDGE ;
SetWindowLong(hwndmu,GWL_EXSTYLE,exstyle);
//设置窗体的位置,取消其最前端显示,为图简单807,632是我自己随便
设的
//当然最好是先用AdjustWindowRect函数调整一下大小SetWindowPos(hwndmu,HWND_NOTOPMOST,0,0,807,632,SWP_SHOWWINDOW);
ShowWindow(hwndmu,SW_SHOWNORMAL);
//修改窗体的回调函数地址到我们自己定义的回调函数oldproc=(FARPROC)GetWindowLong(hwndmu,GWL_WNDPROC);
if(SetWindowLong(hwndmu,GWL_WNDPROC,(long)WINPROC)==0)return false;return true;}
//---------------------------------------------------------//
自己定义的回调函数
LRESULT CALLBACK WINPROC(HWND hwnd,UINT umsg,WPARAM wparam,LPARAM lparam)
{
//消息过滤 switch (umsg)
{
case WM_ACTIVATEAPP: case WM_ACTIVATE:
case WM_KILLFOCUS:
case WM_SETFOCUS:
case WM_CLOSE:
return 0;//这里是个关键,
把这个计时器kill
case WM_TIMER:
if(wparam==0x3e9)
KillTimer(hwnd,wparam);
break; }
return CallWindowProc(oldproc,hwnd,umsg,wparam,lparam); }//----------------------------------------------------------
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
hwndmu: HWND;
hhook1: HHOOK;
hinstance: LongWord;
bs: Boolean = false;
oldproc: FARPROC;
function HookProc(nCode: Integer; wParam: WPARAM ; lParam: LPARAM): LRESULT; stdcall;
function WINPROC(hwnd: HWND ;umsg: TMessage; wparam: WPARAM ;lparam: LPARAM): LRESULT; stdcall;
function winmu(): Boolean; stdcall;implementation{$R *.dfm} // unsigned long reason, void* lpReservedfunction DllEntryPoint(hinst: LongWord): integer;
begin
hinstance := hinst ;
result := 1;
end;//安装WH_GETMESSAGE钩子到指定的线程中
function SetHook(hwindow: HWND): Boolean;Stdcall;
var
thid: DWORD;
begin
//if(hhook == NULL) 获取前端窗口句炳,当然也可以用FindWindow(NULL,"MU")
//来替代
thid := GetWindowThreadProcessId(hwindow,nil);
hhook1 := SetWindowsHookEx(WH_GETMESSAGE,HookProc,hinstance,thid);
if hhook1 = NULL then
begin
ShowMessage('Set Hook fails');
result := false;
end
else
result := true;
end;//卸载钩子
function RemoveHook():Boolean;Stdcall;
begin
if hhook1 = NULL then
begin
ShowMessage('Hook Removed');
result := false;
end
else
begin
UnhookWindowsHookEx(hhook1);
result := true;
end;
end;//钩子回调函数
function HookProc(nCode: Integer; wParam: WPARAM ; lParam: LPARAM): LRESULT;
begin
if ncode < 0 then
result := CallNextHookEx(hhook1,ncode,wparam,lparam);
//用一个bool变量让我们的winmu()只调用一次
if bs <> winmu() then
begin
bs := true;
result := CallNextHookEx(hhook1,ncode,wparam,lparam);
end;
end;function winmu(): Boolean; stdcall;
var
style,exstyle: DWORD;
devmode: TDeviceMode;
begin
hwndmu := GetForegroundWindow();
if EnumDisplaySettings(nil, 0, devmode) then
begin
devmode.dmPelsWidth := 1024;
devmode.dmPelsHeight := 768;
devmode.dmBitsPerPel := 32;
devmode.dmDisplayFrequency := 75;
ChangeDisplaySettings(devmode,0);
end;
//修改窗体的style属性
style := GetWindowLong(hwndmu,GWL_STYLE);
style := style or WS_CAPTION ;
SetWindowLong(hwndmu,GWL_STYLE,style);
//修改窗体的exstyle属性exstyle=GetWindowLong(hwndmu,GWL_EXSTYLE);
exstyle := exstyle or WS_EX_APPWINDOW or WS_EX_WINDOWEDGE;
SetWindowLong(hwndmu,GWL_EXSTYLE,exstyle);
//设置窗体的位置,取消其最前端显示,为图简单807,632是我自己随便设的
//当然最好是先用AdjustWindowRect函数调整一下大小
//SetWindowPos(hwndmu,HWND_NOTOPMOST,0,0,807,632,SWP_SHOWWINDOW);
ShowWindow(hwndmu,SW_SHOWNORMAL);
//修改窗体的回调函数地址到我们自己定义的回调函数
//oldproc=(FARPROC)GetWindowLong(hwndmu,GWL_WNDPROC);
if SetWindowLong(hwndmu,GWL_WNDPROC,WINPROC) = 0 then
Result := false
else
Result := True;
end;
end;//自己定义的回调函数
function WINPROC(hwnd: HWND, umsg :TMessage, WPARAM wparam, LPARAM lparam): LRESULT;
begin
//消息过滤
case umsg.Msg of
WM_ACTIVATEAPP: result := 0;//这里是个关键,
WM_ACTIVATE: result := 0;
WM_KILLFOCUS: result := 0;
WM_SETFOCUS: result := 0;
WM_CLOSE: result := 0;
WM_TIMER:
begin
if wparam = 0x3e9 then
begin
KillTimer(hwnd,wparam); //把这个计时器kill
break;
end;
result := CallWindowProc(oldproc,hwnd,umsg,wparam,lparam);
end;
end;
end;end.
function RemoveHook():Boolean;Stdcall;
begin
if hhook1 = NULL then应该是 if hhook1 = nil then
if hhook1 = NULL then
//自己定义的回调函数
function WINPROC(hwnd: HWND, umsg :TMessage, WPARAM wparam, LPARAM lparam): LRESULT; //明显有错误??
begin
//消息过滤
case umsg.Msg of
WM_ACTIVATEAPP: result := 0;//这里是个关键,
WM_ACTIVATE: result := 0;
WM_KILLFOCUS: result := 0;
WM_SETFOCUS: result := 0;
WM_CLOSE: result := 0;
WM_TIMER:
begin
if wparam = 0x3e9 then
begin
KillTimer(hwnd,wparam); //把这个计时器kill
break;
end;
result := CallWindowProc(oldproc,hwnd,umsg,wparam,lparam);
end;
end;
end;