您的位置:首页 > 其它

Windows 60个常用API

2012-10-18 09:36 316 查看
附录A 常用的Windows API 调用

本附录列出了PowerBuilder 常用Windows API 系统调用同时给出了这些函数的功能

说明方式以及应用示例下表首先给出常用API 调用的名称和扼要功能读者需要详细了

解某个函数的声明格式和示例时可通过序号在本附录中找到相应说明

表A 常用的Windows API 系统

序号 函数 功能

1 Arc() 在窗口上画一条弧线

2 Beep() 让计算机按指定的频率和周期发声

3 BringWindowToTop() 将窗口放置在最前面

4 Chord() 绘制弦图

5 CloseHandle() 释放打开对象的句柄

6 CloseWindow() 最小化窗口

7 DeleteFileA() 删除文件

8 DeleteMenu() 删除指定菜单的菜单项

9 DestroyWindow() 关闭窗口

10 DllRegisterServer() 触发OCX 控件完成自注册过程

11 Ellipse() 绘制椭圆

12 ExitWindowsEx() 通知Windows 关闭操作系统

13 FatalExit() 立即退出应用程序

14 FindWindowA() 根据窗口标题查找窗口

15 FreeLibrary() 从活动内存中卸载一个DLL

16 GetBkColor() 得到指定窗口的背景颜色

17 GetCapture() 得到鼠标所在窗口的句柄

18 GetComputerNameA() 得到执行应用程序的计算机的名称

19 GetClassNameA() 得到指定窗口或对象的类名

20 GetCurrentDirectoryA() 得到当前工作目录

21 GetCurrentThread() 得到当前线程的句柄

22 GetCursor() 得到光标的句柄

23 GetCursorPos() 得到光标的位置

24 GetDC() 得到指定窗口的设备上下文

25 GetKeyboardState() 得到键盘的状态

26 GetKeyState() 得到键盘上指定按键的状态

27 GetModuleHandleA() 得到活动内存中指定模块或动态链接库的句柄

28 GetPixel() 得到窗口上指定像素的颜色

29 GetSystemMenu() 得到系统菜单或窗口菜单的句柄

30 GetSystemTime() 得到系统时间并存放到一个结构中

31 GetThreadPriority() 得到指定线程的优先级

32 GetSystemMetrics() 得到屏幕的解析度以像素为单位

33 GetUserNameA() 得到当前用户的登录名称

34 GetVolumnInformationA() 得到硬盘的参数信息

35 GetWindowsDirectoryA() 得到缺省的Windows 目录

36 GlobalMemoryStatus() 得到内存的详细信息



附录A 常用的774 Windows API 调用

附录

A

常用的

Windows API调用

774

序号 函数 功能

37 LineTo() 从当前位置到指定位置绘制一条直线

38 LoadLibraryA() 将32 位DLL 加载到活动内存中

39 mciSendStringA() 控制和播放.AVI 文件

40 MessageBoxA() 显示一个消息对话框

41 Mouse_Event() 控制和操作鼠标

42 MoveToEx() 将光标移动到指定位置同时保存移动前的光标位置

43 MoveWindow() 移动放大或缩小窗口

44 Pie() 绘制饼图

45 Polygon() 绘制多边形

46 PostMessageA() 向创建指定窗口的线索发送一条消息但不等待线索处理该消息

47 Rectangle() 绘制矩形

48 ReleaseCapture() 解锁鼠标

49 SendMessageA() 向创建指定窗口的线索发送一条消息但等待线索处理该消息

50 SetCapture() 锁定鼠标

51 SetComputerNameA() 修改计算机的名称

52 SetCurrentDirectoryA() 设置当前目录

53 SetCursorPos() 设置光标的位置

54 SetFocus() 将输入焦点设置到指定对象或窗口上

55 SetKeyboardState() 设置键盘的状态

56 SetPixel() 设置窗口上指定像素的颜色

57 SetThreadPriority() 设置线程的优先级

58 Sleep() 睡眠指定的时间

59 SndPlaySoundA()

WaveOutGetNumDevs()

播放.WAV 文件

60 SwapMouseButton() 交换鼠标的左右按钮

61 WinExec() 运行指定的可执行文件

使用这些函数时首先将它们声明为外部全局函数然后在脚本中调用在下面的所

有声明格式中声明都应该在一行内书写由于排版原因本书可能分写在多行上需

要注意的是所有引用类参数由关键字Ref 指明在传递之前都必须分配足够的内存

否则可能引发GPF 错误参看函数GetComputerNameA()的说明下面分别介绍这些函



1 Arc()

功能在窗口上画一条弧线

声明格式FUNCTION boolean Arc(ulong hwnd,long r1,long r2,long r3,long r4,long a1,

long a2,long a3,long a4) LIBRARY "Gdi32.dll"

示例 Boolean rtn

ulong l_handle, l_device

long lv[8]

l_handle = handle(w_main) // w_main 是一个示例窗口

l_device = GetDC(l_handle) //得到窗口的设备上下文

lv[ ] = {10,40,300,220,0,0,180,0} //数组赋值

rtn = Arc(l_device, lv[1], lv[2], lv[3], lv[4], lv[5], lv[6], lv[7], lv[8])

2 Beep()

功能让计算机按指定的频率和周期发声



附录A 常用的Windows API 调用 775

附录

A

常用的

Windows API调用

775

声明格式FUNCTION boolean Beep(long freq,long dur) LIBRARY "Kernel32.dll"

示例 Boolean rtn

Long ll_freq, ll_dur

ll_freq = 500

ll_dur = 20

rtn = Beep(ll_freq, ll_dur)

3 BringWindowToTop()

功能将窗口放置在最前面

声明格式FUNCTION boolean BringWindowToTop(ulong w_handle) LIBRARY

"User32.dll"

示例 Boolean rtn

ulong l_handle

l_handle = handle(w_win2) //得到窗口的句柄

rtn = BringWindowToTop(l_handle)

4 Chord()

功能绘制弦图弦图由椭圆的一部分和一个线段组成

声明格式FUNCTION boolean Chord(ulong hwnd,long x1,long y1,long x2,long y2,long r1,

long r2, long r3, long r4) LIBRARY "Gdi32.dll"

示例 boolean rtn

ulong l_handle, l_device

long lv[8]

l_handle = handle(w_main)

l_device = GetDC(l_handle)

l_device = GetDC(handle(w_main))

lv[ ] = {5,5,200,200,0,0,200,300}

rtn = Chord(l_device, lv[1], lv[2], lv[3], lv[4], lv[5], lv[6], lv[7], lv[8])

5 CloseHandle()

功能释放打开对象的句柄

声明格式FUNCTION boolean CloseHandle(ulong w_handle) LIBRARY "Kernel32.dll"

示例 boolean rtn

ulong l_handle

rtn = CloseHandle(l_handle) //l_handle 为窗口或对象的句柄

6 CloseWindow()

功能最小化窗口

声明格式FUNCTION boolean CloseWindow(ulong w_handle) LIBRARY "User32.dll"

示例 boolean rtn

ulong l_handle

string ls_wname

ls_wname = "<窗口标题>" //请替换为要最小化的窗口的准确标题

l_handle = FindWindowA(0, ls_wname) //由窗口标题找到窗口句柄

rtn = CloseWindow(l_handle)

7 DeleteFileA()



附录A 常用的776 Windows API 调用

附录

A

常用的

Windows API调用

776

功能删除文件

声明格式FUNCTION boolean DeleteFileA(ref string filename) LIBRARY "Kernel32.dll"

示例 下例中用户在单行编辑框中输入一个文件名后代码将该文件删除

string l_file

boolean rtn

l_file = string(sle_deletefile.text)

rtn = DeleteFileA(l_file)

MessageBox("删除文件", string(rtn))

8 DeleteMenu()

功能删除指定菜单的菜单项如果该菜单项下还有子菜单则函数删除指向子菜单

的句柄并释放子菜单所占用的内存

声明格式FUNCTION boolean DeleteMenu(ulong mhand, uint upos, uint flag) LIBRARY

"user32.dll"

示例 ulong m_handle

boolean rtn

m_handle = GetSystemMenu(handle(w_main), false) // 首先得到系统菜单的句柄

rtn = DeleteMenu(m_handle, 1, 0) // 第二个参数这里为1 指定要删除菜

//单项在菜单中的位置

Messagebox("菜单句柄", string(m_handle))

Messagebox("菜单删除结果", string(rtn))

9 DestroyWindow()

功能关闭窗口该函数向指定窗口发送一条删除窗口的消息

声明格式FUNCTION boolean DestroyWindow(ulong w_handle) LIBRARY

"USER32.DLL"

示例 boolean rtn

ulong l_handle

open(w_win2) // 打开一个测试窗口

l_handle = handle(w_win2) //得到窗口的句柄

rtn = DestroyWindow(l_handle) //关闭窗口

10 DllRegisterServer()

功能触发OCX 控件完成自注册过程实际使用时将声明格式中ocxname 替换为

真实的OCX 的名称

声明格式FUNCTION long DllRegisterServer() LIBRARY "c:\windows\ocxname.ocx"

示例 Long ll_rtn

ll_rtn = DllRegisterServer() //通常返回0 表示OCX 已经注册

11 Ellipse()

功能绘制椭圆

声明格式FUNCTION boolean Ellipse(ulong hwnd,long x1,long y1,long x2,long y2)

LIBRARY "Gdi32.dll"

示例 Boolean rtn

ulong l_handle, l_device

long lv[4]



附录A 常用的Windows API 调用 777

附录

A

常用的

Windows API调用

777

l_handle = handle(w_main)

l_device = GetDC(l_handle)

lv[ ] = {5,5,300,300}

rtn = Ellipse(l_device, lv[1], lv[2], lv[3], lv[4])

12 ExitWindowsEx()

功能通知Windows 关闭操作系统

声明格式FUNCTION boolean ExitWindowsEx(uint dwReserved, uint uReserved)

LIBRARY "User32.dll"

示例 boolean rtn

rtn = ExitWindowsEx(0,0) // 两个参数0 告诉Windows 立即关闭系统

13 FatalExit()

功能立即退出应用程序不做任何清理工作各种对象依然保留在内存中调用该

函数时通常会导致GPF 错误一般在调试应用程序时使用

声明格式SUBROUTINE FatalExit(int exitcode) LIBRARY "Kernel32.dll"

示例 int rtn

rtn = MessageBox("该API 调用将产生GPF 错误!","一定要做吗", Exclamation!, YesNo!,2)

If rtn = 1 Then

MessageBox("再次警告!","执行该API 调用后必须重新启动系统")

FatalExit(1)

End If

14 FindWindowA()

功能根据窗口标题查找窗口

声明格式FUNCTION ulong FindWindowA(ulong classname,string windowname)

LIBRARY "User32.dll"

示例 ulong l_handle

string ls_wname

ls_wname = "<窗口标题>" //比如无标题 - 记事本

l_handle = FindWindowA(0, ls_wname)

15 FreeLibrary()

功能从活动内存中卸载一个DLL 警告卸载正在使用的DLL 时将引发GPF

声明格式SUBROUTINE FreeLibrary(ulong libhandle) LIBRARY "Kernel32.dll"

示例 ulong modhandle

modhandle = LoadLibrary("<32 位dll 文件名>") //通常在另一个事件过程中加载库

FreeLibrary(modhandle)

16 GetBkColor()

功能得到指定窗口的背景颜色

声明格式FUNCTION ulong GetBkColor (ulong hwnd) LIBRARY "Gdi32.dll"

示例 ulong l_handle, l_device, l_color

l_handle = handle(w_main)

l_device = GetDC(l_handle) //得到设备上下文

l_color = GetBkColor(l_device) //得到背景颜色

17 GetCapture()



附录A 常用的778 Windows API 调用

附录

A

常用的

Windows API调用

778

功能得到鼠标所在窗口的句柄

声明格式FUNCTION ulong GetCapture( ) LIBRARY "User32.dll"

示例 ulong l_handle

l_handle = GetCapture( )

18 GetComputerNameA()

功能得到执行应用程序的计算机的名称需要注意的是名称参数cname 必须分配

足够的内存否则在退出应用程序时可能会引发GPF 错误实际上所有外部函数的引用

类参数都必须分配足够的内存

声明格式FUNCTION boolean GetComputerNameA(ref string cname,ref long nbuf)

LIBRARY "Kernel32.dll"

示例 string ls_compname

long ll_buf

ll_buf = 25 //设置cname 参数的长度

ls_compname = space(ll_buf) //为ls_compname 参数分配空间

GetComputerNameA(ls_compname, ll_buf)

MessageBox("计算机名称", ls_compname)

19 GetClassNameA()

功能得到指定窗口或对象的类名

声明格式Function long GetClassNameA(ulong hwnd, ref string cname, int buf) Library

"User32.dll"

示例 string l_class

long rtn

ulong l_handle

l_handle = handle(w_main)

l_class = space(50)

rtn = GetClassNameA(l_handle,l_class,50)

Messagebox("类名", l_class)

20 GetCurrentDirectoryA()

功能得到当前工作目录

声明格式FUNCTION ulong GetCurrentDirectoryA(ulong BufferLen, ref string currentdir)

LIBRARY "Kernel32.dll"

示例 string ls_curdir

ulong l_buf

l_buf = 100

ls_curdir = space(l_buf) //为参数分配空间

GetCurrentDirectoryA(l_buf, ls_curdir)

MessageBox("当前目录", ls_curdir)

21 GetCurrentThread()

功能得到当前线程的句柄

声明格式FUNCTION ulong GetCurrentThread() LIBRARY "Kernel32.dll"

示例 ulong rtn

rtn = GetCurrentThread()



附录A 常用的Windows API 调用 779

附录

A

常用的

Windows API调用

779

MessageBox("当前线程句柄", string(rtn))

22 GetCursor()

功能得到光标的句柄

声明格式FUNCTION ulong GetCursor( ) LIBRARY "User32.dll8

示例 ulong l_cursor

l_cursor = GetCursor( )

23 GetCursorPos()

功能得到光标的位置参数mousepos 是一个结构它包括两个分量long xpos, long

ypos

声明格式FUNCTION boolean GetCursorPos(ref mousepos) LIBRARY "User32.dll"

示例 mousepos mouseloc

GetCursorPos(mouseloc)

Messagebox("光标位置", "X="+string(mouseloc.xpos)+"Y="+string(mouseloc.ypos))

24 GetDC()

功能得到指定窗口的设备上下文如果想执行图形外部函数调用必须得到设备上

下文

声明格式Function ulong GetDC(ulong hwnd) library "user32.dll"

示例 ulong l_handle, l_device

l_handle = handle(w_main)

l_device = GetDC(l_handle)

MessageBox("设备上下文", string(l_device))

25 GetKeyboardState()

功能得到键盘的状态该函数按照字符的ASCII 码表示将键盘上每个键的状态保

存到包含256 个整数元素的数组中值0 表示相应键没有被按下

声明格式FUNCTION boolean GetKeyboardState(ref integer kbarray[256]) LIBRARY

"USER32.DLL"

示例 boolean rtn

integer ipkey[256]

rtn = GetKeyboardState(ipkey)

26 GetKeyState()

功能得到键盘上指定按键的状态值0 表示相应键没有被按下

声明格式Function int GetKeyState(integer VirtualKeycode) Library "User32.dll"

示例 int rtn

rtn = GetKeyState(65) // 65 = A

if rtn = 0 then

MessageBox("按键状态","字母A 没有按下")

else

MessageBox("按键状态","字母A 被按下")

end if

27 GetModuleHandleA()

功能得到活动内存中指定模块或动态链接库的句柄可以使用FreeLibrary()函数释



附录A 常用的780 Windows API 调用

附录

A

常用的

Windows API调用

780

放该函数得到其句柄的模块或动态链接库

声明格式Function long GetModuleHandleA(string modname) Library "Kernel32.dll"

示例 ulong rtn

rtn = GetModuleHandleA("User32.dll")

MessageBox("返回代码", string(rtn))

28 GetPixel()

功能得到窗口上指定像素的颜色

声明格式FUNCTION ulong GetPixel(ulong hwnd, long xpos, long ypos) LIBRARY

"Gdi32.dll"

示例 请参看SetPixel()函数

29 GetSystemMenu()

功能得到系统菜单或窗口菜单的句柄

声明格式FUNCTION boolean GetSystemMenu(ulong mhandle, boolean flag) LIBRARY

"user32.dll"

示例 boolean flag

ulong l_handle, m_handle

l_handle = handle(w_main)

flag = false

m_handle = GetSystemMenu(l_handle, flag)

Messagebox("返回值", string(m_handle))

30 GetSystemTime()

功能得到系统时间并存放到一个结构中SystemTime 结构的分量为uint year

uint month uint dayofweek uint day uint hour uint minute uint second uint millisecond

声明格式SUBROUTINE GetSystemTime(ref systemtime systimeptr) Library

"Kernel32.dll"

示例 systemtime s_systime

string l_day, l_date, l_time

GetSystemTime(s_systime)

l_date = string(s_systime.year) +"/"+ string(s_systime.day) &

+"/"+string(s_systime.month) //日期年/月/日

l_time = string(s_systime.hour) +":"+ string(s_systime.minute) &

+":"+ string(s_systime.second) +":"+ string(s_systime.millisecond) //时分秒毫秒

CHOOSE CASE s_systime.dayofweek

CASE 1

l_day = "星期日"

CASE 2

l_day = "星期一"

CASE 3

l_day = "星期二"

CASE 4

l_day = "星期三"

CASE 5

l_day = "星期四"



附录A 常用的Windows API 调用 781

附录

A

常用的

Windows API调用

781

CASE 6

l_day = "星期五"

CASE 7

l_day = "星期六"

END CHOOSE

Messagebox("系统时间",l_date + " " + l_day + " " + l_time)

31 GetThreadPriority()

功能得到指定线程的优先级线程的缺省优先级为0 大于0 的值优先级更高因

而线程将得到更多的CPU 时间

声明格式FUNCTION int GetThreadPriority(ulong hthread) LIBRARY "Kernel32.dll"

示例 ulong l_handle

integer rtn

l_handle = GetCurrentThread()

rtn = GetThreadPriority(l_handle)

MessageBox("当前线程的优先级", string(rtn))

32 GetSystemMetrics()

功能得到屏幕的解析度以像素为单位请注意该函数区分大小写函数名称要

严格按照书上的式样书写

声明格式FUNCTION int GetSystemMetrics(int indexnum) LIBRARY "user32.dll"

示例 int l_xx, l_yy

l_xx = GetSystemMetrics(0) //得到X 方向解析度

l_yy = GetSystemMetrics(1) //得到Y 方向解析度

Messagebox("屏幕解析度", string(l_xx) + " , " + string(l_yy))

33 GetUserNameA()

功能得到当前用户的登录名称

声明格式FUNCTION boolean GetUserNameA(ref string uname, ref ulong slength)

LIBRARY "ADVAPI32.DLL"

示例 string ls_username

string ls_var

ulong lu_val

boolean rtn

lu_val = 255

ls_username = Space( 255 ) //分配足够的空间

rtn = GetUserNameA(ls_username, lu_val)

Messagebox("GetUserNameA", "用户名为" + string(ls_username))

34 GetVolumnInformationA()

功能得到硬盘的参数信息

声明格式FUNCTION boolean GetVolumeInformation(ref string lpRootPathName,ref

string lpVolumeNameBuffer,ulong nVolumeNameSize,ref ulong lpVolumeSerialNumber,ref

ulong lpMaximumComponentLength,ref ulong lpFileSystemFlags,ref string

lpFileSystemNameBuffer, ulong nFileSystemNameSize) Library "kernel32.dll"

示例 boolean rtn



附录A 常用的782 Windows API 调用

附录

A

常用的

Windows API调用

782

string lprootpathname = "c:" //驱动器

string lpVolumeNameBuffer = space(256) //分配足够的空间

ulong nVolumeNameSize = 256

ulong lpVolumeSerialNumber

ulong lpMaximumComponentLength

setnull(lpVolumeSerialNumber)

lpMaximumComponentLength = 256

ulong lpFileSystemFlags

setnull(lpFileSystemFlags)

string lpFileSystemNameBuffer = space(256) //分配足够的空间

ulong nFileSystemNameSize = 256

rtn = GetVolumeInformationA(lpRootPathName, lpVolumeNameBuffer, &

nVolumeNameSize,lpVolumeSerialNumber, lpMaximumComponentLength, &

lpFileSystemFlags, lpFileSystemNameBuffer, nFileSystemNameSize)

sle_1.text = lprootpathname //路径名

sle_2.text = lpVolumeNameBuffer

sle_3.text = string(nVolumeNameSize)

sle_4.text = string(lpVolumeSerialNumber)

sle_5.text = string(lpMaximumComponentLength)

sle_6.text = string(lpFileSystemFlags)

sle_7.text = string(lpFileSystemNameBuffer)

sle_8.text = string(nFileSystemNameSize)

35 GetWindowsDirectoryA()

功能得到缺省的Windows 目录

声明格式FUNCTION ulong GetWindowsDirectoryA(ref string wdir, ulong buf)

LIBRARY "kernel32.dll"

示例 ulong l_buf

string windir

l_buf = 144

windir = space(144)

GetWindowsDirectoryA(windir, l_buf)

MessageBox("当前目录", windir)

36 GlobalMemoryStatus()

功能得到内存的详细信息该函数使用的Memory 结构包括下述分量ulong m_length

ulong m_loaded ulong m_totalphys ulong m_availphys ulong m_totalpagefile ulong

m_availpagefile ulong m_totalvirtual ulong m_availvirtual

声明格式SUBROUTINE GlobalMemoryStatus(ref memory mem2) LIBRARY

"Kernel32.dll"

示例 memory sysmem

GlobalMemoryStatus(sysmem)

Messagebox("内存大小", string(sysmem.m_length))

Messagebox("已加载内存", string(sysmem.m_loaded))

Messagebox("总的物理内存", string(sysmem.m_totalphys))

Messagebox("总的可用内存", string(sysmem.m_availphys))



附录A 常用的Windows API 调用 783

附录

A

常用的

Windows API调用

783

Messagebox("总的页面数", string(sysmem.m_totalpagefile))

Messagebox("可用页面数", string(sysmem.m_availpagefile))

Messagebox("总的虚拟内存", string(sysmem.m_totalvirtual))

Messagebox("可用虚拟内存", string(sysmem.m_availvirtual))

37 LineTo()

功能从当前位置到指定位置绘制一条直线

声明格式FUNCTION boolean LineTo(ulong hwnd,long wx, long wy) LIBRARY

"Gdi32.dll"

示例 请参看MoveToEx()函数

38 LoadLibraryA()

功能将32 位DLL 加载到活动内存中

声明格式FUNCTION ulong LoadLibraryA(string modname) LIBRARY "Kernel32.dll"

示例 ulong modhandle

//当DLL 库在DOS 搜索路径上时可以不指定路径

modhandle = LoadLibraryA("c:\windows\mydll.dll")

If modhandle > 0 Then

MessageBox("返回值", "加载成功句柄 = " + string(modhandle))

else

MessageBox("函数执行结果","不能加载指定模块")

end if

39 mciSendStringA()

功能控制和播放.AVI 文件

声明格式FUNCTION long mciSendStringA(string cmd, REF string rtn, long size, long

wnd) LIBRARY "winmm.dll"

示例 string s_errortext

string filename

filename ="c:\spin.avi"

mciSendStringA ("open "+Filename+" type AVIVideo alias test wait",s_errortext, 0,0)

mciSendStringA ("Window test handle " + string(handle(w_main)) + " wait",s_errortext, 0, 0)

mciSendStringA ("Put test destination wait",s_errortext, 0, 0)

mciSendStringA ("Play test wait", s_errortext, 0, 0)

mciSendStringA ("Close test", s_errortext, 0, 0)

40 MessageBoxA()

功能显示一个消息对话框

声明格式FUNCTION long MessageBoxA(ulong hwnd, ref string text, ref string title,

ulong style) LIBRARY "User32.dll"

示例 long rtn

ulong handle1, style1

string text1

string title1

handle1 = handle(parent)

text1 = "这是一个API Messagebox"

title1 = "API MessageBox"



附录A 常用的784 Windows API 调用

附录

A

常用的

Windows API调用

784

style1 = 0

rtn = MessageBoxA(handle1,text1,title1,style1)

41 Mouse_Event()

功能控制和操作鼠标该函数能够移动鼠标指针按下鼠标按钮并且能够完成用

户使用鼠标能够完成的一切任务下面的示例将鼠标指针左移100 个像素上移70 个像素

声明格式SUBROUTINE Mouse_Event(ulong dwflag,ulong dx,ulong dy,ulong

cbutton,ulong dwextra) LIBRARY "User32.dll"

示例 int lflag

lflag = 1 //1 = 移动鼠标指针, 7 = 按下左按钮, 25 = 按下右按钮

mouse_event(lflag,-80,-50,0,0)

42 MoveToEx()

功能将光标移动到指定位置同时保存移动前的光标位置函数使用的prepos 结构

包含下述分量long xpos, long ypos

声明格式FUNCTION boolean MoveToEx(ulong hwnd,long wx, long wy,ref prepos

prepos2) LIBRARY "Gdi32.dll"

示例 ulong l_handle, l_device

prepos previouspos //用于保存光标移动前的位置

l_handle = handle(w_main)

l_device = GetDC(l_handle)

MoveToEx(l_device,200,200,previouspos) //将光标移动到200,200 处

LineTo(l_device,300,300) //在当前位置和300,300 之间画一条直线

43 MoveWindow()

功能移动放大或缩小窗口

声明格式FUNCTION boolean MoveWindow(ulong whand,int wx,int wy,int ww,int

wh,boolean wflag) LIBRARY "user32.dll"

示例 boolean rtn

ulong l_handle, l_device

l_handle = handle(w_main)

//10-X 位置20-Y 位置100-宽度200-高度

rtn = MoveWindow(l_handle,10,20,100,200,true)

MessageBox("返回值",string(rtn))

44 Pie()

功能绘制饼图

声明格式FUNCTION boolean Pie(ulong hwnd,long x1,long y1,long x2,long y2,long

x3,long y3,long x4,long y4) LIBRARY "Gdi32.dll"

示例 Boolean rtn

ulong l_handle,l_device

long lv[8]

lv[ ] = {10,50,290,220,0,0,80,0}

l_handle = handle(w_main)

l_device = GetDC(l_handle)

rtn = Pie(l_device,lv[1],lv[2],lv[3],lv[4],lv[5],lv[6],lv[7],lv[8])



附录A 常用的Windows API 调用 785

附录

A

常用的

Windows API调用

785

45 Polygon()

功能绘制多边形该函数使用的结构poly 包含下述分量long xpos[5], long ypos[5]

数组的大小与多边形边的条数相关示例中边的条数为5

声明格式FUNCTION boolean Polygon(hdc, ref struct poly poly2, int cnt) LIBRARY

"Gdi32.dll"

示例 ulong l_handle, l_device

int pcnt

l_handle = handle(w_main)

l_device = GetDC(l_handle)

pcnt = 5

poly poly3

poly3.xpos[ ] = {50,100,150,200,250}

poly3.ypos[ ] = {50,100,150,200,250}

Polygon(l_device,poly3,pcnt)

46 PostMessageA()

功能向创建指定窗口的线索发送一条消息但不等待线索处理该消息

声明格式FUNCTION boolean PostMessageA(ulong hwndle,UINT wmsg,ulong

wParam,ulong lParam) Library "User32.dll"

示例 ulong l_handle

boolean rtn

l_handle = handle(w_main)

// 61472 = 最小化窗口61488 = 最大化窗口61728 = 正常窗口

rtn = PostMessageA(l_handle,274,61472,0)

47 Rectangle()

功能绘制矩形

声明格式FUNCTION boolean Rectangle(ulong hwnd,long x1,long y1,long x2,long y2)

LIBRARY "Gdi32.dll"

示例 Boolean rtn

ulong l_handle,l_device

long lv[4]

lv[ ] = { 10,10,275,215} //矩形的四个顶点

l_handle = handle(w_main)

l_device = GetDC(l_handle)

rtn = Rectangle(l_device,lv[1],lv[2],lv[3],lv[4])

48 ReleaseCapture()

功能解锁鼠标

声明格式FUNCTION boolean ReleaseCapture( ) LIBRARY "User32.dll"

示例 请参看SetCapture()函数

49 SendMessageA()

功能向创建指定窗口的线索发送一条消息但等待线索处理该消息

声明格式FUNCTION long SendMessageA(ulong hwndle,UINT wmsg,ulong

wParam,ulong lParam) Library "User32.dll"



附录A 常用的786 Windows API 调用

附录

A

常用的

Windows API调用

786

示例 ulong l_handle

long rtn

l_handle = handle(w_main)

rtn = SendMessageA(l_handle,274,61728,0)

50 SetCapture()

功能锁定鼠标鼠标锁定后使用ReleaseCapture()解锁鼠标

声明格式FUNCTION ulong SetCapture(ulong a) LIBRARY "User32.dll"

示例 boolean rtn

ulong l_loop, u_test, u_long

u_test = handle(parent)

u_long = SetCapture(u_test) //锁定鼠标

SetPointer(SizeNWSE!)

for l_loop = 1 to 150000 //循环等待

next

rtn = ReleaseCapture( ) //解锁鼠标

51 SetComputerNameA()

功能修改计算机的名称

声明格式FUNCTION boolean SetComputerNameA(ref string cname) LIBRARY

"kernel32.dll"

示例 boolean rtn

string l_name

l_name = "ZCF 计算机"

rtn = SetComputerNameA(l_name)

if rtn then

MessageBox("计算机的名称已经修改为ZCF 计算机", &

"重新启动计算机后该名称发挥作用")

else

MessageBox("设置计算机名称", "咳失败了")

end if

52 SetCurrentDirectoryA()

功能设置当前目录

声明格式FUNCTION boolean SetCurrentDirectoryA(ref string cdir) LIBRARY

"kernel32.dll"

示例 boolean rtn

string l_dir

l_dir = "C:\My Documents"

rtn = SetCurrentDirectoryA(l_dir)

MessageBox("SetCurrentDirectory", string(rtn))

53 SetCursorPos()

功能设置光标的位置

声明格式FUNCTION boolean SetCursorPos(int cx, int cy) LIBRARY "User32.dll"

示例 SetCursorPos(300,350)

Messagebox("光标位置","X =" +string(mouseloc.xpos) + "Y =" &



附录A 常用的Windows API 调用 787

附录

A

常用的

Windows API调用

787

+ string(mouseloc.ypos))

54 SetFocus()

功能将输入焦点设置到指定对象或窗口上.

声明格式SUBROUTINE SetFocus(long objhandle) LIBRARY "User32.dll"

示例 SetFocus(handle(sle_1)) // handle 函数得到单行编辑框控件sle_1 的句柄

55 SetKeyboardState()

功能设置键盘的状态值0 表示相应键没有被按下

声明格式FUNCTION boolean SetKeyboardState(ref integer kbarray[256]) LIBRARY

"USER32.DLL"

示例 boolean rtn

rtn = SetKeyboardState(ipkey)

if rtn = false then

Messagebox("操作失败","加载键盘状态时出错")

else

Messagebox("操作成功","成功完成键盘状态加载工作")

end if

56 SetPixel()

功能设置窗口上指定像素的颜色

声明格式FUNCTION ulong SetPixel(ulong hwnd, long xpos, long ypos, ulong pcol)

LIBRARY "Gdi32.dll"

示例 long lx, ly

ulong rtn

ulong l_handle, l_device

lx = 200

ly = 200

l_handle = handle(w_main)

l_device = GetDC(l_handle) //得到设备上下文

rtn = GetPixel(l_device, lx, ly) //得到指定位置的像素颜色

MessageBox("颜色","位置x="+string(lx)+",y="+ string(ly)+"的颜色值="+ string(rtn))

SetPixel(l_device, lx, ly, 0) //将指定位置像素的颜色设置为黑色0

57 SetThreadPriority()

功能设置线程的优先级线程的缺省优先级为0 大于0 的优先级拥有更高的优先

权优先级越大得到的CPU 时间越多但是不应该把优先级设置得过大否则将导致

鼠标停止工作

声明格式FUNCTION boolean SetThreadPriority(ulong hthread, int npriority) LIBRARY

"Kernel32.dll"

示例 ulong l_handle

boolean rtn

l_handle = GetCurrentThread() //得到当前的优先级

rtn = SetThreadPriority(l_handle, 2) // 将线程的优先级设置为2

MessageBox("将线程的当前优先级修改为", string(rtn))

58 Sleep()



附录A 常用的788 Windows API 调用

附录

A

常用的

Windows API调用

788

功能睡眠指定的时间该函数执行过程中系统将不重绘屏幕该函数的作用相当

于在PowerBuilder 执行了一个For ... Next 循环

声明格式SUBROUTINE Sleep(ulong milli) LIBRARY "Kernel32.dll"

示例 ulong l_delay

l_delay = 5000 //5000 毫秒5 秒

Sleep(l_delay) //睡眠5 秒钟

59 SndPlaySoundA()和WaveOutGetNumDevs()

功能这两个函数结合起来使用用于播放.WAV 格式的文件

声明格式FUNCTION boolean SndPlaySoundA(string wavfile, uint flag) LIBRARY

"WINMM.DLL"

FUNCTION uint WaveOutGetNumDevs() LIBRARY "WINMM.DLL"

示例 uint lui_NumDevs, l_mode

string ls_file

l_mode = 0

ls_file = string(c:\windows\media\chimes.wav)

lui_NumDevs = WaveOutGetNumDevs()

IF lui_NumDevs > 0 THEN

SndPlaySoundA(ls_file, l_mode)

END IF

60 SwapMouseButton()

功能交换鼠标的左右按钮让鼠标按钮返回正常状态时需要再次调用该函数

声明格式FUNCTION boolean SwapMouseButton(boolean var) LIBRARY "User32.dll"

示例 boolean rtn, l_mouse

rtn = SwapMouseButton(l_mouse)

If l_mouse = true Then

MessageBox("交换鼠标按钮","左边的按钮发挥右按钮的作用")

Else

MessageBox("交换鼠标按钮","右边的按钮发挥左按钮的作用")

End If

61 WinExec()

功能运行指定的可执行文件

声明格式FUNCTION uint WinExec(ref string filename, uint wstyle) LIBRARY

"kernel32.dll"

示例 string ls_filename

uint rtn, wstyle

ls_filename = "c:\windows\calc.exe" //程序名称

wstyle = 1

rtn = WinExec(ls_filename, wstyle) //运行指定程序

Messagebox("返回值", string(rtn))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: