pb垂直居中:PB中消息对话框的居中显示

上海大学 孙渊磊

SharedObject系列
和共享对象有关包括:SharedObjectRegister、SharedObjectGet、SharedObjectUnregister和SharedObjectDirectory
首先用SharedObjectRegister化共享对象并建立个单独线程如:
SharedObjectRegister (“ccuo_thread” ,“thread1” )
其中ccuo_thread是个共享自定义类用户对象类名thread1是共享对象例子共享名如果SharedObjectRegister返回Success则新线程创建成功
然后执行指定代码有两种思路方法让新线程执行指定代码:种是在自定义类用户对象constructor事件中编写脚本新线程创建后就会自动执行该事件脚本;另种思路方法是使用SharedObjectGet实现共享对象例子引用如:
SharedObjectGet ( “thread1” ,inv_thread )  
其中inv_thread是用来存储共享对象例子个对象变量要求和ccuo_thread具有同个类名
最后通过使用Post语句即以inv_thread.Post of_function(agrs)形式异步共享对象of_function
在完成任务后可以用SharedObjectUnregister中止线程也可用SharedObjectDirectory列出所有有效共享对象
部分
本文所用Win32 API原型为:
Function Ulong FindWindowA ( String lpClassName ,String lpWindowName ) Library “user32.dll”
Function Ulong GetTickCount ( ) Library “kernel32.dll”  
Function Ulong GetDesktopWindow ( ) Library “user32.dll”  
Function Boolean GetWindowRect ( Ulong hWnd ,ref stc_rect lpRect ) Library “user32.dll”
Function Boolean MoveWindow ( Ulong hWnd , X , Y , nWidth , nHeight ,Boolean bRepa ) Library “user32.dll”  
下面具体讨论如何实现消息对话框居中显示:
//声明对象变量
ccuo_thread lccuo_thread  
//创建新线程
SharedObjectRegister (‘ccuo_thread’ ,‘thread_center’ )
//引用例子
SharedObjectGet (‘thread_center’ ,lccuo_thread )  
//窗口居中
lccuo_thread.Post of_center (‘#32770’ ,‘Demostration’ ,2000 )  
//创建消息对话框
MessageBox ( ‘Demostration’ ,‘Copyright(c) 2001 by Y.L.Sun’ )  
//中止线程
SharedObjectunRegister ( ‘thread_center’ )  
实现部分
实现窗口居中显示是自定义类用户对象ccuo_thread对象of_center其实现代码如下:
ccuo_thread.of_center ( String lpname ,String  
lpwindowname , Ulong dwtimeout ) Boolean
//lpname: 消息对话框类名(#32770)
//lpwindowname: 消息对话框标题
//dwtimeout: 超时计数
Ulong lul_hwnd //存放消息对话框句柄
Ulong lul_start //计时开始时刻
lul_start = GetTickCount ( ) //计时开始
do
//查找顶层窗口
lul_hwnd=FindWindowA ( lpname ,lpwindowname )
//找到顶层窗口后跳出循环
lul_hwnd <> 0 then exit
//判断是否已超时
loop while GetTickCount( )-lul_start< dwtimeout  
//没有找到消息对话框
lul_hwnd = 0 then  
false  

//对话框居中
of_center ( 0 ,lul_hwnd )  
end
of_center重载代码如下:
ccuo_thread.of_center ( Ulong hwndp ,Ulong hwndc ) Boolean
//hwndp:父窗口句柄值为0时认为是桌面
//hwndc:子窗口句柄  
li_x //窗口X坐标
li_y //窗口Y坐标
stc_rect lstc_parent //父窗口4边坐标
stc_rect lstc_child //子窗口4边坐标
//值为0时认为是桌面
hwndp = 0 then hwndparent =  
GetDesktopWindow ( )  
//获得窗口4边坐标
not GetWindowRect ( hwndcurrent ,lstc_child ) then false  
not GetWindowRect ( hwndparent ,lstc_parent ) then false
li_x = (( lstc_parent.right - lstc_parent.left ) -  
( lstc_child.right -lstc_child.left )) /2
li_y = (( lstc_parent.bottom - lstc_parent.top ) -  
( lstc_child.bottom -lstc_child.top )) /2
//计算子窗口X、Y坐标
li_x < 0 or li_y < 0 then false  
//移动子窗口
not MoveWindow ( hwndcurrent ,li_x ,li_y ,lstc_child.right -lstc_child.left ,lstc_child.bottom - lstc_child.top ,false ) then false  
true
本文代码在PB 7.0下通过 
Tags:  对话框不显示 模态对话框消息 pb垂直居中

延伸阅读

最新评论

发表评论