<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Xqlab's Blog &#187; API</title>
	<atom:link href="http://www.xqlab.com/html/ytag/api/feed" rel="self" type="application/rss+xml" />
	<link>http://www.xqlab.com</link>
	<description>一个研究电脑和网络等IT技术的实验室</description>
	<lastBuildDate>Sat, 21 Aug 2010 10:37:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>API在VB中的一些应用技巧</title>
		<link>http://www.xqlab.com/html/y2009/332.html</link>
		<comments>http://www.xqlab.com/html/y2009/332.html#comments</comments>
		<pubDate>Sat, 25 Apr 2009 01:50:42 +0000</pubDate>
		<dc:creator>xqlab</dc:creator>
				<category><![CDATA[编程代码]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://www.xqlab.com/?p=332</guid>
		<description><![CDATA[　　API函数在VB中得到了充分的运用，同时也让无数VB爱好者沉溺于其中。以下是俺最近收集的一些API函数在VB中应用的实例，现在分享出来，希望大伙有用。不过老实说，在VB中的API感觉不是很... ]]></description>
			<content:encoded><![CDATA[<p>　　API函数在VB中得到了充分的运用，同时也让无数VB爱好者沉溺于其中。以下是俺最近收集的一些API函数在VB中应用的实例，现在分享出来，希望大伙有用。不过老实说，在VB中的API感觉不是很好~<br />
　　<br />
　　1、如何让窗体总在最前面？<br />
　　<br />
　　*API函数声明<br />
　　Declare Function SetWindowPos Lib &#8220;user32&#8243; ( ByVal hwnd As Long, ByVal　 hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long<br />
　　注释：常量声明<br />
　　Private Const SWP_NOSIZE = &amp;H1<br />
　　Private Const SWP_NOMOVE = &amp;H2<br />
　　Private Const HWND_TOPMOST = -1<br />
　　Private Const HWND_NOTOPMOST = -2<br />
　　注释： 在某个form里写：<br />
　　SetWindowPos me.hWnd,WND_TOPMOST,0,0,0,0, SWP_NOMOVE 注释：或下面<br />
　　SetWindowPos me.hWnd,WND_TOPMOST,0,0,0,0,　SWP_NOSIZE<br />
　　<span id="more-332"></span><br />
　　2、使用API函数sendmessage，获得光标所在行和列。<br />
　　<br />
　　Sub getcaretpos(byval 　TextHwnd&amp;,LineNo&amp;,ColNo&amp;)<br />
　　　注释：TextHwnd为TextBox的hWnd属性值，　　LineNo为所在行数，ColNo为列数<br />
　　　　dim i&amp;,j&amp;,k&amp; 注释：获取起始位置到光标所在位置字节数　　　　　　　　　i=SendMessage(TextHwnd,&amp;HB0&amp;,0,0) j=i/2^16 注释：确定所在行　　　　　　LineNo=SendMessage(TextHwnd,&amp;HC9&amp;,j,0)+1<br />
　　　　注释：确定所在列<br />
　　　　k=SendMessage(TextHwnd,&amp;HBB&amp;,-1,0)<br />
　　　　ColNo=j-k+1<br />
　　End sub<br />
　　<br />
　　3、如何以某种颜色填充某区域？<br />
　　<br />
　　*API函数声明<br />
　　Private Declare Sub FloodFill Lib &#8220;gdi32&#8243; _ (ByVal hDC As Long, ByVal X As Long, ByVal Y As _ Long, ByVal crColor As Long<br />
　　注释：设(fillx,filly)为此区域内任一点<br />
　　注释：Color为某种颜色<br />
　　FloodFill Picture1.hDC, fillx, filly,Color<br />
　　<br />
　　4、如何关闭计算机？<br />
　　*API函数声明<br />
　　Declare Function ExitWindows Lib &#8220;User&#8221; (ByVal dwReturnCode As Long, ByVal wReserved As Integer) As Integer<br />
　　注释：执行<br />
　　Dim DUMMY<br />
　　DUMMY=ExitWindows(0，0)<br />
　　<br />
　　5、如何获取Windows目录和System目录？<br />
　　<br />
　　注释：复制以下代码到一模块中<br />
　　Public Declare Function GetWindowsDirectory Lib &#8220;kernel32&#8243; Alias &#8220;GetWindowsDirectoryA&#8221; (ByVal lpBuffer As String, ByVal nSize As Long) As Long<br />
　　Public Declare Function GetSystemDirectory Lib &#8220;kernel32&#8243; Alias &#8220;GetSystemDirectoryA&#8221; (ByVal lpBuffer As String, ByVal nSize As Long) As Long<br />
　　注释：在程序中调用<br />
　　Dim WindowsDirectory As String, SystemDirectory As String, x As Long<br />
　　WindowsDirectory = Space(255)<br />
　　SystemDirectory = Space(255)<br />
　　x = GetWindowsDirectory(WindowsDirectory, 255)<br />
　　x = GetSystemDirectory(SystemDirectory, 255)<br />
　　MsgBox &#8220;Windows的安装目录是:&#8221; + WindowsDirectory+&#8221;,系统目录是:&#8221; + SystemDirectory<br />
　　<br />
　　6、如何建立简单的超级连接？<br />
　　<br />
　　*API函数声明<br />
　　Private Declare Function ShellExecute Lib &#8220;shell32.dll&#8221; Alias &#8220;ShellExecute A&#8221; (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd A s Long) As Long<br />
　　注释：打开某个网址<br />
　　ShellExecute 0, &#8220;open&#8221;, &#8220;<a href="http://tyvb.126.com&quot;/">http://tyvb.126.com&#8221;/</a>, vbNullString, vbNullString, 3<br />
　　注释：给某个信箱发电子邮件<br />
　　ShellExecute hwnd, &#8220;open&#8221;, &#8220;<a href="mailto:sst95@21cn.com">mailto:sst95@21cn.com</a>&#8220;, vbNullString, vbNullString, 0<br />
　　<br />
　　7、如何得知TextBox中文字所有的行数？<br />
　　<br />
　　*API函数声明<br />
　　Declare Function SendMessage Lib &#8220;user32&#8243; Alias &#8220;SendMessageA&#8221; (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long<br />
　　Public Const EM_GETLINECOUNT = &amp;HBA<br />
　　注释：在程序中调用<br />
　　LineCnt = SendMessage(ctl.hwnd, EM_GETLINECOUNT, 0, 0)<br />
　　注释：LineCnt即为此TextBox的行数。<br />
　　<br />
　　8、如何设置ListBox的水平卷动轴的宽度？<br />
　　<br />
　　*API函数声明<br />
　　Const LB_SETHORIZONTALEXTENT = &amp;H194<br />
　　Private Declare Function SendMessage Lib &#8220;user32&#8243; Alias &#8220;SendMessageA&#8221; _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As Long<br />
　　注释：调用<br />
　　Call SendMessage(List1.hwnd, LB_SETHORIZONTALEXTENT, 400, ByVal 0&amp;)<br />
　　注释：注意400是以象素为单位，你可以根据情况自行设定。<br />
　　<br />
　　9、如何交换鼠标按键？<br />
　　<br />
　　*API函数声明<br />
　　Declare Function SwapMouseButton&amp; Lib &#8220;user32&#8243; _ (ByVal bSwap as long)<br />
　　要交换鼠标按键，将bSwap参数设置为True。要恢复正常设置，将bSwap设置为False。 然后调用函数就可以交换和恢复鼠标按键了。<br />
　　<br />
　　10、如何让窗体的标题条闪烁以引起用户注意？<br />
　　<br />
　　在窗体中放一个Timer控件Timer1,设置其Inteval=200<br />
　　*API函数声明<br />
　　Private Declare Function FlashWindow Lib &#8220;user32&#8243; (ByVal hwnd As Long, ByVal bInvert As Long) As Long<br />
　　注释：在窗体中写下如下代码:<br />
　　Private Sub Timer1_Timer()<br />
　　　FlashWindow Me.hwnd, True<br />
　　End Sub<br />
　　<br />
　　11、怎样找到鼠标指针的XY坐标？<br />
　　<br />
　　*API函数声明<br />
　　Type POINTAPI<br />
　　x As Long<br />
　　y As Long<br />
　　End Type<br />
　　Declare Function GetCursorPos Lib &#8220;user32&#8243; (lpPoint As POINTAPI) As Long<br />
　　调用：<br />
　　GetCursorPos z<br />
　　print z.x<br />
　　print z.y<br />
　　<br />
　　<br />
　　12、怎样获得和改变双击鼠标的时间间隔？<br />
　　<br />
　　获得鼠标双击间隔时间：<br />
　　Public Declare Function GetDoubleClickTime Lib &#8220;user32&#8243; Alias _ &#8220;GetDoubleClickTime&#8221; () As Long<br />
　　<br />
　　获得鼠标双击间隔时间：<br />
　　Declare Function SetDoubleClickTime Lib &#8220;user32&#8243; Alias &#8220;SetDoubleClickTime&#8221; (ByVal wCount As Long) As Long<br />
　　注释：注意：这种改变将影响到整个操作系统<br />
　　<br />
　　以上两个函数都可精确到毫秒级。<br />
　　<br />
　　<br />
　　13、在程序中如何打开和关闭光驱门？<br />
　　<br />
　　*API函数声明如下:<br />
　　Private Declare Function mciSendString Lib &#8220;winmm.dll&#8221; Alias &#8220;mciSendStringA&#8221; (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long<br />
　　注释：调用时的代码如下<br />
　　Dim Ret As Long<br />
　　Dim RetStr As String<br />
　　注释：打开光驱门<br />
　　Ret = mciSendString(&#8220;set CDAudio door open&#8221;, RetStr, 0, 0)<br />
　　注释：关闭光驱门<br />
　　Ret = mciSendString(&#8220;set CDAudio door closed&#8221;, RetStr, 0, 0)<br />
　　<br />
　　<br />
　　14、如何获得Windows启动方式?<br />
　　<br />
　　在Form1中加入一个CommandButton、一个Label并加入如下代码:<br />
　　Private Declare Function GetSystemMetrics Lib &#8220;user32&#8243; (ByVal nIndex As Long) As Long<br />
　　Const SM_CLEANBOOT = 67<br />
　　<br />
　　Private Sub Command1_Click()<br />
　　　Select Case GetSystemMetrics(SM_CLEANBOOT)<br />
　　　Case 1<br />
　　　　Label1 = &#8220;安全模式.&#8221;<br />
　　　Case 2<br />
　　　　Label1 = &#8220;支持网络的安全模式.&#8221;<br />
　　　Case Else<br />
　　　　Label1 = &#8220;Windows运行在普通模式.&#8221;<br />
　　　End Select<br />
　　End Sub<br />
　　<br />
　　<br />
　　15、怎样使Ctrl-Alt-Delete无效？<br />
　　<br />
　　*API函数声明<br />
　　Private Declare Function SystemParametersInfo Lib &#8220;user32&#8243; Alias &#8220;SystemParametersInfoA&#8221; (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long<br />
　　编写如下函数：<br />
　　Sub DisableCtrlAltDelete(bDisabled As Boolean)<br />
　　Dim X As Long<br />
　　X = SystemParametersInfo(97, bDisabled, CStr(1), 0)<br />
　　End Sub<br />
　　使Ctrl-Alt-Delete无效 ：<br />
　　Call DisableCtrlAltDelete(True)<br />
　　恢复Ctrl-Alt-Delete ：<br />
　　Call DisableCtrlAltDelete(False)<br />
　　<br />
　　<br />
　　16、如何移动没有标题栏的窗口？<br />
　　<br />
　　我们一般是用鼠标按住窗口的标题栏，然后移动窗口，当窗口没有标题栏时，我们可以用下面的方法来移动窗口：<br />
　　<br />
　　*API函数声明：<br />
　　Declare Function ReleaseCapture Lib &#8220;user32&#8243; () As Long Declare Function SendMessage Lib &#8220;user32&#8243; Alias &#8220;SendMessageA&#8221; (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long<br />
　　Public Const HTCAPTION = 2<br />
　　Public Const WM_NCLBUTTONDOWN = &amp;HA1<br />
　　在 Form_MouseDown 事件中：<br />
　　Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)<br />
　　ReleaseCapture SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION,0&amp;<br />
　　End Sub<br />
　　<br />
　　<br />
　　17、VB中如何使用延时函数？<br />
　　<br />
　　*API函数声明：<br />
　　Declare Sub Sleep Lib &#8220;kernel32&#8243; (ByVal dwMilliseconds As Long)<br />
　　调用：<br />
　　注释：延时1秒<br />
　　Call Sleep(1000)<br />
　　<br />
　　<br />
　　18、调用修改屏幕保护口令的窗口：<br />
　　<br />
　　Private Declare Function PwdChangePassword Lib &#8220;mpr&#8221; Alias &#8220;PwdChangePasswordA&#8221; (ByVal lpcRegkeyname As String, ByVal hwnd As Long, ByVal uiReserved1 As Long, ByVal uiReserved2 As Long) As Long<br />
　　调用：<br />
　　Call PwdChangePassword(&#8220;SCRSAVE&#8221;, Me.hwnd, 0, 0)<br />
　　<br />
　　19、使Windows开始屏幕保护：<br />
　　*API函数声明<br />
　　Private Declare Function SendMessage Lib &#8220;user32&#8243;<br />
　　Alias &#8220;SendMessageA&#8221; (ByVal hWnd As Long, ByVal wMsg<br />
　　As Long, ByVal wParam As Long, ByVal lParam As Long)<br />
　　As Long<br />
　　Const WM_SYSCOMMAND = &amp;H112&amp;<br />
　　Const SC_SCREENSAVE = &amp;HF140&amp;<br />
　　注释：调用<br />
　　Dim result As Long<br />
　　result = SendMessage(Form1.hWnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0&amp;)<br />
　　<br />
　　<br />
　　20、如何改变Windows桌面背景？<br />
　　*API函数声明<br />
　　Const SPI_SETDESKWALLPAPER = 20<br />
　　Const SPIF_UPDATEINIFILE = &amp;H1<br />
　　Declare Function SystemParametersInfo Lib &#8220;user32&#8243; Alias &#8220;SystemParametersInfoA&#8221; (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long<br />
　　注释：调用<br />
　　Call SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, &#8220;C:windowsClouds.bmp&#8221;, SPIF_UPDATEINIFILE)<br />
　　<br />
　　<br />
　　21、怎样确定系统是否安装了声卡？<br />
　　<br />
　　*API函数声明：<br />
　　Declare Function waveOutGetNumDevs Lib &#8220;winmm.dll&#8221; Alias &#8220;waveOutGetNumDevs&#8221; () As Long<br />
　　代码如下：<br />
　　Dim i As Integer<br />
　　i = waveOutGetNumDevs()<br />
　　If i &gt; 0 Then MsgBox &#8220;你的系统可以播放声音。&#8221;, vbInformation, &#8220;声卡检测&#8221;<br />
　　Else<br />
　　MsgBox &#8220;你的系统不能播放声音。&#8221;, vbInformation, &#8220;声卡检测&#8221;<br />
　　End If<br />
　　<br />
　　<br />
　　22、如何找到CD-ROM驱动器的盘号？<br />
　　下面的函数将检查你计算机所有的驱动器看是否是 CD-ROM，如果是就返回驱动器号，如果没有就返回空字符<br />
　　Public Function GetCDROMDrive() As String<br />
　　　Dim lType As Long,i As Integer,tmpDrive as String,found as Boolean<br />
　　　On Error GoTo errL<br />
　　　For i = 0 To 25<br />
　　　　tmpDrive = Chr(65 + i) &amp; &#8220;:&#8221;<br />
　　　　lType = GetDriveType(tmpDrive) 注释：Win32 API 函数<br />
　　　　If (lType = DRIVE_CDROM) Then 注释：Win32 API 常数<br />
　　　　　found = True<br />
　　　　　Exit For<br />
　　　　End If<br />
　　　Next<br />
　　　If Not found Then tmpDrive = &#8220;&#8221;<br />
　　　BI_GetCDROMDrive = tmpDrive<br />
　　　exit Function<br />
　　　errL: msgbox error$<br />
　　End Function<br />
　　<br />
　　<br />
　　23、如何将文件放入回收站？<br />
　　<br />
　　**API函数声明<br />
　　Public Type SHFILEOPSTRUCT<br />
　　hwnd As Long<br />
　　wFunc As Long<br />
　　pFrom As String<br />
　　pTo As String<br />
　　fFlags As Integer<br />
　　fAnyOperationsAborted As Long<br />
　　hNameMappings As Long<br />
　　lpszProgressTitle As Long<br />
　　End Type<br />
　　Public Declare Function SHFileOperation Lib _ &#8220;shell32.dll&#8221; Alias &#8220;SHFileOperationA&#8221; (lpFileOp As SHFILEOPSTRUCT) As Long<br />
　　Public Const FO_DELETE = &amp;H3<br />
　　Public Const FOF_ALLOWUNDO = &amp;H40<br />
　　注释：调用<br />
　　Dim SHop As SHFILEOPSTRUCT, strFile as string<br />
　　With SHop<br />
　　.wFunc = FO_DELETE<br />
　　.pFrom = strFile + Chr(0)<br />
　　.fFlags = FOF_ALLOWUNDO<br />
　　End With<br />
　　<br />
　　<br />
　　24、VB中如何使用未安装的字体？<br />
　　Declare Function AddFontResource Lib &#8220;gdi32&#8243; Alias &#8220;AddFontResourceA&#8221; (ByVal lpFileName As String) As Long<br />
　　Declare Function RemoveFontResource Lib &#8220;gdi32&#8243; Alias &#8220;RemoveFontResourceA&#8221; (ByVal lpFileName As String) As Long<br />
　　增加字体：<br />
　　Dim lResult As Long<br />
　　lResult = AddFontResource(&#8220;c:myAppmyFont.ttf&#8221;)<br />
　　删除字体：<br />
　　Dim lResult As Long<br />
　　lResult = RemoveFontResource(&#8220;c:myAppmyFont.ttf&#8221;)<br />
<h3>相关日志</h3>
<ul class="related_post">
<li><a href="http://www.xqlab.com/html/y2009/360.html" title="VB读写TXT文件的常用方法">VB读写TXT文件的常用方法</a></li>
<li><a href="http://www.xqlab.com/html/y2009/353.html" title="VB编程中的一些细节和经验">VB编程中的一些细节和经验</a></li>
<li><a href="http://www.xqlab.com/html/y2009/327.html" title="VB:Format 函数示例">VB:Format 函数示例</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.xqlab.com/html/y2009/332.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
