实用API

在应用程序中启动控制面板
在应用程序中启动控制面板,只需用ShellExecute函数打开对应的CPL文件即可,例如要在应用程序中修改Windows密码,只需打开Password.cpl文件,启动ODBC管理器只要打开ODBCCP32.CPL。
函数声明:
Function Long ShellExecute(Long hwindow, String lpOperation, String lpFile, String lpParameters, String lpDirectory, Long nShowCmd) Library 'shell32.dll' Alias for ShellExecuteA
Function Long GetDesktopWindow() Library 'user32.dll'
脚本如下:
String ls_cpl_name
String ls_null
SetNull(ls_null)
ls_cpl_name = "Password.cpl"
ShellExecute(GetDesktopWindow(), ls_null, 'rundll32.exe', "shell32.dll,Control_RunDLL " + ls_cpl_name + ",", ls_null, 0)

-------------------------------------------------------
数据类型转换表
MICROSOFT PB(16Bit) PB(32Bit)
Bool Boolean Boolean
Byte,Char Char Char
Char* Ref string Ref String
Colorref Uint Ulong
Double Double Double
Dword Uint Ulong
Float N/A N/A
Handle Uint Ulong
Hdc Uint Ulong
Hfile Uint Ulong
Hinstance Uint Ulong
Hwnd Uint Ulong
Int Int Int
Long Long Long
Lparam Uint Ulong
Lpbyte Ref Int Ref Long
Lpcwstr Ref Blob Ref Blob (Unicode use ToUnicode())
Lpcvoid Ref String Ref String
Lpdword Ref Uint Ref Ulong
Lpfiletime Ref Time Ref Time
Lpint Ref Int Ref Long
Lpstr,Lpcstr Ref String Ref String
Lpvoid Ref Structstruct_inst Ref Struct struct_inst
Lpword Ref Int Ref Ulong
Mcierror Long Long
Pbyte Ref Int[#] Ref Long[#]
Short Int Int
Structure Ref Struct struct_inst Ref Struct Struct_inst
Uint Uint Uint
Void** SUBROUTINE SUBROUTINE
Word Int Long
Wparam Uint Ulong

----------------------------------------------------
将指定的窗口带至窗口列表顶部
在应用程序中有时我们需要用Bringwindowtotop将指定的窗口带至窗口列表顶部。倘若它部分或全部隐藏于其他窗口下面,则将隐藏的部分完全显示出来。但是在某些情况下这个函数并不能达到我们想要的效果,倘若某窗口并非前台应用程序的一部分,那么一旦随同该窗口调用本函数,仍会将窗口带至它自己那个应用程序的窗口列表顶部。但是,不会同时使那个应用成为前台应用程序。这意味着在调用了本函数后,窗口仍会保持隐藏状态。那么如何使应用成为前台应用呢?
我们可以使用SetForegroundWindow函数。 例如:
FUNCTION long FindWindowA( ulong Winhandle, string wintitle ) Library "user32"
FUNCTION ulong SetForegroundWindow(ulong hwnd) LIBRARY "user32.dll"
long ll_winhandle
ll_winhandle=FindWindowA( 0, "test" )
if ll_winhandle >0 then
SetForegroundWindow(ll_winhandle)
end if

-----------------------------------------------------------
程序中切换中英文输入法
函数声明: function boolean ImmSimulateHotKey (ULong hWnd, ULong dwHotKeyID) library "IMM32.dll"
function ulong GetKeyboardLayout(ulong dwLayout) LIBRARY "user32.dll"
function boolean ImmIsIME(uLong hklKeyboardLayout) library "IMM32.DLL"
脚本如下:
constant int IME_THotKey_IME_NonIME_Toggle=112
ulong hklCurrent ulong hnd
//切换到英文输入法
hklCurrent=GetKeyboardLayout(0)
if ImmIsIME(hklCurrent) then
hnd=Handle(parent)
ImmSimulateHotKey(hnd,IME_THotKey_IME_NonIME_Toggle)
end if
//切换到中文输入法
hklCurrent=GetKeyboardLayout(0)
if not ImmIsIME(hklCurrent) then
hnd=Handle(parent)
ImmSimulateHotKey(hnd,IME_THotKey_IME_NonIME_Toggle)
end if

-----------------------------------------------------------------
用ExitWindowEx关闭系统
为方便用户,有时需要在应用程序中增加退出并关闭计算机的功能,其实只要几行代码即可实现。
首先声明API函数:
Function long ExitWindowsEx (long uFlags , long dwReserved ) Library "user32"
然后在程序中调用:
ExitWindowEx( 1, 0 )
参数型及说明:
uFlags Long,指定下述一个或多个标志(用OR运算符合并到一起)
EWX_FORCE 强迫中止没有响应的进程
EWX_LOGOFF 中止进程,然后注销
EWX_SHUTDOWN 关掉系统电源(如果可能的话,ATX电源就可以)
EWX_REBOOT 重新引导系统
EWX_SHUTDOWN 关闭系统
dwReserved Long,保留,设为零

-------------------------------------------------------------
用WinHelpA函数显示Pop-Up帮助
First, go to the Window painter and select Declare, Global External Functions from the menu. Paste this declaration:
FUNCTION integer WinHelpA(long hWnd, string lpHelpFile, integer wCommand, integer dwData) LIBRARY "User32.dll"
This tells PowerBuilder how to call a function that resides in a DLL.
Next, you create a help file with indexed topics using a tool like Doc-to-help or RoboHelp. Here's the code you would put in some event that corresponds to the user hitting F1, or whatever:
WinHelpA(handle(parent), "application.hlp", 8, 1)
This example comes from a control on a window.
The first parameter gets a handle to the window, which is the control's parent. "application.hlp" is your help file.
The number "8" tells the function to display the yellow context-sensitive help.
The last parameter, a "1" in this example, means topic number 1.

------------------------------------------------------------
显示/隐藏windows任务栏
首先声明如下外部函数:
Function long FindWindowExA
& ( long hWnd, long hWndChild, ref string lpszClassName,
& ref string lpszWindow) library 'user32'
Function long ShowWindow
& (long hWnd, long nCmdShow ) library 'user32'
在Script中加入如下内容:
Constant Long SW_HIDE = 0
Constant Long SW_NORMAL = 1
Constant Long SW_SHOWMINIMIZED = 2
Constant Long SW_SHOWMAXIMIZED = 3
Constant Long SW_SHOWNOACTIVATE = 4
Constant Long SW_SHOW = 5
Constant Long SW_MINIMIZE = 6
Constant Long SW_SHOWMINNOACTIVE = 7
Constant Long SW_SHOWNA = 8
Constant Long SW_RESTORE = 9
Constant Long SW_SHOWDEFAULT = 10
String ls_ShellTaskBarWnd = "Shell_TrayWnd"
String ls_isnull
Long ll_HTaskBar, ll_HDeskTop
ll_HTaskBar = FindWindowExA( 0, 0, ls_ShellTaskBarWnd, ls_Null )
ShowWindow( ll_HTaskBar, SW_HIDE )
MessageBox( 'Pause', 'Can you see TaskBar?' )
ll_HTaskBar = FindWindowExA( 0, 0, ls_ShellTaskBarWnd, ls_Null )
ShowWindow( ll_HTaskBar, SW_SHOW )

-----------------------------------------------
Windows风格的About对话框
利用Api函数ShellAboutA可以显示一个与Windows*作系统风格一致的About对话框。
首先声明如下外部函数:
function int ShellAboutA( ulong al_hWnd, string as_szApp,
& string as_szOtherStuff, ulong hIcon ) library "shell32"
Script如下:
ShellAboutA( handle( parent ), "Window Title#more text", & "Still more text", 0 )
其中:
Window Title 窗口标题
more text Still more text 你自己的说明

--------------------------------------------------------
获得Windows系统目录
在应用程序中有时需要用到Windows系统目录,用GetWindowsDirectory函数即可实现。
首先声明如下外部函数:
Function uint GetWindowsDirectoryA(
& ref string dirtext, uint textlen)
& library "KERNEL32.DLL"
Script如下:
String ls_WinPath
ls_WinPath = Space( 128 )
GetWindowsDirectoryA( ls_WinPath, 128 )

-------------------------------------------------------------
限制应用程序只运行一次
1.声明外部函数如下:
FUNCTION uLong ShowWindow( ulong winhandle, int wincommand ) Library "user32"
FUNCTION uLong BringWindowToTop( ulong HWND ) Library "user32"
FUNCTION long FindWindowA( ulong Winhandle, string wintitle ) Library "user32"
2. 创建一个窗口'w_test' . 将title 设置为"Test Window".
3. 在application的 Open 事件中加入如下代码:
long ll_winhandle
ll_winhandle = FindWindowA( 0, "Test Window" )
If ll_winhandle > 0 Then
BringWindowToTop( ll_winhandle )
ShowWindow( ll_winhandle , 5 )
Return
End If
Open( w_test )

----------------------------------------------------------
将长文件名转换为短文件名
1.声明外部函数如下:
function long GetShortPathNameA( string lpLong, ref string lpShort, long lBuff ) library 'kernel32'
2.定义函数f_Convert( string as_long ), 函数代码如下:
String ls_Buffer
Long ll_RC
ls_Buffer = Space( 255 )
ll_RC = GetShortPathNameA( as_Long, ls_Buffer, 255 )
RETURN ls_Buffer

---------------------------------------------------------------
使用动画鼠标指针
尽管在PB的帮助中没有提到,实际上在PB中时可以使用动画鼠标指针的,只需指定路径和文件名即可,例如:
st_1.Pointer = "c:/windows/cursor/hourglas.ani"
在Response窗口中使用Menu PB中Response窗口不能直接放置Menu,但我们可以通过脚本来实现,在窗口的OPEN事件中加入:
This.changemenu(m_test)

-------------------------------------------------------------
获得应用运行的模式
运行如下脚本:
Handle(GetApplication()),如果返回0,则应用运行在开发模式,否则运行的是编译后的EXE文件

----------------------------------------------------------------------
移动窗口中的控件
在mousedown事件中写入如下脚本即可在窗口中任意移动此控件:
Send(handle(this), 274, 61458, 0)
如果控件没有mousedown事件,定义事件如下:
ue_mousedown pbm_lbuttondown
将以上代码写在窗口的mousedown事件中,在窗口客户区任意地方按下鼠标左键即可拖动窗口

-----------------------------------------------------------------------------
捕获控制菜单中的Close事件
用户按控制菜单中的Close按钮(窗口右上角)关闭窗口时会触发pbm_syscommand事件,定义事件如下:
ue_close pbm_syscommand
在事件中加入如下代码:
IF Message.WordParm = 61536
THEN MessageBox("","窗口将关闭")
END IF

-------------------------------------------------------------------------------
用DWSyntax获得颜色值
有时我们需要在脚本中直接使用颜色值,如何获得呢?打开DWSyntax,单击'RGB'按钮,选中需要的颜色后按“确定”,然后在Script窗口中“粘贴”,OK!

-----------------------------------------------------------------------
在PowerBuilder中动态调用函数
目前流行的大部分应用程序中都提供了Undo功能,在PowerBuilder中也可以利用Undo()函数实现该功能。Undo()函数可用于DataWindow, EditMask, MultiLineEdit, RichTextEdit和SingleLineEdit 对象,如果只对某一个对象进行Undo*作,只需在Undo菜单项的单击事件中键入如下脚本:
Objectname.undo(),
但是当窗口中有多个对象,我们在编写脚本时并不知道要对哪个对象执行undo()*作,如何解决这一问题呢?
在PowerBuilder中,undo()等函数只能用于可视对象,而所有可视对象均继承自系统对象类GraphicObject。因此我们可以定义一个GraphicObject对象的实例变量go_object,等到运行时再用getfocus()函数确定具体*作对象。然后用Typeof()函数确定当前对象的类型,再用Choose case语句根据不同的类型引用不同的实例变量,代码如下:
graphicobject go_object
DataWindow dw_object
EditMask em_object
MultiLineEdit mle_object
RichTextEdit rte_object
SingleLineEdit sle_object
go_object=getfocus()
choose case TypeOf(go_object)
case DataWindow!
dw_object=go_object dw_object.undo()
case EditMask!
em_object=go_object em_object.undo()
case MultiLineEdit!
mle_object=go_object
mle_object.undo()
case RichTextEdit!
rte_object=go_object
rte_object.undo()
case SingleLineEdit!
sle_object=go_object
sle_object.undo()
case else
messagebox("Error","Can not undo")
end choose
其实我们可以用动态调用函数的方法简单地解决这一问题(只需三行代码),即对GraphicObject对象调用undo()函数,然后在函数名前加上关键字Dynamic。因为对象类GraphicObject并没有undo()这个对象函数,如果不加关键字Dynamic,编译时就会出现错误。使用了Dynamic关键字,PowerBuilder在编译时不检查该函数和所用参数的有效性,而到脚本运行时才去检查该函数。代码如下:
GraphicObject go_object
go_object=getfocus()
go_object.dynamic undo()
允许动态调用函数是的PowerBuilder5.0的特性之一。在程序设计中灵活运用动态函数调用的方法有助于提高所设计程序的可维护性、可重用性。 以上程序在PowerBuileer 5.0,Window95环境下运行通过。

-----------------------------------------------------------
如何在SCRIPT中调用数据管道
首先打开Pipe画板,定义源数据库、源表、目标数据库、目标表,然后取名存盘,在本例中Pipe名为Pipe_sample。
打开User Object画板,定义一个用户对象,该对象继承自系统对象Pipeline,并在该对象中定义如下实例变量:
STATICTEXT READ
STATICTEXT WRITTEN
STATICTEXT ERROR
这三个变量用来记录数据管道执行时读入的行数,写入的行数,发生错误的行数。
在该用户对象的Pipemeter事件中写入如下语句:
READ.TEXT=STRING(ROWSREAD)
WRITTEN.TEXT=STRING(ROWSWRITTEN)
ERROR.TEXT=STRING(ROWSINERROR) 然后取名存盘,在本例中取名为U_Pipe_Sample。
打开窗口画板,创建一个窗口,在该窗口中放置一个数据窗口对象和三个静态文本对象,分别名为DW_1,ST_READ,ST_WRITTEN,ST_ERROR,这个数据窗口用来记录管道执行过程中发生的错误信息,不要为该数据窗口对象指定数据窗口。
在该窗口中定义如下实例变量:
Transaction I_SRC,I_DST
U_Pipe_Sample I_PIPE
在窗口的Open事件中写入如下语句:
i_src=create transaction
i_src.dbms='odbc'
i_src.dbparm="Connectstring='DSN=数据库名';uid=用户名;pwd=口令"
connect using i_src;
If i_src.sqlcode <> 0 then
Messagebox("源数据库连接错误",i_src.sqlerrtext)
Return
End If
i_dst=create transaction
i_dst.dbms='odbc'
i_dst.dbparm="Connectstring='DSN=数据库名';uid=用户名;pwd=口令"
connect using i_dst;
If i_dst.sqlcode <> 0 then
Messagebox("目标数据库连接错误",i_dst.sqlerrtext)
Return
End If
在窗口中放置一个按钮用来运行数据管道,在此按钮的Clicked事件中写入如下语句:
i_pipe=create u_pipe_sample
i_pipe.read=st_read
i_pipe.written=st_written
i_pipe.error=st_error
i_pipe.dataobject="Pipe_Sample"
//execute pipe
i_pipe.start(i_src,i_dst,dw_1)

----------------------------------------------------------------------
如何在DataWindow中实现列的自动折行
我们在PowerBuilder应用程序的开发过程中, 使用DataWindow时, 经常会遇 到某列的数据太长, 不能同时全部显示的情况. 若采用自动水平滚动, *作起 来又不够简便.
下面介绍一种方法, 实现列数据多行显示, 即实现列数据的自动折行.具体步骤如下:
1) 在DataWindow Painter中打开此DataWindow.
2) 在需设定自动折行的列上双击鼠标, 弹开此列的属性窗口.
3) 选择Position标签, 选中Autosize Height 多选框.
4) 选择Edit标签, 不选中Auto Horz Scroll多选框.
5) 单击OK按钮, 保存所做的修改.
6) 点中Detail Band (即写有Detail的灰色长带), 单击鼠标右键, 选择 Properties... 菜单项.
7) 选中Autosize Height多选框.
8) 单击OK按钮, 保存所做的修改.
9) 保存此DataWindow.
注意: 连在一起的汉字(中间没有标点或空格分隔), 系统将认为是一个单词, 不会自动进行折行.
限制应用程序只运行一次

关键:ShowWindow、BringWindow和FindWindow API函数

1.声明外部函数如下:

FUNCTION boolean ShowWindow( ulong winhandle,& int wincommand ) Library "user32"
FUNCTION boolean BringWindowToTop( ulong HWND )& Library "user32"
FUNCTION long FindWindowA( ulong Winhandle, & string wintitle ) Library "user32"
2. 创建一个窗口'w_test' . 将title 设置为"Test Window".

3. 在application的 Open 事件中加入如下代码:

long ll_winhandle

ll_winhandle = FindWindowA( 0, "Test Window" )

If ll_winhandle > 0 Then

BringWindowToTop( ll_winhandle )

ShowWindow( ll_winhandle , 5 )

Return

End If

Open( w_test )

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值