---- 、实现功能
---- 按钮可以显示 4种状态图形:
---- 1、Normal状态;
---- 2、Disabled状态;
---- 3、MouseOver状态(鼠标进入按钮区);
---- 4、ClickDown状态(鼠标按下)
---- 2、关键思路方法
---- 1、当鼠标进入按钮区域时Control控件图片改换成MouseOver状态图片并设置状态信号;
---- 2、鼠标滑入按钮区域后用Windows APISetCapture来捕获鼠标输入消息跟踪鼠标位置;
---- 3、当监测到鼠标滑出按钮区域时用ReleaseCapture释放鼠标捕获恢复按钮图片到Normal状态并设置状态信号;
---- 4、改变Control控件图片(PictureName)前先用ReleaseCapture释放鼠标捕获然后改变PictureName属性值接着重新SetCapture改变图片后PowerBuilder重新建立了Control控件窗口窗口句柄(hWnd)也随的改变了
---- 3、设计过程
---- 1、新建“User Object” -〉选择VisualStandard类 -〉选择“Picture”;
---- 2、定义全局或局部外部:
// *******************************
// Declare External Functions
// *******************************
function ulong SetCapture
(ulong hwnd) library "user32.dll"
function boolean ReleaseCapture
(ulong hwnd) library "user32.dll"
function boolean DrawEdge(ulong hdc,
ref rect qrc, u edge, u grfFlags)
library "user32.dll"
---- 3、定义结构数据类型
RECT
{
long left
long top
long right
long bottom
}
---- 4、定义Control控件共享变量:
// *******************************
// Declare Shared Variables
// *******************************
boolean sb_SuppressHoverBorder
---- 5、定义Control控件例子变量:
// *******************************
// Declare Instance Variables
// *******************************
Private:
boolean ib_MouseCaptured
Public:
is_PicNormal
is_PicDisabled
is_PicMouseOver
is_PicClickDown
in_State
---- 6、定义用户事件:
// *******************************
// Declare User Events
// *******************************
Event Name="mousemove", ID="pbm_mousemove"
Event Name="lbuttondown", ID="pbm_lbuttondown"
Event Name="lbuttonup", ID="pbm_lbuttonup"
---- 7、编写事件代码:
// “Constructor” 事件代码
// *** begin constructor event ***
//
is_PicNormal = this.PictureName
is_PicDisabled = "Disabled状态图片.bmp"
is_PicMouseOver = "MouseOver状态图片.bmp"
is_PicClickDown = "ClickDown状态图片.bmp"
in_State = 0
sb_SuppressHoverBorder = FALSE
//
// *** end constructor event ***
// “MouseMove” 事件代码
// *** begin mousemove event ***
//
rect lr_Border
not ib_MouseCaptured then
flags < > 1 then
this.PictureName = is_PicMouseOver
// Left Button Down
this.PictureName = is_PicClickDown
end
in_State = 1
SetCapture(handle(this))
ib_MouseCaptured = TRUE
not sb_SuppressHoverBorder then
lr_Border.left = 0
lr_Border.top = 0
lr_Border.right = UnitsToPixels
(this.Width, XUnitsToPixels!)
lr_Border.bottom = UnitsToPixels
(this.Height, YUnitsToPixels!)
flags < > 1 then
DrawEdge(GetDC(handle(this)),
lr_Border, 4, 1+2+4+8)
// Left Button Down
DrawEdge(GetDC(handle(this)),
lr_Border, 2, 1+2+4+8)
end
end
// 检测鼠标是否滑出按钮区域?
(XPos < 0 or YPos < 0) or (XPos >
this.Width or YPos > this.Height) then
ib_MouseCaptured = FALSE
ReleaseCapture
in_State = 0
this.PictureName = is_PicNormal
end
end
1
//
// *** end mousemove event ***
// “LButtonDown” 事件代码
// *** begin lbuttondown event ***
//
rect lr_Border
ib_MouseCaptured then
ib_MouseCaptured = FALSE
ReleaseCapture
end
in_State = 2
this.PictureName = is_PicClickDown
SetCapture(handle(this))
ib_MouseCaptured = TRUE
not sb_SuppressHoverBorder then
lr_Border.left = 0
lr_Border.top = 0
lr_Border.right = UnitsToPixels
(this.Width, XUnitsToPixels!)
lr_Border.bottom = UnitsToPixels
(this.Height, YUnitsToPixels!)
DrawEdge(GetDC(handle(this)),
lr_Border, 2, 1+2+4+8)
end
1
//
// *** end lbuttondown event ***
// “LButtonUp” 事件代码
// *** begin lbuttonup event ***
//
rect lr_Border
ib_MouseCaptured then
ib_MouseCaptured = FALSE
ReleaseCapture
end
(XPos < 0 or YPos < 0) or (XPos >
this.Width or YPos > this.Height) then
in_State = 0
this.PictureName = is_PicNormal
in_State = 1
this.PictureName = is_PicHover
SetCapture(handle(this))
ib_MouseCaptured = TRUE
not sb_SuppressHoverBorder then
lr_Border.left = 0
lr_Border.top = 0
lr_Border.right = UnitsToPixels
(this.Width, XUnitsToPixels!)
lr_Border.bottom = UnitsToPixels
(this.Height, YUnitsToPixels!)
DrawEdge(GetDC(handle(this)),
lr_Border, 4, 1+2+4+8)
end
end
// 产生Clicked事件
this.event post clicked
1
//
// *** end lbuttonup event ***
// “Other” 事件代码
// *** begin other event ***
//
message.number = 533 and ib_MouseCaptured then
// wm_CaptureChanged
ib_MouseCaptured = FALSE
in_State = 0
this.PictureName = is_PicNormal
1
end
0
//
// *** end other event ***
---- 4、简要介绍说明
---- 1、ib_MouseCaptured变量是作为MouseMove事件刷新Control控件图片信号灯及判断是否已安装了鼠标捕捉器;
---- 2、sb_SuppressHoverBorder变量默认值为FALSE当值为TRUE时Control控件不绘制凸或凹边框;
---- 3、“Other”事件当鼠标捕捉器被释放或被替换时会触发WM_CAPTURECHANGED事件例如:您在Clicked事件中MessageBox时将触发WM_CAPTURECHANGED事件在此事件代码中恢复按钮到Normal状态
最新评论