这两个函数具体应用如下:SetWindowLong函数GetWindowLong函数
Delphi窗口化游戏
var
Thwnd:HWND;//声明变量 句柄变量
devmodel1:DEVMODE;//屏幕分辨率变量
style,exstyle:longint;//窗口属性变量
begin
//得到游戏句柄
Thwnd:findwindow(‘CLIENT‘,nil);
//设置分辨率
if EnumDisplaySettings(0,0,devmode1)then
begin
devmode1.dmPelsWidth:1024;
devmode1.dmPelsHeight:=768;
devmode1.dmBitsPerPel:=32;
devmode1.dmDisplayFrequency:=75;
ChangeDisplaySetting(devmode1,0);
end;
//得到窗口原来的属性
style:=GetWindowLong(Thwnd,GWL_STYLE);
//定义新属性
style:=WS_OVERLAPPEDWINDOW or WS_VISIBLE;
//更改窗口属性
SetWindowLong(Thwnd,GWL_STYLE,style);
//得到窗口扩展属性
exstyle:=GetWindowLong(Thwnd,GWL_EXSTYLE);
//定义新扩展属性
exstyle:=WS_EX_APPWINDOW or WS_EX_WINDOWEDGE;
//更改窗口扩展属性
SetWindowLong(Thwnd,GWL_EXSTYLE,exstyle);
//更改的屏幕以屏幕的0,0为坐标,大小为800*600
SetWindowPos(Thwnd,HWND_NOTOPMOST,0,0,1020,768,SWP_SHOWWINDOW);
ShowWindow(Thwnd,SW_SHOWNORMAL);
二、WS_EX_APPWINDOW在任务栏显示图标
SetWindowLong(Handle,GWL_EXSTYLE,Style or WS_EX_APPWINDOW)
三、修改托盘相关信息
unit Unit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,ShellApi,StdCtrls;
const
//声明常量
WM_MYNOTIFY=WM_USER+1000;//消息
ID_MAIN=100;//定义图标的ID
type
TForm1=class(TForm)
Button1: TButton;
Button2: TButton;
popupmenu1:TPopupmenu;
procedure FormCreate(Sender:TObject);
procedure FormClose(Sender: TObject;var Action: TCloseAction);
//声明私有函数
Function AddIcon(hwnd: HWND):Boolean;
Function RemoveIcon(hwnd: HWND):Boolean;
Procedure MyNotify(var Msg: TMessage);message WM_MYNOTIFY;
private
{Privatedeclarations}
ExitApp:Boolean;
public
{Publicdeclarations}
end;
var
Form1:TForm1;
implementation
{$R *.dfm}
//函数实现部分
Function TForm1.AddIcon(hwnd:HWND):Boolean;
var
nid:NOTIFYICONDATA;//系统消息对象
begin
nid.cbSize:=sizeof(NOTIFYICONDATA);//对象存储大小
nid.Wnd:=hwnd;//消息对象句柄为窗口句柄
nid.uID:=ID_MAIN;//图标的ID号
nid.uFlags:=7; //NIF_MESSAGE or NIF_ICON or NIF_TIP;//响应的事件
nid.uCallbackMessage:=WM_MYNOTIFY;//返回信息
nid.hIcon:=Application.Icon.Handle;//图标
strCopy(nid.szTip,‘hello‘);//转换字符串格式
AddIcon:=Shell_NotifyIcon(NIM_ADD,@nid);//添加到任务栏
end;
Function TForm1.RemoveIcon(hwnd:HWND):Boolean;
var
nid:NOTIFYICONDATA;
begin
nid.cbSize:=sizeof(NOTIFYICONDATA);
nid.Wnd:=hwnd;
nid.uID:=ID_MAIN;
nid.uFlags:=0;
RemoveIcon:=Shell_NotifyIcon(NIM_DELETE,@nid);
end;
Procedure TForm1.MyNotify(var Msg:TMessage);
var
mousept: TPoint;
begin
if frm_login.Visible=false then
begin
case Msg.LParam of
WM_LBUTTONDBLCLK: //鼠标左键按下WM_LBUTTONDOWN,左键双击WM_LBUTTONDBLCLK,左键弹起WM_LBUTTONUP,右键弹起WM_RBUTTONUP
begin
ShowWindow(Handle, SW_SHOW);
ShowWindow(Application.handle, SW_SHOW);
SetWindowLong(Application.Handle, GWL_EXSTYLE,
not (GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW));
end;
end;
case Msg.LParam of
WM_RBUTTONUP:
begin
GetCursorPos(mousept);
Form1.popupmenu1.popup(mousept.x, mousept.y);
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender:TObject);
begin
ExitApp:=False;
SetWindowLong(application.handle,gwl_exstyle,ws_ex_toolwindow);
AddIcon(handle);//添加图标
end;
procedure TForm1.FormClose(Sender: TObject;var Action: TCloseAction);
begin
if ExitApp then
Action:= caFree
else
Action:= caNone;
//隐藏窗体.程序并没有退出
ShowWindow(Handle, SW_HIDE);
ShowWindow(Application.Handle, SW_HIDE);
SetWindowLong(Application.Handle, GWL_EXSTYLE,
GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW);
end;
end.
四、从任务栏隐藏该使用
ShowWindow(Application.Handle,SW_HIDE);
显示
ShowWindow(Application.Handle,SW_SHOW);
五、
{将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
六、
参考:
如果你下载(源程序下载)并运行这个例程序,你会发现如果我们在托盘图标上点击鼠标右键,则会弹出一个右键菜单(如右图)。如果点击相应的菜单项,程序主窗体会随之变化,这样就可以控制程序的行为。而如果当主窗体处于最小化状态时,我们在托盘图标上点击左键,窗体会回复到原来的大小。其实实现上述的功能都要依赖于WINDOWS操作系统的消息机制,要完全弄懂这个机制挺不容易的,但是我们可以按下述文字来理解它。