一直有人问如何在DirectX全屏游戏中弹出窗口就象金山游侠一样.我答应过要给出原码,只是一直没有时间整理,不过现在总算是弄玩了.代码不长,大致作了些注释,但愿你能看懂:)
按照我的说明一步步作应该就能成功,但有时可能不行,为什么?我也不知道,或许是你哪一步做错了,或者是Delphi的问题?天知道,但大多数时候按照我给出的步骤,这些代码应该能实现我们的目标.
下面的代码经过了一定的测试,但并没有刻意设计保证程序兼容性和稳定性的代码,所以不能保证在所有的机器上正常运行.如果死机或者花屏了,那么很不幸它不适合你,在找些别人写的类似的代码吧(不过以前有人公开过类似的代码吗?如果有请mail给我:)
试一试吧,希望我们能把它完善起来.
{***************HOOK.DLL************
FileName:Hook.dpr(The KEY unit to pop up a window in DX apps)
Author: tTui or tt.t (As u like ;)
Feature:This unit contain the Demo codes for pop up an MODAL window in Apps which use exclusive directX fullscreen mode.
Description: 1.Uses KeyBoard hook to hook the hotkey.
2.Uses s0me tricks to get the *real* IDirectDraw pointer.
3.Call the *IDirectDraw.FilptoGDISurface* to make sure the poped up window could be seen.(See MSDN for the reason)
4.Uses GetMessage hook to hook the WM_TIMER,WM_SETFOCUS... messages.(Why?I don't want to tell u :) Find the reason by urself)
5.The HotKey is Left WIN + NumPad *
6.Mute codes needed, but havn't wrote yet.
7.Complied with Delphi 6. Tested under Win98&SE, Win ME, Win 2K,Win XP and Win 2003.NET with DirectX 8&9.
Known Bugs: 1.Cannot repaint the background when the poped up window moved.
2.May crash when try to pop up from some games and apps.
3.Cannot show the cursor in some games.
4.May minimize the main App, when try to pop up the window.
5.Many more...but unknown yet...
MY MAIL: ttui@163.com
BTW, if u want to pop up an MODALLESS window, u should write the codes all by urself.
*DO NOT* ask me for that.
***********************************}
library Hook;
uses
SysUtils,
Classes,
Windows,
Messages,
Dialogs,
DirectDraw, //*Modified* Jedi's DirectX header file for Delphi.
FormUnit in 'FormUnit.pas' {Form1}; //The unit contains the popup window.
{$R *.res}
type
PHookRec = ^THookRec;
THookRec = record
ParentWnd:HWND; //The main app's handle
FormWnd:HWND; //Handle of the popup window
Poped:Boolean; //A flag. eq True if the window poped
HH1:HHOOK; //Hook handle of the keyboard hook 
HH2:HHOOK; //Hook handle of the GetMessage hook
end;
var
rHookRec: PHookRec = nil;
hMapObject: THandle = 0;
var
pDirectDrawCreate:function (lpGUID: PGUID;out lplpDD: IDirectDraw;pUnkOuter: IUnknown) : HResult; stdcall;
function WHGETMESSAGE(iCode:Integer;wParam: WPARAM;lParam: LPARAM):LRESULT; stdcall;
begin
result:=0;
if iCode<0 then
begin
CallNextHookEx(rHookRec^.HH2,iCode,wParam,lParam);
result:=0;
Exit;
end;
case PMSG(lParam)^.message of
WM_TIMER, //$113
WM_WINDOWPOSCHANGING, //$47
WM_SETCURSOR, //$20
WM_ACTIVATEAPP, //$1c
WM_SETFOCUS: //$7
begin //Some other messages should be processed here.
PMSG(lParam)^.message:=0;
end;
end;
end;
function HookProc(iCode:Integer;wParam: WPARAM;lParam: LPARAM):LRESULT; stdcall;
var
dh:dword;
FD:IDirectDraw;
pp:pointer;
a:dword;
sc:integer;
begin
result:=0;
if iCode<0 then
begin
CallNextHookEx(rHookRec^.HH1,iCode,wParam,lParam);
result:=0;
Exit;
end;
if ((lParam and $80000000)=0) and
(GetKeyState(VK_LWIN)<0) and (wParam=$6a) then //The HotKey is Left WIN + NumPad *
begin
rHookRec^.ParentWnd:=getforegroundwindow;
if not isWindow(rHookRec^.ParentWnd) then exit;
try
if not rHookRec^.Poped then
begin
dh:=GetModuleHandle('ddraw.dll'); //is a dx app??
if dh<>0 then
begin
dh:=dword(GetProcAddress(dh,'DirectDrawCreate'));
if dh<>0 then
begin
pDirectDrawCreate:=Pointer(dh);
if pDirectDrawCreate(nil,FD,nil)=0 then
begin
pp:=@fd;
a:=dword(pointer(dword(pp^)+8)^); //Now a is the pointer to the *REAL* IDirectDraw
asm //Call FliptoGDISurface
mov eax,a
push eax
mov eax,[eax]
call [eax+$28]
end;
FD:=nil;
end;
end;
end;
rHookRec^.HH2:=setwindowshookex(WH_GETMESSAGE,@WHGETMESSAGE,0,GetCurrentThreadID);
sc:=ShowCursor(true); //Show cursor
form1:=tform1.CreateParented(rHookRec^.ParentWnd); //Create the window that'll pop up
rHookRec^.Poped:=true; //set flag
rHookRec^.FormWnd:=form1.Handle;
form1.ShowModal; //Bingo!! The window pops up!!
form1.Free;
rHookRec^.Poped:=false; //set flag
UnhookWindowshookEx(rHookRec^.HH2);
if sc>=0 then
ShowCursor(true)
else
ShowCursor(false);
end;
finally
end;
result:=1;
end;
end;
function sethook:bool;export; //Call the func to set the keyboard hook
begin
result:=false;
if rHookRec^.HH1<>0 then exit; 
rHookRec^.Poped:=False;
rHookRec^.HH1 := SetWindowsHookEx(WH_KEYBOARD,hookproc,HInstance,0);
Result := rHookRec^.HH1 <> 0;
end;
function endhook:bool;export; //Call the func to unhook the keyboard hook
begin
if rHookRec^.HH1 <> 0 then
begin
UnhookWindowshookEx(rHookRec^.HH1);
rHookRec^.HH1 := 0;
end;
Result := rHookRec^.HH1 = 0;
end;
procedure EntryPointProc(Reason: Integer); //Create and Close the file mapping to share data in different processes.
begin
case reason of
DLL_PROCESS_ATTACH:
begin
hMapObject := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(THookRec), '_Popup_A_Wnd_DEMO_');
rHookRec := MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(THookRec));
end;
DLL_PROCESS_DETACH:
begin
try
UnMapViewOfFile(rHookRec);
CloseHandle(hMapObject);
except
end;
end;
end;
end;
Exports
SetHook,
EndHook;
begin
DllProc := @EntryPointProc;
EntryPointProc(DLL_PROCESS_ATTACH);
end.
//==================================================
{*************FormUnit.pas**********
FileName:FormUnit.pas
Author: tTui or tt.t (As u like ;)
Description: This unit contains the codes of the popup window.
MY MAIL: ttui@163.com
TIPS:The form's BoaderStyle property must be "bsDialog" or the popup window may not be seen.
***********************************}
unit FormUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); //u can add other VCL components.
private
{ Private declarations }
procedure WndProc(var Message: TMessage); override;
public
{ Public declarations }
end;
type
PHookRec = ^THookRec;
THookRec = record
ParentWnd:HWND;
FormWnd:HWND;
Poped:Boolean;
HH1:HHOOK;
HH2:HHOOK;
end;
var
Form1: TForm1;
TILC_Message:Cardinal; //Exit message
rHookRec: PHookRec = nil;
hMapObject: THandle = 0;
implementation
{$R *.dfm}
procedure TForm1.WndProc(var Message: TMessage);
begin
inherited WndProc(Message);
if Message.Msg=TILC_Message then 
Close;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
TILC_Message:=RegisterWindowMessage(pchar('Poooop!!'));
hMapObject := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(THookRec), '_Popup_A_Wnd_DEMO_');
rHookRec := MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(THookRec));
// the popup window cann't access its handle via its property "form.handle" or an exception'll rise.
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
try
UnMapViewOfFile(rHookRec);
CloseHandle(hMapObject);
except
end;
end;
end.
//========================================
{***************Test.pas************
FileName:Test.pas
Author: tTui or tt.t (As u like ;)
Description: This unit demostrates how to use HOOK.DLL.
File->New->Application
MY MAIL: ttui@163.com
***********************************}
unit Test;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
function sethook:Bool;External 'Hook.DLL';
function endhook:Bool;External 'Hook.DLL';
var
Form1: TForm1;
TILC_Message:Cardinal;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if Button1.Caption='SetHook' then
begin
SetHook;
Button1.Caption:='EndHook';
end
else
begin
Button1.Caption:='SetHook';
EndHook;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
EndHook;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
TILC_Message:=RegisterWindowMessage(pchar('Poooop!!'));
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
r:DWORD;
begin
r:=BSM_APPLICATIONS;
BroadcastSystemMessage(BSF_QUERY,@r,TILC_Message,0,0); //Broadcast the exit message when quit.
end;
end.
//===============================
Finally, we must modify the DirectDraw.pas to prevent to load the ddraw.dll when the application runs.
Find the initialization part at the end of DirectDraw.pas and add 
"if false then" before "if not IsNTandDelphiRunning then".

DirectX全屏游戏中弹出窗口(转)的更多相关文章

  1. OBS录制全屏游戏的方法(超好录屏)

    新版Windows设置 详见 https://github.com/obsproject/obs-studio/wiki/Laptop-Troubleshooting 新版的Windows 10: l ...

  2. MFC全屏显示和多窗口动态显示的一些技巧和方法

    一.全屏 1.全屏窗口从dialogex继承,因为要处理一些东西 2.全屏代码,这样设置后尺寸不会出bug,只设置为最大值的话容易出bug //get current system resolutio ...

  3. nvidia 无显示选项怎么设置全屏游戏

    转自:2楼   http://nbbbs.zol.com.cn/41/218_408871.html 网上搜的方法: 1.按键盘上那个windows键+R,输入regedit 2.然后就是下面的步骤了 ...

  4. JS只弹出一个居中弹出窗口

    var newWindow;//定义一个窗口,有利于窗口间的通讯function makeNewWindow(url) {   if (!newWindow || newWindow.closed) ...

  5. webdriver设置浏览器全屏及设置浏览器窗口为特定大小的方法

    from selenium import webdriver driver = webdriver.Chrome() #全屏 driver.maximize_window() #具体大小 driver ...

  6. CEF中弹出窗口的处理

    CEF开发如果不想在弹出窗口中打开网页,即想要在当前窗体加载目标Url, 就需要重写OnBeforePopup,它是属于CefLifeSpanHandler类中的. /*--cef(optional_ ...

  7. 在IE7+ 中弹出窗口并关闭本身窗口的脚本(备忘)

    window.onload =function(){ window.open("http://www.126.com"); window.opener=null; window.o ...

  8. win7系统玩游戏不能全屏的解决办法

    1.修改注册表中的显示器的参数设置   Win键+R键,打开运行窗口,输入regedit回车,这样就打开了注册表编辑器,然后,定位到以下位置:   HKEY_LOCAL_MACHINE\SYSTEM\ ...

  9. QT中关于窗口全屏显示与退出全屏的实现

    近期在学习QT时遇到了很多问题这也是其中一个,个人通过在各种书籍和网络上的查阅找到了一些关于这方面的答案,希望能给大家一些帮助. 首先,在QT中对于窗口显示常用的有这么几个方法可以调用: Qt全屏显示 ...

随机推荐

  1. Axure RP 快速原型设计工具

       Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或Web网站的线框图 ...

  2. ajax与302响应

    在ajax请求中,如果服务器端的响应是302 Found,在ajax的回调函数中能够获取这个状态码吗?能够从Response Headers中得到Location的值进行重定向吗?让我们来一起看看实际 ...

  3. 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+kibanaV5.4.2+x-packV5.4.2安装

    相关博文: 搜索引擎ElasticSearchV5.4.2系列一之ES介绍 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+klanaV5.4.2+x-p ...

  4. springcloud Zuul中异常处理细节

    Spring Cloud Zuul对异常的处理整体来说还是比较方便的,流程也比较清晰,只是由于Spring Cloud发展较快,各个版本之间有差异,导致有的小伙伴在寻找这方面的资料的时候经常云里雾里, ...

  5. python3.3中print换行

    python  3.3版本中的print默认有个换行的操作 如: for i in range(5): print(i) 结果为: 01234 如果不想换行,需要用到print函数的end参数,pri ...

  6. stylus项目知识点

    1.在项目中,引入.sty文件的时候,用来下面方式 @import "~common/stylus/variable.styl" ~ 是stylus的写法,参考https://gi ...

  7. 洛谷P3379倍增LCA

    传送门 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> ...

  8. MySQL一问一答

    一.问:如果有一张表,里面有个字段为id的自增主键,当已经向表里面插入了10条数据之后,删除了id为8,9,10的数据,再把mysql重启,之后再插入一条数据,那么这条数据的id值应该是多少,是8,还 ...

  9. 2018年东北农业大学春季校赛 C-wyh的商机

    一天,你们wyh学长和你们zhl学长玩一个游戏,这个游戏规则是这样的 给你n个城市,保证这n个城市之间都只有一条道路可以到达. 有一件物品,在所有城市中都是一样的,但是由于各个城市的经济发展不同,导致 ...

  10. Vue.js学以致用之遇到的那些坑

    前段时间的react授权许可的闹剧告诉大家一个问题,只有自己的东西用着才放心.各大巨头也逐渐明白使用自家东西的优势.本来vue的生态就愈加火热,这次的闹剧无疑又加速了vue的发展.当下,国内越来越多的 ...