⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vbtips4.htm

📁 所有我收藏的VB技巧
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>VBTips5</title>
</head>

<body>

<h1 align="center"><a name="home"></a>VB技巧<font size="5"><strong>(5)</strong></font></h1>

<blockquote>
    <p><strong>1、</strong><a href="#tips0"><strong>使用 IIF
    和 SWITCH 以精减代码</strong></a><strong><br>
    2、</strong><a href="#tips1"><strong>放置“透明”的图片</strong></a><strong><br>
    3、</strong><a href="#tips2"><strong>编译时不要自动使用
    快速代码优化</strong></a><strong><br>
    4、</strong><a href="#tips3"><strong>避免装载多份应用程序</strong></a><strong><br>
    5、</strong><a href="#tips4"><font size="3"><strong>启动时禁止装入</strong></font><font
    size="3" face="宋体"><strong> Add-Ins</strong></font></a><font
    size="3" face="宋体"><strong><br>
    6</strong></font><font size="3"><strong>、</strong></font><a
    href="#tips5"><strong>快速查找属性</strong></a><strong><br>
    7、</strong><a href="#tips6"><strong>在字符串中使用双引号</strong></a><strong><br>
    8、</strong><a href="#tips7"><strong>混合字符串的长度</strong></a><strong><br>
    9、</strong><a href="#tips8"><strong>取得应用所在的目录</strong><font
    face="宋体"><strong> </strong></font></a><font
    face="宋体"><strong><br>
    10</strong></font><strong>、</strong><a href="#tips9"><strong><b>快速交换整数</b></strong></a><strong><b><br>
    </b></strong></p>
    <div align="center"><center><table border="0" cellspacing="1"
    width="88%">
        <tr>
            <td width="80%"><p align="left"><a
            href="vbtips.htm#Return">[1]</a> <a
            href="vbtips1.htm">[2]</a> <a href="vbtips2.htm">[3]</a>
            <a href="vbtips3.htm">[4]</a> [5] <a
            href="vbtips5.htm">[6]</a> <a href="vbtips7.htm">[7]</a>
            <a href="#home">[8]</a> <a href="vbtips9.htm">[9]</a>
            <a href="vbtips10.htm">[10]</a></p>
            </td>
            <td><p align="right"><font size="2">第五页(共十页)</font></p>
            </td>
        </tr>
    </table>
    </center></div>
</blockquote>

<hr>
<div align="center"><center>

<table border="0" cellspacing="1" width="88%">
    <tr>
        <td width="50%"><a name="tips0"></a><strong>使用 IIF
        和 SWITCH 以精减代码</strong><br>
        在很多地方你都可以使用一个更紧凑的 IIf
        函数来代替 If...Else...Endif 的结构: <br>
        例:返回两个值中较大的一个 maxValue =
        IIf(first &gt;= second, first, second) <br>
        Switch
        则是一个很少使用的函数,可是在很多方面它都提供比
        If...ElesIf 结构更好的 <br>
        例:判断 &quot;x&quot; 是正、负还是 null? <br>
        Print Switch(x&lt;0,&quot;负&quot;,x&gt;0,&quot;正&quot;,
        True, &quot;Null&quot;) <br>
        [返回技巧索引] <br>
        变量的地址 (97/11/29) <br>
        VB5 内置了一个 VarPtr 函数,可是此函数在
        VB4 中没有提供。可是你知道吗?VB4 的运行库中已经包含了此函数。只是在用它之前,我们需要声明一下:
        <br>
        #If Win16 Then <br>
        Declare Function VarPtr Lib &quot;VB40016.DLL&quot;
        (variable As Any) As Long <br>
        #Else <br>
        Declare Function VarPtr Lib &quot;VB40032.DLL&quot;
        (variable As Any) As Long <br>
        #End If <br>
        此函数在传递一个 Type
        结构(如果此结构要求其一段是另一个变量或记录的地址)给一个外部的
        API 程序时十分有用。 <br>
        <a href="#home">返回</a><p><a name="tips1"></a><strong>放置“透明”的图片</strong><br>
        在 VB
        中,如果你试着把一只有鸟的图片放到背景的一棵树上,你就会发现树会被鸟遮住一个矩形的区域(即鸟的图片矩形)。我们可以通过以下方法使图片上非鸟的其它部分变透明:
        <br>
        我们可以利用一个 WinAPI 函数 BitBlt
        对图形进行一系列的位操作来达到此目的。
        <br>
        函数声明: <br>
        Declare Function BitBlt Lib &quot;gdi32&quot; Alias
        &quot;BitBlt&quot; (ByVal hDestDC As Long, ByVal x As
        Long, ByVal y As Long, ByVal nWidth As Long, ByVal
        nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As
        Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long<br>
        参数解释: <br>
        目标环境:hDestDC——目标设备环境;x——左上角;y——顶端;nWidth——宽;nHeight——高
        <br>
        源环境:hSrcDC——源设备环境;xSrc——源左上角;ySrc——源顶端;
        <br>
        dwRop——位处理操作,如 vbSrcAnd;vbSrcAnd;vbSrcCopy;vbSrcErase;vbSrcInvert
        等 <br>
        (目标环境或源环境只能是 Picture, Form 或
        Printer 对象。各单位为象素。) <br>
        进行处理之前,我们需要对鸟的图片进行处理:先复制一份相同的图形,将其应该透明之处(鸟的背景)设置为黑色(设此图为sPic),再将另一图做以下处理:要复制的地方(鸟)设置为黑色,其余地方设置(鸟的背景)为白色(设此图为Mask)。
        <br>
        设树的图形为名dPic。 <br>
        最后,请加入以下代码: <br>
        R=BitBlt(dPic.hdc,0,0,sPic.Width,sPic.Height,Mask.hdc,0,0,vbScrCopy)
        <br>
        R=BitBlt(dPic.hdc,0,0,sPic.Width,sPic.Height,sPic.hdc,0,0,vbScrInvert)
        <br>
        后记: <br>
        1、VB 中的 PaintPicture
        方法提供类似功能,但速度不及此方法; <br>
        2、在此方法上稍微加入一些代码,就不难实现动画的显示。
        <br>
        3、VB 例子中的 CallDlls 就使用此方法。<br>
        <a href="#home">返回</a></p>
        <p><a name="tips2"></a><strong>编译时不要自动使用
        快速代码优化</strong><br>
        如果你第一次使用 VB
        的本地代码优化选项,你可能会立即尝试选择“优化代码选项”。可是,你知道吗?这样做并不一定保证使你的程序得到最佳性能。
        <br>
        除非你拥有大量内存,不然程序的性能优化一般不会运行很快。因为这将导致程序装载速度缓慢,在内存不足的机器上特别明显,这样“优化代码选项”就可能让你的用户觉得好象比“优化大小选项”还慢。
        <br>
        基于以上原因,你可以考虑用 P
        代码编译你的程序,特别是大型的、UI 和数据库加强的程序。本地“优化代码选项”所获得的性能并不一定可以弥补程序增长大小后带来的问题。
        <br>
        要决定你到底适合那种编译方式,请使用 VB
        企业版上的 Application Performance Explorer (APE)
        。 <br>
        <a href="#home">返回</a></p>
        <p><a name="tips3"></a><strong>避免装载多份应用程序</strong><br>
        你的程序可能只支持单一用户,那么怎么来避免多用户同时使用它吗?
        <br>
        你可以利用 App 对象的 PrevInstance
        属性来轻易达到你的目的。<br>
        我们可以在程序打开时加入以下代码来验证:<br>
        If App.PrevInstance Then<br>
        MsgBox (&quot;程序已经运行,不能再次装载。&quot;),
        vbExclamation<br>
        Unload Me<br>
        End If <br>
        <a href="#home">返回</a></p>
        <p><a name="tips4"></a><font size="3"><strong>启动时禁止装入</strong></font><font
        size="3" face="宋体"><strong> Add-Ins<br>
        </strong></font>启动VB时, Add-Ins 将加载。如果Add-Ins中有错误的话,每次都可能产生错误。
        为了启动时禁止装入 Add-Ins, 在启动VB前,编辑
        Windows 目录中的 VBAddin.INI 文件。找到以下的语句:<br>
        <br>
        AppWizard.Wizard=1<br>
        <br>
        将 1 改为 0 。 <br>
        <a href="#home">返回</a></p>
        <p><a name="tips5"></a><strong>快速查找属性<br>
        </strong>在属性窗口中,在打入字符时,按住
        Ctrl+Shift。属性窗口将自动翻滚到以该字符开头的地方。
        <br>
        <a href="#home">返回</a></p>
        <p><a name="tips6"></a><strong>在字符串中使用双引号<br>
        </strong>使用 Chr$(34) 连接是个办法,用
        &quot;&quot; 更简单。如: MyName = &quot;我的名字是
        &quot;&quot;<font face="宋体">Blackcat</font>&quot;&quot;。&quot;
        <br>
        <a href="#home">返回</a></p>
        <p><a name="tips7"></a><strong>混合字符串的长度<br>
        </strong>在中文环境下,每个字被当做两个
        Byte :<br>
        Len(&quot;汉1&quot;) = 2<br>
        LenB(&quot;汉1&quot;) = 4<br>
        但在许多情况下,我们希望中文字长度为 2,英文字符为
        1。可用以下的函数:<br>
        LenB(StrConv(&quot;汉1&quot;), vbFormUnicode))<br>
        <a href="#home">返回</a></p>
        <p><a name="tips8"></a><strong>取得应用所在的目录<br>
        </strong>使用 App.Path
        可以得到应用所在的目录。不过得注意,当在根目录下时,Path
        的返回值最右字符为 “\” ,如“c:\”,而如果不在根目录,则最右字符非
        “\”,如“c:\vb5”。所以在使用 Path
        做连接时,应使用以下的代码:<br>
        <br>
        Dim FileName as string<br>
        Dim fullpath As String<br>
        If Right(App.Path, 1) = &quot;\&quot; Then <br>
        fullpath = App.Path + FileName<br>
        Else<br>
        fullpath = App.Path + &quot;\&quot; + FileName<br>
        End If<br>
        或者:<br>
        pth$ = app.Path &amp; IIf(Len(app.Path) &gt; 3,
        &quot;\&quot;, &quot;&quot;)<br>
        <a href="#home">返回</a></p>
        <p><a name="tips9"></a><strong><b>快速交换整数<br>
        </b></strong>可用以下的代码快速交换两个整数(Interger):<br>
        a = a xor b<br>
        &nbsp; b = a xor b<br>
        &nbsp; a = a xor b<br>
        <a href="#home">返回</a></p>
        </td>
    </tr>
</table>
</center></div>

<hr>
<div align="center"><center>

<table border="0" cellspacing="1" width="88%">
    <tr>
        <td width="80%"><p align="left"><a
        href="vbtips.htm#Return">[1]</a> <a href="vbtips1.htm">[2]</a>
        <a href="vbtips2.htm">[3]</a> <a href="vbtips3.htm">[4]</a>
        [5] <a href="vbtips5.htm">[6]</a> <a href="vbtips7.htm">[7]</a>
        <a href="#home">[8]</a> <a href="vbtips9.htm">[9]</a> <a
        href="vbtips10.htm">[10]</a></p>
        </td>
        <td><p align="right"><font size="2">第五页(共十页)</font></p>
        </td>
    </tr>
</table>
</center></div>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -