《2022年VB小程序 .pdf》由会员分享,可在线阅读,更多相关《2022年VB小程序 .pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、 VB 实现按钮浮动效果关于浮动按钮的实现思路不少,多是采取多图片重叠显示来实现。这种方法代码量多,实现起来较繁琐。因为,一个按钮还好,如果有十个按钮呢?一个按钮三个图片,十个就要三十个图片,可不是闹着玩的。我的思路是:舍弃 CommandButton 控件,每个按钮用4 条 Line 控件和一个Label 控件替代。4 条 Line围住 Label 的边缘,调入窗体时,置显示属性为False,并将左、上直线的颜色设为白色,右、下直线的颜色设为黑色。当鼠标移到Label 上时,4 条 Line 的显示属性置True;当鼠标离开按钮时,将4条 Line 的显示属性设置为False。这样在视觉上就
2、完全得到立体浮动的效果。另外,VB 的 Line 控件还支持直线倾斜,以此类推,完全可以做出更加美观的倾斜按钮。篇幅所限,下面仅给出一个按钮实现浮动效果的源代码。Option Explicit Private Sub Form_Load()初始 Form 与 Label Form1.Caption=“浮动按钮 Form1.KeyPreview=False label1.Caption=“确定 初始 4 条 Line 的显示属性为False Line1.Visible=False Line2.Visible=False Line3.Visible=False Line4.Visible=Fals
3、e 初始 4 条 Line 的颜色Line1.BorderColor=HE0E0E0 Line2.BorderColor=HE0E0E0 Line3.BorderColor=H808080 Line4.BorderColor=H808080 End Sub Private Sub Form_MouseMove(Button As Integer,Shift As Integer,X As Single,Y As Single)鼠标指针在窗体上(不在按钮上)时,置 4 条 Line 的显示属性为False Line1.Visible=False Line2.Visible=False Line3
4、.Visible=False Line4.Visible=False End Sub Private Sub label1_MouseMove(Button As Integer,Shift As Integer,X As Single,Y As Single)鼠标指针在按钮上时,置4条 Line 的显示属性为True Line1.Visible=True Line2.Visible=True Line3.Visible=True 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 6 页 -Line4.Visible rue End Sub-setup类型的进度条Dim tenth
5、 As Long 条件编译#If Win32 Then Private Declare Function BitBlt Lib gdi32 _(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#Else Private Declare Function BitBlt L
6、ib GDI(ByVal hDestDC As _ Integer,ByVal x As Integer,ByVal y As Integer,ByVal nWidth _ As Integer,ByVal nHeight As Integer,ByVal hSrcDC As Integer,_ ByVal xSrc As Integer,ByVal ySrc As Integer,ByVal dwRop As _ Long)As Integer#End If Sub UpdateStatus(FileBytes As Long)-更新 Picture1 status bar-Static p
7、rogress As Long Dim r As Long Const SRCCOPY=&HCC0020 Dim Txt$progress=progress+FileBytes If progress Picture1.ScaleWidth Then progress=Picture1.ScaleWidth End If Txt$=Format$(CLng(progress/Picture1.ScaleWidth)*100)+%Picture1.Cls Picture1.CurrentX=_(Picture1.ScaleWidth-Picture1.TextWidth(Txt$)2 Pictu
8、re1.CurrentY=_(Picture1.ScaleHeight-Picture1.TextHeight(Txt$)2 Picture1.Print Txt$Picture1.Line(0,0)-(progress,Picture1.ScaleHeight),_ Picture1.ForeColor,BF r=BitBlt(Picture1.hDC,0,0,Picture1.ScaleWidth,_ 名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 6 页 -Picture1.ScaleHeight,Picture1.hDC,0,0,SRCCOPY)End Sub Priva
9、te Sub Command1_Click()Picture1.ScaleWidth=109 tenth=10 For i=1 To 11 Call UpdateStatus(tenth)x=Timer While Timer x+0.75 DoEvents Wend Next End Sub Private Sub Form_Load()Picture1.FontBold=True Picture1.AutoRedraw=True Picture1.BackColor=vbWhite Picture1.DrawMode=10 Picture1.FillStyle=0 Picture1.For
10、eColor=vbBlue End Sub-图像浏览小程序下面分步设置各控件的属性:1.设置窗体的Caption 属性为“图片浏览器”,BorderStyle 属性为 3,即窗体大小不能改变。2.设置 Label1 的属性为“当前驱动器”,Label2 的属性为“当前目录”,Label3 的属性为“当前文件”;Label4 的属性为“当前图像”,Label5 的属性为“当前文件路径”。3.设置 Text1 的 Text 属性为空。4.设置 Image1 的 Stretch 属性为 True,即所装入的图形能够缩放以适应图像框大小。5.设置 Command1 的 Caption 属性为“确定”,
11、Command2 的 Caption 属性为“退出”。Private Sub Command1_Click()Image1.Picture=LoadPicture(Text1.Text)当单击“确定”时,文本框中的文件在图像框中显示出来End Sub Private Sub Command2_Click()当单击“退出”时,弹出是否退出系统对话框Dim exi As String exi=MsgBox(您真的想退出吗?,vbYesNo+vbQuestion+vbDefaultButton1,退出)If exi=vbYes Then 名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共
12、6 页 -End End If End Sub Private Sub Dir1_Change()File1.Path=Dir1 当目录发生变化时,文件列表框中的文件也相应变化End Sub Private Sub Drive1_Change()Dir1.Path=Drive1 当驱动器变化时,目录列表也相应变化End Sub Private Sub File1_Click()Text1.Text=File1.Path+File1 用来在文本框中显示被选中的图形名 End Sub Private Sub File1_DblClick()Image1.Picture=LoadPicture(Fi
13、le1.Path+File1)当双击文件列表中的文件时,文件在图像框中显示出来End Sub Private Sub Form_Load()Command1.Enabled=False 在未选择图形文件时“确定”按钮不可用File1.Pattern=*.bmp;*.jpg;*.ico;*.cur 在 文 件 列 表 框 中 显示 扩 展 名为.BMP、.JPG、.ICO、.CUR 的文件 End Sub Private Sub Text1_Change()Command1.Enabled=True 当文本框中的内容变化时使“确定”按钮可用End Sub-浮动按钮小程序Option Explic
14、it Private Sub Form_Load()初始 Form 与 Label Form1.Caption=浮动按钮 Form1.KeyPreview=False Label1.Caption=确定 初始 4 条 Line 的显示属性为False Line1.Visible=False Line2.Visible=False Line3.Visible=False Line4.Visible=False 初始 4 条 Line 的颜色Line1.BorderColor=&HE0E0E0 Line2.BorderColor=&HE0E0E0 Line3.BorderColor=&H80808
15、0 Line4.BorderColor=&H808080 名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 6 页 -End Sub Private Sub Form_MouseMove(Button As Integer,Shift As Integer,X As Single,Y As Single)鼠标指针在窗体上(不在按钮上)时,置 4 条 Line 的显示属性为False Line1.Visible=False Line2.Visible=False Line3.Visible=False Line4.Visible=False End Sub Private Sub L
16、abel1_Click()End End Sub Private Sub Label1_MouseMove(Button As Integer,Shift As Integer,X As Single,Y As Single)鼠标指针在按钮上时,置4 条 Line 的显示属性为True Line1.Visible=True Line2.Visible=True Line3.Visible=True Line4.Visible=True End Sub-VB 实现按钮浮动Option Explicit Private Sub Form_Load()初始 Form 与 Label Form1.Ca
17、ption=Form1.KeyPreview=False Label1.Caption=确定 初始 4 条 Line 的显示属性为False Line1.Visible=False Line2.Visible=False Line3.Visible=False Line4.Visible=False 初始 4 条 Line 的颜色Line1.BorderColor=&HE0E0E0 Line2.BorderColor=&HE0E0E0 Line3.BorderColor=&H808080 Line4.BorderColor=&H808080 End Sub Private Sub Form_M
18、ouseMove(Button As Integer,Shift As Integer,X As Single,Y As Single)鼠标指针在窗体上(不在按钮上)时,置 4 条 Line 的显示属性为False 名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 6 页 -Line1.Visible=False Line2.Visible=False Line3.Visible=False Line4.Visible=False End Sub Private Sub Label1_Click()End End Sub Private Sub Label1_MouseMove(B
19、utton As Integer,Shift As Integer,X As Single,Y As Single)鼠标指针在按钮上时,置4 条 Line 的显示属性为True Line1.Visible=True Line2.Visible=True Line3.Visible=True Line4.Visible=True End Sub Private Sub Form_MouseDown(Button As Integer,Shift As Integer,X As Single,Y As Single)ReleaseCapture SendMessage hwnd,WM_NCLBUT
20、TONDOWN,HTCAPTION,0&End Sub Module1.bas 移动模块的程序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 Public Const HTCAPTION=2 Public Const WM_NCLBUTTONDOWN=&HA1-名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 6 页 -