PB中实现多线程技术

在PB中实现多线程技术,有两种方法:一种是用VC++将各种操作写入函数并编译成动态链接库供PB调用;另一种是用PB的SharedObject系列函数来实现多线程。本文基于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下通过。    
  //   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值