zoukankan      html  css  js  c++  java
  • 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下通过。
    //

  • 相关阅读:
    C#文件下载(实现断点续传)
    C#winform实现跑马灯
    Asp.net GridView转换成DataTable
    SQL Server 索引重建脚本
    SQL SERVER数据库维护与重建索引
    python try except 出现异常时,except 中如何返回异常的信息字符串
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte
    bower 安装依赖提示 EINVRES Request to https://bower.herokuapp.com/packages/xxx failed with 502
    EINVRES Request to https://bower.herokuapp.com/packages/ failed with 502
    使用notepad++插件远程编辑linux下的配置文件
  • 原文地址:https://www.cnblogs.com/youyoutianxia/p/3185619.html
Copyright © 2011-2022 走看看