现在的位置: 首页 > 编程语言 > 正文

PB中消息对话框的居中显示

2020年02月19日 编程语言 ⁄ 共 2775字 ⁄ 字号 评论关闭
文章目录

上海大学 孙渊磊

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 ,int X ,int Y ,int nWidth ,int nHeight ,Boolean bRepaint ) 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 lpclassname ,String
lpwindowname , Ulong dwtimeout ) return Boolean
//lpclassname: 消息对话框的类名(#32770)
//lpwindowname: 消息对话框的标题
//dwtimeout: 超时计数
Ulong lul_hwnd //存放消息对话框的句柄
Ulong lul_start //计时开始时刻的值
lul_start = GetTickCount ( ) //计时开始
do
//查找顶层窗口
lul_hwnd=FindWindowA ( lpclassname ,lpwindowname )
//找到顶层窗口后,跳出循环
if lul_hwnd <> 0 then exit
//判断是否已超时
loop while GetTickCount( )-lul_start< dwtimeout
//没有找到消息对话框
if lul_hwnd = 0 then
return false
else
//对话框居中
return of_center ( 0 ,lul_hwnd )
end if
of_center的重载函数代码如下:
ccuo_thread.of_center ( Ulong hwndp ,Ulong hwndc ) return Boolean
//hwndp:父窗口的句柄,值为0时认为是桌面
//hwndc:子窗口的句柄
int li_x //窗口的X坐标
int li_y //窗口的Y坐标
stc_rect lstc_parent //父窗口的4边坐标
stc_rect lstc_child //子窗口的4边坐标
//值为0时认为是桌面
if hwndp = 0 then hwndparent =
GetDesktopWindow ( )
//获得窗口的4边坐标
if not GetWindowRect ( hwndcurrent ,lstc_child ) then return false
if not GetWindowRect ( hwndparent ,lstc_parent ) then return 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坐标
if li_x < 0 or li_y < 0 then return false
//移动子窗口
if not MoveWindow ( hwndcurrent ,li_x ,li_y ,lstc_child.right -lstc_child.left ,lstc_child.bottom - lstc_child.top ,false ) then return false
return true
本文代码在PB 7.0下通过。

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( xuebuyuan.com )

以上就上有关PB中消息对话框的居中显示的相关介绍,要了解更多编程,开发,编程论坛,程序员内容请登录学步园。

抱歉!评论已关闭.