打印本文 打印本文 关闭窗口 关闭窗口
Visual Basic开发要点三则
作者:武汉SEO闵涛  文章来源:敏韬网  点击数632  更新时间:2009/4/23 14:58:37  文章录入:mintao  责任编辑:mintao
一、 用控件拖放表单

---- 怎样用控件拖放表单呢?很简单,将这段代码插入到Declare部分。

Declare Function ReleaseCapture Lib "user32" () As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

再在控制的Mousedown事件中插入:

Sub Command1_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Ret&
ReleaseCapture
Ret& = SendMessage(Me.hWnd, &H112, &HF012, 0)
End Sub

二、 把表单放在屏幕的正中央

---- 在开发VB程序时,一般希望将表单放在屏幕可利用区域的正中央,实现上可以利用Move(Screen.Width - Width)\2,(Screen.Height - Height)\2的方法来实现。但是当用户使用Windows 95或 NT 操作系统时,在 屏幕底端会有一任务条,上述的实现方法并未考虑该任务条所占的空间,表单实际并未处于屏幕可利用区域的正中央。下面的代码段实现了在每次启动应用程序时,无论屏幕是否有任务条,表单都会屏幕可利用区域的正中央。在工程中增添一模块,在模块中加上如下的代码:

Option Explicit
Private Const SPI_GETWORKAREA = 48
Private Declare Function SystemParametersInfo& Lib "User32" Alias
"SystemParametersInfoA" (ByVal uAction As Long,ByVal uParam As Long, lpvParam As
Any, ByVal fuWinIni As Long)
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Public Function CenterForm32 (frm As Form)
Dim ScreenWidth&, ScreenHeight&, ScreenLeft&, ScreenTop&
Dim DesktopArea As RECT
Call SystemParametersInfo (SPI_GETWORKAREA, 0, DesktopArea, 0)
ScreenHeight = (DesktopArea.Bottom - DesktopArea.Top) * Screen.TwipsPerPixelY
ScreenWidth = (DesktopArea.Right - DesktopArea.Left) * Screen.TwipsPerPixelX
ScreenLeft = DesktopArea.Left * Screen.TwipsPerPixelX
ScreenTop = DesktopArea.Top * Screen.TwipsPerPixelY
frm.Move (ScreenWidth - frm.Width)\ 2 + ScreenLeft, (ScreenHeight - frm.Height) \ 2
+ ScreenTop
d Function



---- 要调用CenterForm32函数,可在表单的Load事件中增添代码CenterForm32 Me即可。以上代码在VB4/32,VB5 中实现。

三、 在RichTextBox 控件中实现上、下标形式

---- VB提供了一个优秀的控件RichTextBox,我们可以在其中实现文本的各种编辑方式。下面的程序是在RichTextBox 控件中实现上标和下标的形式,主要是使作为上、下标的字符的尺寸小一些,位置在基线上下浮动。程序利用属性SelCharOffset,由它确定RichTextBox 控件中的文本是出现在基线上(正常状态),当SelCharOffset $#@62;0 时,文本出现在基线之上,成为上标形式;

---- 当SelCharOffset$#@60; 0 时,文本出现在基线之下,成为下标形式。

---- 该属性在设计时无效。

---- 在表单的Load事件中添加以下代码:

Private Sub Form_Load()
RichTextBox1.Font.Name = "Times New Roman"
RichTextBox1.Font.Size = 10
RichTextBox1.Text = "H2SO4"
" Move the numbers down 2 points.
OffsetRichText RichTextBox1, 1, 1, 2
OffsetRichText RichTextBox1, 4, 1, -2
End Sub

Private Sub OffsetRichText(box As RichTextBox, start As Integer, length As Integer,
offset As Integer)

"box指RichTextBox控件;start指作为上下标的
"字符的起始位置;length指上下标字符的长度;
"offset指上标还是下标,大于0上标;小于0下标。

box.SelStart = start
box.SelLength = length
box.SelFontSize = box.Font.Size -abs(offset)
box.SelCharOffset = ScaleY(offset,vbPoints, vbTwips)
box.SelStart = 0
box.SelLength = 0
End Sub

上述程序在WINDOWS98/ME中通过。
156

打印本文 打印本文 关闭窗口 关闭窗口