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 ,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下通過。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章