|
|
|
VB问题全功略(15) |
[上一页](15)[下一页] |
71、如何用鼠标移动没有标题的
Form,或移动 Form 中的控制项? 72、如何判断目前电脑中所有磁盘之型态? 73、检查文件是否存在? 74、如何用 Image
来做成带有图片的按钮,按下鼠标时如同按钮般会变换图片? 75、听说 VB 6.0 的
CommandButton 己经可放图片,要如何使用?
71、如何用鼠标移动没有标题的
Form,或移动 Form 中的控制项?
在声明区中放入以下声明:
'16 位版本: ( Sub 无返回值
) Private Declare Sub ReleaseCapture Lib "User" () Private Declare
Sub SendMessage Lib "User" (ByVal hwnd As Integer, ByVal wMsg As Integer,
ByVal wParam As Integer, lParam As Long)
'32 位版本: ( Function
有返回值,Integer 改成 Long ) Private Declare Function ReleaseCapture Lib
"user32" () As Long Private Declare Function SendMessage Lib "user32"
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam
As Long, lParam As Any) As Long
'共用常数: Const WM_SYSCOMMAND =
&H112 Const SC_MOVE = &HF012
'若要移动
Form,程序码如下: Private Sub Form_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y As Single) Dim i As Long i =
ReleaseCapture i = SendMessage(Form1.hwnd, WM_SYSCOMMAND, SC_MOVE,
0) End Sub
'以上功能也适用于用鼠标在 Form 中移动控制项,程序码如下: Private Sub
Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As
Single) Dim i As Long i = ReleaseCapture i =
SendMessage(Command1.hwnd, WM_SYSCOMMAND, SC_MOVE, 0) End
Sub
72、如何判断目前电脑中所有磁盘之型态?
在 Form 中放置一个 ListBox 名称为 List1
Private
Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal
nDrive As String) As Long
Private Sub Form_Load() Dim i As
Integer Dim ret As Long '返回值 Dim wtype As String '磁盘型态 For i = 65
To 90 'ASC(A) ~ ASC(Z) wtype = "" ret = GetDriveType(Chr$(i) &
":\") '传入磁盘代号 Select Case ret Case 2 wtype = "软盘" Case
3 wtype = "硬盘" Case 4 wtype = "网路磁盘" Case 5 wtype =
"光盘" End Select If wtype <> "" Then List1.AddItem Chr$(i)
& ":\" & vbTab & wtype Next End Sub
若是 16
位程序,声明略有不同,如下: Private Declare Function GetDriveType Lib "Kernel"
(ByVal nDrive As Integer) As Integer 传入的参数型态是 Integer,0 代表 A 磁盘,依次加 1,2
代表 C 磁盘。
73、检查文件是否存在?
Function FileExists(filename As String) As
Integer Dim i As Integer On Error Resume Next i =
Len(Dir$(filename)) If Err Or i = 0 Then FileExists = False Else
FileExists = True End Function
传入之参数是含完整路径之文件名称,若文件存在,则传回
-1,否则返回 0。
74、如何用 Image
来做成带有图片的按钮,按下鼠标时如同按钮般会变换图片?
在 Form 中放三个 Image
Control,名称分别为 Image1、LockOpen、LockClosed,并设定好 LockOpen 及 LockClosed 的
Picture 属性为开启及关闭的 Icon,然后
Sub Form_Load() Image1.Picture =
LockOpen.Picture End Sub
Sub Image1_Click() Static
LockedFlag As Integer If LockedFlag Then Image1.Picture =
LockOpen.Picture Else Image1.Picture = LockClosed.Picture End
If LockedFlag = Not LockedFlag End Sub
以上之程序代码虽然在 VB
的各个版本都适用,但 VB 6.0 的 CommandButton 已经可以放置图片了,所以 VB 6.0 可以直接使用 CommandButton
达到以上功能!
75、听说 VB 6.0 的 CommandButton
己经可放图片,要如何使用?
先将 Style 属性设成 「1 - 图片外观」,再设定
Picture 属性即可。 若希望 Mouse_Down 时可改变图片,则需要再设定 DownPicture 属性。 若希望按钮
Disable 时可改变图片,则需要再设定 DisabledPicture
属性。 |
|