VB初学者实例50例(共62页)_第1页
VB初学者实例50例(共62页)_第2页
VB初学者实例50例(共62页)_第3页
VB初学者实例50例(共62页)_第4页
VB初学者实例50例(共62页)_第5页
已阅读5页,还剩60页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 第四代计算机语言,VB的特点是显而易见的。因为是“可视的”所以我们不妨先看看VB的界面(我用的是VB4.0 32位版),很容易可以看到,屏幕上有五个窗口,(1)是菜单栏;(2)是工具箱(Tools Box),里面是VB所需的控件;(3)是VB程序要设计的窗体(Form);(4)控件及窗体的属性(Properties),在VB中每一个控件都有其自己的属性,比如文本框有Text属性,而任何一个控件都有一个自己唯一的名字,这使得我们无需编程就可以完成部分编程的工作;(5)是VB的文件控制窗口,VB的每一个程序称为一个项目(Project),一个项目文件里往往包括以下两种文件,窗体(Form)文件和

2、模块(Bas)文件,当然还有Class文件,这些文件组成一个完整的程序,而项目文件(Vbp)则用来描述程序所包含的内容。实际上,我一直认为学习的最好方法就是实际操作一下,好,下面我们就来看一个最简单的程序,和所有的编程教科书一样,我们把第一个程序称为“Hello,world!”。首先,我们先来熟悉一下窗体。如果你用过Win95附件所带的画图板的话,那么你一定会很快熟悉VB的窗体设计方法,用鼠标单击一下工具箱中的任何一个控件,然后在窗体上任意拖动鼠标,你就会得到一个所需的控件;当然还有其他的方法得到控件,你可以在工具箱上双击得到一个控件;或是按住Ctrl键,再单击所需控件,重复第一次的步骤,你会

3、发现,不用再回去点击工具箱了,点右键可以恢复鼠标指针,这样对于重复画同一个控件尤其有用。对于我们的第一个小程序,知道这些就已经足够了。现在你可以在我们的窗体上画如下控件一个图片框(Picture Box),用来显示图画,每一个控件建立的时候,系统都会给它设一个默认的名字(Name),这个PictureBox的名字就叫做Picture1,为了方便起见,我们以后的控件都使用缺省的名称。选定Picture1,在左边的属性窗口中找到Picture属性,单击会出现,点击为Picture1选一个图像文件,VB支持的图像文件有BitMap、WMF、ICO三种,微软为用户提供了一套图标文件,存放在VB目录的I

4、cons目录下,选择Iconsmiscface01.ico文件,这样图标就被装入了Picture1。再选择Pictrue1的Autosize属性,设为1-True,则会发现Picture1以变成和图像一样大了;把BorderStyle定为0-None,去掉图片边框。用同样的方法再添加两个图片框:Picture2(Picture:face02.ico),Picture3(Picture:face03.ico)。图片框完成了,第二步,建立三个无线钮(Option Button),由于它象收音机的无线电按钮而得名),把它们的标签名属性(Caption)分别定为“悲哀”,“高兴”,“没治了”,放置成下

5、图的样子。最后我们还需要三个文本框(Text Box),分别把它们的Text属性设为”(空字符串),程序的最后界面如图1_3,这样按下菜单窗口的运行按钮,运行一下程序,还可以吧,不过现在程序还无法工作,就算是VB也不能免俗,还必须输入代码才能使程序运作。VB是一门典型的面向对象编程的语言,它的整个程序都是由事件驱动的。那么,什么是事件呢?其实,我们的生活中充满了各式各样的事件,比如说“闯红灯”这个事件的发生会导致被警察追杀、走路撞到电线杆时会痛。上面的例子虽然有些滑稽,但它可以清楚的解释事件的含义,就象你马上就能体会到的在什么地方单击鼠标时会发生什么,按下键盘时会发生什么,这就是事件,而事件就

6、是这么容易。要进入VB的代码设计区,只需在需要的控件或窗体上双击鼠标即可得到这样的窗口。如图1_4,每一个VB程序都由若干小程序组成,这使得用VB编程更加容易,这些小程序,分为“过程”(Procedure)和“函数”(Function),二者都可以传递参数,所不同的是函数有一个返回值,而过程没有,仅此而已。双击窗体的任何一个部分,将出现一个空的过程:“Form_Load()”,这是一个非常重要的过程,它所驱动的事件就是该窗体被加载的事件,打个比方,就象计算机启动时的批处理程序(Autoexec.bat)。 这个程序里我们要对控件进行一些设置。我们刚才也曾对控件进行设置,但那是在程序设置中进行的

7、,有时我们也需要在程序中改变某些控件的属性,通过程序代码。VB中规定,一个控件属性的访问方法:名称.属性,于是我们对本程序的控件进行如下设置:Private Sub Form_Load()Picture1.Visible = FalsePicture2.Visible = FalsePicture3.Visible = FalseText1.Visible = FalseText2.Visible = FalseText3.Visible = FalseEnd SubPrivate表示此过程是局部的,Sub表示子过程的开始,与之相对的是End Sub表示过程的结束,Visible属性表示控件的

8、可见性,这个属性的值有两个True和False。注意:VB里对大小写是不加区分的,而且,已定义的名称,以后再用到时,系统会自动把它变成相应的大写或小写。小知识:1.VB里的对变量及常量的规矩。变量一般为Abc的格式,常量为ABC,另外这些变量和常量名不得同VB内的关键字相同,也不可以使用一些特殊符号,这些我们将在以后介绍,注意在学习中养成好习惯,使程序便于自己和别人的浏览。2.程序书写的窍门:不要真的一句一句把程序打完,利用快捷键:拷贝Ctrl+C,剪切Ctrl+T,粘贴Ctrl+P。按下F5,运行一下程序,现在发现只剩下无线钮了。如图。Private Sub Form_Load()Pictu

9、re1.Visible = FalsePicture2.Visible = FalsePicture3.Visible = FalseText1.Visible = FalseText2.Visible = FalseText3.Visible = FalseOption4.Left = -2000Option4.Value = TrueEnd SubLeft属性表示控件离窗体左边框的距离,设置为2000,则在窗体内不会看到这个控件,并使之Value=True。再运行程序,一切如愿,如图.注意:为什么不用Option4.Visible=False?因为,一旦设置无线钮为不可见,那么再使它的Va

10、lue为真是没有意义的,不信自己试试。下面我们可以继续进行了。在刚才的代码窗口里,将Object选择为Option1,Proc选择为Click,即得到一个Option1_Click的过程。代码如下:Private Sub Option1_Click()Picture1.Visible = TruePicture2.Visible = FalsePicture3.Visible = FalseText1.Visible = TrueText2.Visible = FalseText3.Visible = FalseText1.Text = 再见,残酷的世界。End Sub相同的建立Option2

11、_Click和Option2_Click:Private Sub Option2_Click()Picture2.Visible = TruePicture1.Visible = FalsePicture3.Visible = FalseText2.Visible = TrueText1.Visible = FalseText3.Visible = FalseText2.Text = 今天心情不错。End SubPrivate Sub Option3_Click()Picture3.Visible = TruePicture2.Visible = FalsePicture1.Visible =

12、 FalseText3.Visible = TrueText2.Visible = FalseText1.Visible = FalseText3.Text = Hello,World。End Sub好,我们的开山之作完成了!按下F5,玩一下吧。这个程序虽然简单,但也用到了许多基本知识,你开始感兴趣了吗?_1.打印三角形Private Sub Form_Click() For i = 1 To 4 Print Space(4 - i); For j = 1 To i Print * ; Next j Print Next iEnd Sub2.题目: (事件)单击窗体。(响应)我国现有人口为 1

13、3 亿, 设年增长率为 1%, 计算多少年后增加到 20 亿。 并将结果输出在窗体上。 将结果存入变量 year 中。使用 do while .loop 语句完成程序。Private Sub Form_Click() Dim year As Integer, sum As Single Dim p as double Dim r as single P=1300000000 r=0.01 Do while p 3000 Then Exit ForEnd IfNext iPrint sumEnd Sub4 .题目(事件) 点击窗体 (响应) 求 1! +2! +3! +10!的值, 并在窗体上输

14、出, 结果存入变量 L 中 Private Sub form_click()Dim l As LongDim p As Longp = 1For i = 1 To 10p = p * il = l + pNextPrint lEnd Sub 5. 题目点击窗体 求 100 以内偶数的和, 并将结果输出在窗体上, 将结果存入变量 sum 中Private sub form _click() Dim sum as integer for i=0 to 100 IF i mod 2=0 then Sum=sum+i end if Next Print sum End sub 6.题目:单击窗体 求

15、1+2+3+4+5+100 的值并将结果输入在窗体上,结果存入变量 sum 中 Private sub form click( ) Dim sum as integer For i=1 to 100 Sum=sum+i Next Print sum End sub 7.题目单击窗体 求 1+5+10+15+.+500 的和,并将结果输出在窗体上,将结果存入变量 sum 中 Private sub form-click() Dim sum as long Sum=1For i=0 to 500 IF i Mod 5 =0 thenSum=sum+i End IfNext Print sum En

16、d sub #8.题目:求两个正整数 m,n 的最大公约数 p 和最小公倍数 q 注意: 请在指定的事件内编写代码。 Dim m As Integer,n As IntegerPrivate Sub Command1_Click()m = Val(InputBox(输入正整数)n = Val(InputBox(输入正整数)If n * m = 0 ThenMsgBox 两数任何一个数都不能为0!End IfEnd SubPrivate Sub Command2_Click()Dim r As Integer,s As Integerr = m:s = 1Do Until m Mod r = 0

17、 And n Mod r = 0 最大公约数r = r - 1LoopDo Until s Mod m = 0 And s Mod n = 0 最小公倍数s = s + 1LoopPrint 最大公约数是: & r & 最小公倍数是: & sEnd Sub9.题目: (事件)单击窗体。(响应)求 1020 之间所有素数的乘积并输出在窗体上。将结果存入变量 L 中Dim l As Long l=1 For i=10 To 20 For j=2 To i-1 If i Mod j = 0 Then Exit For End If Next j If j = i Then l=l*i End If

18、Next i Print l 10.题目: 编写函数 fun 其功能是:判断一个整数 w 的各位数字平方之和能否被 5 整除 , 可以被 5 整除则返回1,否则返回0Private Function fun(w As Integer) As Boolean Dim k as integer ,s as integer Do S=s+(w mod 10)*(w mod 10) W=Int(w/10) Loop while w0 If s mod 5=0 then K=1 Else K=0 End if Fun=k End Function 11.题目: 编写函数 fun,函数的功能是:根据一个百

19、分制成绩mark(整数),显示对应五级制的评定。条件如下:mark大于等于90显示优秀 mark 小于 90 且大于等于80 显示良好 mark小于80且大于等于70显示中等 mark小于70且大于等于60显示及格 mark小于60显示不及格 要求使用 IF 语句来实现。注意:请在指定的事件内编写代码代码只能写在两行标识之间不得修改已有代码 保存时不得修改窗体和工程文件的名称及路径, 否则后果自负。 Private Function fun(mark As Integer) As String if mark=90 then fun=优秀Else if mark=80 then fun=良好

20、else if mark=70 then fun =中等 else if mark=60 then fun=及格 else fun=不及格 end if End Function 12.题目:(事件)双击窗体。(响应)求出100200之间,能被5整除,但不能被3整除的数。并求所有数之和,存入变量中SUM中。Private Sub Form_dblClick() Dim sum As Integer sum = 0 Dim i as integer For i =100 to 200 If (i mod 5)=0) and (i mod 3)0) then Print (CStr(i) sum=

21、sum+i End if Next Print sum Testfunc (sum) End sub 1. 如何消除textbox中按下回车时的beep声音?Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0 End IfEnd Sub2.Textbox获得焦点时自动选中。Private Sub Text1_GotFocus() Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text)End Sub3.屏蔽textbox控件自身的右键

22、菜单,并显示自己的菜单。方法一:Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _As Single) If Button = 2 Then Text1.Enabled = False Text1.Enabled = True PopupMenu mymenu End IfEnd Sub方法二:回调函数module:Option ExplicitPublic OldWindowProc As Long 保存默认的窗口函数的地址Public Const WM_CONTEXTMENU =

23、 &H7B 当右击文本框时,产生这条消息Public Declare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal hWnd _As Long, ByVal nIndex As Long) As LongPublic Declare Function SetWindowLong Lib user32 Alias SetWindowLongA (ByVal hWnd _As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPrivate Decla

24、re Function CallWindowProc Lib user32 Alias CallWindowProcA (ByVal _ lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPublic Function SubClass_WndMessage(ByVal hWnd As Long, ByVal Msg As Long, ByVal wp _As Long, ByVal lp As Long) As Lon

25、g如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理If Msg WM_CONTEXTMENU Then SubClass_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp) Exit FunctionEnd IfSubClass_WndMessage = TrueEnd Function窗体中:Private Const GWL_WNDPROC = (-4)Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Singl

26、e, Y _As Single)If Button = 1 Then Exit Sub OldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC) 取得窗口函数的地址 用SubClass_WndMessage代替窗口函数处理消息 Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf SubClass_WndMessage)End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y A

27、s Single) If Button = 1 Then Exit Sub 恢复窗口的默认函数 Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWindowProc) PopupMenu mymenuEnd Sub4. 设置TEXTBOX为只读属性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

28、Private Declare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal hwnd _ As Long, ByVal nIndex As Long) As LongPrivate Const GWL_STYLE = (-16)Private Const EM_SETREADONLY = &HCFPrivate Sub Command1_Click() Dim l As Long If (GetWindowLong(Text1.hwnd, GWL_STYLE) And &H800) Then Text1.Text

29、= This is a read/write text box. 文本窗口是只读窗口,设置为可读写窗口 l = SendMessage(Text1.hwnd, EM_SETREADONLY, False, vbNull) Text1.BackColor = RGB(255, 255, 255) 将背景设置为白色 Command1.Caption = Read&WriteElse Text1.Text = This is a readonly text box. 文本窗口是可读写窗口,设置为只读窗口 l = SendMessage(Text1.hwnd, EM_SETREADONLY, True

30、, vbNull) Text1.BackColor = vbInactiveBorder 将背景设置为灰色 Command1.Caption = &ReadOnly End IfEnd Sub5. 利用API函数MessageBox代替MSGBOX函数可以使得Timer控件正常工作Private Declare Function MessageBox Lib user32 Alias MessageBoxA (ByVal hwnd As _ Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long)

31、 As LongPrivate Sub Command1_Click() MsgBox 时钟变的无效了End SubPrivate Sub Command2_Click() MessageBox Me.hwnd, 时钟正常运行, hehe, 0End SubPrivate Sub Timer1_Timer() Static i As Integer i = i + 1 Text1.Text = iEnd SubPrivate Declare Function SetWindowPos Lib user32 (ByVal hwnd As Long, ByVal _ hWndInsertAfter

32、 As Long, ByVal X As Long, ByVal Y As Long, ByVal Cx As Long, ByVal Cy _As Long, ByVal wFlags As Long) As LongPublic Sub SetOnTop(ByVal IsOnTop As Integer)Dim rtn As Long If IsOnTop = 1 Then rtn = SetWindowPos(Form1.hwnd, -1, 0, 0, 0, 0, 3) Else rtn = SetWindowPos(Form1.hwnd, -2, 0, 0, 0, 0, 3) End

33、IfEnd SubPrivate Sub Command1_Click() SetOnTop 1 将窗口置于最上面End SubPrivate Sub Command2_Click() SetOnTop 0End Sub7.只容许运行一个程序实例(利用互斥体)选择启动对象为sub main()module:Public Declare Function CreateMutex Lib kernel32 Alias CreateMutexA _ (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVa

34、l lpName _As String) As LongPublic Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As LongEnd TypePublic Const ERROR_ALREADY_EXISTS = 183&Private Sub Main() Dim sa As SECURITY_ATTRIBUTES sa.bInheritHandle = 1 sa.lpSecurityDescriptor = 0 sa.nLength = Len(sa) Debug

35、.Print CreateMutex(sa, 1, App.Title) 这一行可千万不能删除啊 Debug.Print Err.LastDllError If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then MsgBox More than one instance Else Form1.Show End IfEnd Sub8.窗体标题栏闪烁Option ExplicitPrivate Declare Function FlashWindow Lib user32 (ByVal hwnd As Long, ByVal bInvert _As Lo

36、ng) As LongPrivate Sub tmrFlash_Timer() Static mFlash As Boolean FlashWindow hwnd, Not mFlashEnd Sub9. 拷屏方法一:利用模拟键盘Private Declare Sub keybd_event Lib user32 (ByVal bVk As Byte, ByVal bScan As Byte, _ ByVal dwFlags As Long, ByVal dwExtraInfo As Long)Const theScreen = 1Const theForm = 0Private Sub Co

37、mmand1_Click()Call keybd_event(vbKeySnapshot, theForm, 0, 0) 若theForm改成theScreen则Copy整个ScreenDoEventsPicture1.Picture = Clipboard.GetData(vbCFBitmap)End Sub10. 为程序注册热键方法一:修改注册表Private Declare Function RegisterHotKey Lib user32 (ByVal hWnd As Long, ByVal id _As Long, ByVal fsModifiers As Long, ByVal

38、vk As Long) As LongPrivate Declare Function UnregisterHotKey Lib user32 (ByVal hWnd As Long, ByVal id _As Long) As LongPrivate Declare Function PeekMessage Lib user32 Alias PeekMessageA (lpMsg As Msg, _ ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal _ wRemoveMsg

39、As Long) As LongPrivate Declare Function WaitMessage Lib user32 () As LongPrivate Type POINTAPI x As Long y As LongEnd TypePrivate Type Msg hWnd As Long Message As Long wParam As Long lParam As Long time As Long pt As POINTAPIEnd Type 声明常数Private Const MOD_ALT = &H1Private Const MOD_CONTROL = &H2Pri

40、vate Const MOD_SHIFT = &H4Private Const PM_REMOVE = &H1Private Const WM_HOTKEY = &H312Private HotKey_Fg As BooleanPrivate Sub Form_Load() Dim Message As Msg 注册 Ctrl+Y 为热键 RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyY RegisterHotKey Me.hWnd, &HBFF2&, MOD_CONTROL, vbKeyU Me.Show Form1.Hide 等待处理

41、消息 HotKey_Fg = False Do While Not HotKey_Fg 等待消息 WaitMessage 检查是否热键被按下 If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then Form1.Show 1 End If 转让控制权,允许操作系统处理其他事件 DoEvents LoopEnd SubPrivate Sub Form_Unload(Cancel As Integer) HotKey_Fg = True 撤销热键的注册 Call UnregisterHotKey(Me.hWnd,

42、&HBFFF&)End Sub方法二:SendMessagePrivate Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal hwnd As _ Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Const WM_SETHOTKEY = &H32Private Const HOTKEYF_SHIFT = &H1Private Const HOTKEYF_ALT = &H4Private Sub Form_Lo

43、ad() Dim l As Long Dim wHotkey As Long wHotkey = (HOTKEYF_ALT) * (2 8) + 65 定义ALT+A为热键 l = SendMessage(Me.hwnd, WM_SETHOTKEY, wHotkey, 0)End Sub11.在状态栏显示无边框窗体图标。Private Declare Function SetWindowLong Lib user32 Alias SetWindowLongA (ByVal hWnd _ As Long, ByVal nIndex As Long, ByVal dwNewLong As Long

44、) As LongPrivate Declare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal hWnd _ As Long, ByVal nIndex As Long) As LongConst GWL_STYLE = (-16&)Const WS_SYSMENU = &H80000Private Sub Form_Load()Make Forms Icon visible in the taskbarSetWindowLong Me.hWnd, GWL_STYLE, GetWindowLong(Me.hWnd,

45、GWL_STYLE) Or WS_SYSMENUEnd Sub12. 记录窗体的大小及位置和程序中的一些设置Private Sub Form_Load() Me.Width = GetSetting(App.Title, Me.Name, Width, 7200) Me.Height = GetSetting(App.Title, Me.Name, Height, 6300) Me.Top = GetSetting(App.Title, Me.Name, Top, 100) Me.Left = GetSetting(App.Title, Me.Name, Left, 100) Check1.V

46、alue = GetSetting(App.Title, Me.Name, check1, 0)End SubPrivate Sub Form_Unload(Cancel As Integer) Call SaveSetting(App.Title, Me.Name, Width, Me.Width) Call SaveSetting(App.Title, Me.Name, Height, Me.Height) Call SaveSetting(App.Title, Me.Name, Top, Me.Top) Call SaveSetting(App.Title, Me.Name, Left,

47、 Me.Left) Call SaveSetting(App.Title, Me.Name, check1, Check1.Value)End Sub13. 解决mschart控件数据更改时的闪动现象1、在有MSChart控件的窗体中另外加入一个PictureBox控件,如MSChart1和Picture1。2、使Picture1和MSChart1大小一致,位置相同(通过左对齐和顶端对齐)。3、使Picture1在MSChart1前端,设置Picture1的Visible为False,即不可见。只有刷新数据时Picture1才显示。刷新数据过程Private Sub Refresh()Dim

48、V_newchar() n维数组Picture1.Visible = TrueMSChart1.ChartData = V_newchar 给MSChart1重新赋值,即刷新数据MSChart1.EditCopy 将当前图表的图片复制到剪贴板中Picture1.Picture = Clipboard.GetData() 给Picture1赋值剪贴板中的图片End Sub这样每一次刷新数据时Picture1显示的图片都不会产生闪烁现象14. 无边框窗体的右键菜单设计无边框窗体时,如果使用菜单编辑器,就会自动改变成有边框的窗体,此时,可以在另外一个窗体中(一般情况下你的程序应该不止一个窗体的吧,如

49、果真的只有一个,可以利用其他人写的类,添加右键)编辑菜单(VISIBLE属性设为FALSE),然后在本窗体中调用。调用形式如下:Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 ThenPopupMenu Form2.mymenuEnd IfEnd Sub15.创建圆角无边框窗体Private Declare Function CreateRoundRectRgn Lib gdi32 (ByVal X1 As Integer, ByVal

50、Y1 _ As Integer, ByVal X2 As Integer, ByVal Y2 As Integer, ByVal x3 As Integer, ByVal y3 As _ Integer) As LongPrivate Declare Function DeleteObject Lib gdi32 (ByVal hObject As Long) As LongPrivate Declare Function SetWindowRgn Lib user32 (ByVal hwnd As Long, ByVal hrgn As _ Long, ByVal bRedraw As Bo

51、olean) As LongPrivate Sub Form_Load() hround = CreateRoundRectRgn(0, 0, ScaleX(Form1.ScaleWidth, vbTwips, vbPixels), _ ScaleY(Form1.ScaleHeight, vbTwips, vbPixels), 20, 20)SetWindowRgn Me.hwnd, hround, TrueDeleteObject hroundEnd Sub16.拖动没有标题栏的窗体方法一:Private Declare Function ReleaseCapture Lib user32

52、() As LongPrivate Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal hwnd As _ Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Const HTCAPTION = 2Private Const WM_NCLBUTTONDOWN = &HA1Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Sin

53、gle, Y As Single) Dim ncl As Long Dim rel As Long If Button = 1 Then i = ReleaseCapture() ncl = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0) End IfEnd Sub方法二:回调函数module:Public Const GWL_WNDPROC = (-4)Public Const WM_NCHITTEST = &H84Public Const HTCLIENT = 1Public Const HTCAPTION = 2Declare Function CallWindowProc Lib user32 Alias CallWindowProcA (ByVal

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论