几个API调用的例子

作者在开发个人软件“视窗超人”时,较多地应用了Win32API,现在VB开发环境下调用举例,与大家交流经验。
1.拖动无标题的窗体
第一种方法移动时窗口不显示窗口内容,即在移动时只显示虚框
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_MOVE = &HF012
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture'释放鼠标捕获
SendMessage hwnd, WM_SYSCOMMAND, SC_MOVE, 0'发送窗口移动消息
End Sub
第二种方法移动时窗口显示窗口内容,即如同移动一个实体,(如您的窗口风格有标题栏,此代码对窗口位置的定位将有偏差,请作局部调整)
在Load 事件或运行前将窗口的ScaleMode设为3 即用像素作内部度量单位
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim MousePoint As POINTAPI, MouseSize As POINTAPI
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
SetCapture (hwnd)'设置鼠标捕获
Tag = "1" '将其作为移动标志
MouseSize.X = X
MouseSize.Y = Y
'记忆鼠标按下时的光标位置
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Tag = "1" Then
Call GetCursorPos(MousePoint)'返回鼠标在屏幕中的位置
Left = Screen.TwipsPerPixelX * (MousePoint.X - MouseSize.X)
Top = Screen.TwipsPerPixelY * (MousePoint.Y - MouseSize.Y)
'设置窗口位置
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Tag = ""
ReleaseCapture '释放鼠标
End Sub
VB和Delphi有颇多相似,二者代码可轻易转换作对方,现再以Delphi建立系统栏图标举几个API的例子,并且包括自定义消息和对系统消息的自定义处理,如果您想追求更小的exe和更了解系统而不是使用别人封装过的控件,请看以下举例
2.系统栏图标处理
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics,Controls, Forms, Dialogs,
ExtCtrls, shellapi;//在引用中加入shellapi
Const wm_MyCall = wm_User+$1000;//自定义消息

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure wndProc(var Msg: TMessage) ; override;
procedure FormDestroy(Sender: TObject);//重载并处理自定义消息的模块
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Ico : TNotifyIconData;//声明一个实例
implementation

{$R *.DFM}
procedure SysTrayIco(msgs:Dword;icos:Ticon=Nil);//处理任务栏图标过程
begin
if icos=nil then//如果为缺省空值,表示要从系统栏删除此图标
Shell_NotifyIcon(nim_Delete,@ico)
else
begin
With ico do//加入TrayIcon
Begin
cbSize := SizeOf(Ico); //初始化大小为实例大小
Wnd := form1.handle; //有效窗口句柄
uID := 0; //一个应用程序在系统
uFlags := nif_Icon Or NIF_MESSAGE Or nif_Tip;//设定有效域为图标、消息和提示
uCallBackMessage := wm_MyCall; //对应的消息处理
hicon:=icos.handle; //图标
lstrcpy(szTip,'左键弹出系统菜单,右键更改图标');//设定点鼠标经过时的提示
Shell_NotifyIcon(msgs,@Ico);
End;
end;
end;
procedure Tform1.wndProc(var Msg: TMessage) ;//重载并自定义消息
var MousePoint:TPoint;CurrentTime,DelaySecond:real;
begin
GetCursorPos(MousePoint); //得到当前鼠标位置
with msg do
begin
if msg =wm_MyCall then//如是自定义消息
Begin
DelaySecond:=GetDoubleClickTime();//得到鼠标双击的最大时间间隔
case LParam of
WM_LBUTTONDBLCLK: //双击
begin
tag:=1; //发生双击时,此处代码重入并使单击条件为假
messagebox(handle,'这是双击处理','加入代码吧',mb_ok);
SendMessage(Handle, WM_CLOSE, 0, 0)
end;
wm_LButtonDown: //单击
begin
currenttime:=gettickcount; //得到当前开机时间
while ((gettickCount)<(currentTime+delaySecond)) and (tag=0) do
application.ProcessMessages;
//如果在发出双击的有效时间内未双击,则执行单击,可将tag看作为是否双击的bool变量,此循环是时钟控件的代替物,也可在单击时激活一个时钟,若时钟尚未触发时又发生双击事件,则关闭时钟,执行双击代码,否则在时钟触发时执行单击代码,这样可很好地避免一个双击动作对一单一双两条消息的处理
if tag=0 then TrackPopupMenu(GetSystemMenu(handle,false),tpm_BottomAlign or tpm_RightAlign,MousePoint.x,MousePoint.Y,0,handle,nil);//弹出系统菜单,在单击事件时的鼠标位置
tag:=0;
end;
wm_RButtonDown: SysTrayIco(nim_Modify,application.Icon);//修改系统栏图标
End;
end
else
if WParam =SC_MINIMIZE then messagebox(handle,'将要最小化','截获系统消息',MB_OK);//可用此方法截获发给窗口的全部消息,以自定义处理过程
end;
inherited wndProc(Msg); //继续未被处理的消息
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
SysTrayIco(nim_Add,icon);//建立系统栏图标
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
SysTrayIco(nim_Delete);//退出时系统栏删除图标
end;

end.
以上是一段较多运用Api的代码,API调用有:
Shell_NotifyIcon(对系统任务栏图标的操作); GetCursorPos(得到当前鼠标位置); GetDoubleClickTime(得到鼠标双击的最大时间间隔); messagebox(消息对话框);gettickcount(得到当前开机时间); TrackPopupMenu(弹出菜单); GetSystemMenu(得到系统菜单句柄)和通过SendMessage关闭窗口


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值