版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) 第四代計(jì)算機(jī)語言,VB的特點(diǎn)是顯而易見的。因?yàn)槭恰翱梢暤摹彼晕覀儾环料瓤纯碫B的界面(我用的是VB4.0 32位版),很容易可以看到,屏幕上有五個窗口,(1)是菜單欄;(2)是工具箱(Tools Box),里面是VB所需的控件;(3)是VB程序要設(shè)計(jì)的窗體(Form);(4)控件及窗體的屬性(Properties),在VB中每一個控件都有其自己的屬性,比如文本框有Text屬性,而任何一個控件都有一個自己唯一的名字,這使得我們無需編程就可以完成部分編程的工作;(5)是
2、VB的文件控制窗口,VB的每一個程序稱為一個項(xiàng)目(Project),一個項(xiàng)目文件里往往包括以下兩種文件,窗體(Form)文件和模塊(Bas)文件,當(dāng)然還有Class文件,這些文件組成一個完整的程序,而項(xiàng)目文件(Vbp)則用來描述程序所包含的內(nèi)容。實(shí)際上,我一直認(rèn)為學(xué)習(xí)的最好方法就是實(shí)際操作一下,好,下面我們就來看一個最簡單的程序,和所有的編程教科書一樣,我們把第一個程序稱為“Hello,world!”。首先,我們先來熟悉一下窗體。如果你用過Win95附件所帶的畫圖板的話,那么你一定會很快熟悉VB的窗體設(shè)計(jì)方法,用鼠標(biāo)單擊一下工具箱中的任何一個控件,然后在窗體上任意拖動鼠標(biāo),你就會得到一個所需的
3、控件;當(dāng)然還有其他的方法得到控件,你可以在工具箱上雙擊得到一個控件;或是按住Ctrl鍵,再單擊所需控件,重復(fù)第一次的步驟,你會發(fā)現(xiàn),不用再回去點(diǎn)擊工具箱了,點(diǎn)右鍵可以恢復(fù)鼠標(biāo)指針,這樣對于重復(fù)畫同一個控件尤其有用。對于我們的第一個小程序,知道這些就已經(jīng)足夠了?,F(xiàn)在你可以在我們的窗體上畫如下控件一個圖片框(Picture Box),用來顯示圖畫,每一個控件建立的時候,系統(tǒng)都會給它設(shè)一個默認(rèn)的名字(Name),這個PictureBox的名字就叫做Picture1,為了方便起見,我們以后的控件都使用缺省的名稱。選定Picture1,在左邊的屬性窗口中找到Picture屬性,單擊會出現(xiàn),點(diǎn)擊為Pict
4、ure1選一個圖像文件,VB支持的圖像文件有BitMap、WMF、ICO三種,微軟為用戶提供了一套圖標(biāo)文件,存放在VB目錄的Icons目錄下,選擇Iconsmiscface01.ico文件,這樣圖標(biāo)就被裝入了Picture1。再選擇Pictrue1的Autosize屬性,設(shè)為1-True,則會發(fā)現(xiàn)Picture1以變成和圖像一樣大了;把BorderStyle定為0-None,去掉圖片邊框。用同樣的方法再添加兩個圖片框:Picture2(Picture:face02.ico),Picture3(Picture:face03.ico)。圖片框完成了,第二步,建立三個無線鈕(Option Butto
5、n),由于它象收音機(jī)的無線電按鈕而得名),把它們的標(biāo)簽名屬性(Caption)分別定為“悲哀”,“高興”,“沒治了”,放置成下圖的樣子。最后我們還需要三個文本框(Text Box),分別把它們的Text屬性設(shè)為”(空字符串),程序的最后界面如圖1_3,這樣按下菜單窗口的運(yùn)行按鈕,運(yùn)行一下程序,還可以吧,不過現(xiàn)在程序還無法工作,就算是VB也不能免俗,還必須輸入代碼才能使程序運(yùn)作。VB是一門典型的面向?qū)ο缶幊痰恼Z言,它的整個程序都是由事件驅(qū)動的。那么,什么是事件呢?其實(shí),我們的生活中充滿了各式各樣的事件,比如說“闖紅燈”這個事件的發(fā)生會導(dǎo)致被警察追殺、走路撞到電線桿時會痛。上面的例子雖然有些滑稽,
6、但它可以清楚的解釋事件的含義,就象你馬上就能體會到的在什么地方單擊鼠標(biāo)時會發(fā)生什么,按下鍵盤時會發(fā)生什么,這就是事件,而事件就是這么容易。要進(jìn)入VB的代碼設(shè)計(jì)區(qū),只需在需要的控件或窗體上雙擊鼠標(biāo)即可得到這樣的窗口。如圖1_4,每一個VB程序都由若干小程序組成,這使得用VB編程更加容易,這些小程序,分為“過程”(Procedure)和“函數(shù)”(Function),二者都可以傳遞參數(shù),所不同的是函數(shù)有一個返回值,而過程沒有,僅此而已。雙擊窗體的任何一個部分,將出現(xiàn)一個空的過程:“Form_Load()”,這是一個非常重要的過程,它所驅(qū)動的事件就是該窗體被加載的事件,打個比方,就象計(jì)算機(jī)啟動時的批處
7、理程序(Autoexec.bat)。 這個程序里我們要對控件進(jìn)行一些設(shè)置。我們剛才也曾對控件進(jìn)行設(shè)置,但那是在程序設(shè)置中進(jìn)行的,有時我們也需要在程序中改變某些控件的屬性,通過程序代碼。VB中規(guī)定,一個控件屬性的訪問方法:名稱.屬性,于是我們對本程序的控件進(jìn)行如下設(shè)置:Private Sub Form_Load()Picture1.Visible = FalsePicture2.Visible = FalsePicture3.Visible = FalseText1.Visible = FalseText2.Visible = FalseText3.Visible = FalseEnd SubP
8、rivate表示此過程是局部的,Sub表示子過程的開始,與之相對的是End Sub表示過程的結(jié)束,Visible屬性表示控件的可見性,這個屬性的值有兩個True和False。注意:VB里對大小寫是不加區(qū)分的,而且,已定義的名稱,以后再用到時,系統(tǒng)會自動把它變成相應(yīng)的大寫或小寫。小知識:1.VB里的對變量及常量的規(guī)矩。變量一般為Abc的格式,常量為ABC,另外這些變量和常量名不得同VB內(nèi)的關(guān)鍵字相同,也不可以使用一些特殊符號,這些我們將在以后介紹,注意在學(xué)習(xí)中養(yǎng)成好習(xí)慣,使程序便于自己和別人的瀏覽。2.程序書寫的竅門:不要真的一句一句把程序打完,利用快捷鍵:拷貝Ctrl+C,剪切Ctrl+T,粘
9、貼Ctrl+P。按下F5,運(yùn)行一下程序,現(xiàn)在發(fā)現(xiàn)只剩下無線鈕了。如圖。Private Sub Form_Load()Picture1.Visible = FalsePicture2.Visible = FalsePicture3.Visible = FalseText1.Visible = FalseText2.Visible = FalseText3.Visible = FalseOption4.Left = -2000Option4.Value = TrueEnd SubLeft屬性表示控件離窗體左邊框的距離,設(shè)置為2000,則在窗體內(nèi)不會看到這個控件,并使之Value=True。再運(yùn)行程
10、序,一切如愿,如圖.注意:為什么不用Option4.Visible=False?因?yàn)椋坏┰O(shè)置無線鈕為不可見,那么再使它的Value為真是沒有意義的,不信自己試試。下面我們可以繼續(xù)進(jìn)行了。在剛才的代碼窗口里,將Object選擇為Option1,Proc選擇為Click,即得到一個Option1_Click的過程。代碼如下:Private Sub Option1_Click()Picture1.Visible = TruePicture2.Visible = FalsePicture3.Visible = FalseText1.Visible = TrueText2.Visible = Fals
11、eText3.Visible = FalseText1.Text = 再見,殘酷的世界。End Sub相同的建立Option2_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()Pi
12、cture3.Visible = TruePicture2.Visible = FalsePicture1.Visible = 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 P
13、rint * ; Next j Print Next iEnd Sub2.題目: (事件)單擊窗體。(響應(yīng))我國現(xiàn)有人口為 13 億, 設(shè)年增長率為 1%, 計(jì)算多少年后增加到 20 億。 并將結(jié)果輸出在窗體上。 將結(jié)果存入變量 year 中。使用 do while .loop 語句完成程序。Private Sub Form_Click() Dim year As Integer, sum As Single Dim p as double Dim r as single P= r=0.01 Do while p 3000 Then Exit ForEnd IfNext iPrint sumE
14、nd Sub4 .題目(事件) 點(diǎn)擊窗體 (響應(yīng)) 求 1! +2! +3! +10!的值, 并在窗體上輸出, 結(jié)果存入變量 L 中 Private Sub form_click()Dim l As LongDim p As Longp = 1For i = 1 To 10p = p * il = l + pNextPrint lEnd Sub 5. 題目點(diǎn)擊窗體 求 100 以內(nèi)偶數(shù)的和, 并將結(jié)果輸出在窗體上, 將結(jié)果存入變量 sum 中Private sub form _click() Dim sum as integer for i=0 to 100 IF i mod 2=0 then
15、 Sum=sum+i end if Next Print sum End sub 6.題目:單擊窗體 求 1+2+3+4+5+100 的值并將結(jié)果輸入在窗體上,結(jié)果存入變量 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 的和,并將結(jié)果輸出在窗體上,將結(jié)果存入變量 sum 中 Private sub form-click() Dim sum as long Sum=1For i=0 to 50
16、0 IF i Mod 5 =0 thenSum=sum+i End IfNext Print sum End sub #8.題目:求兩個正整數(shù) m,n 的最大公約數(shù) p 和最小公倍數(shù) q 注意: 請?jiān)谥付ǖ氖录?nèi)編寫代碼。 Dim m As Integer,n As IntegerPrivate Sub Command1_Click()m = Val(InputBox(輸入正整數(shù))n = Val(InputBox(輸入正整數(shù))If n * m = 0 ThenMsgBox 兩數(shù)任何一個數(shù)都不能為0!End IfEnd SubPrivate Sub Command2_Click()Dim r A
17、s Integer,s As Integerr = m:s = 1Do Until m Mod r = 0 And n Mod r = 0 最大公約數(shù)r = r - 1LoopDo Until s Mod m = 0 And s Mod n = 0 最小公倍數(shù)s = s + 1LoopPrint 最大公約數(shù)是: & r & 最小公倍數(shù)是: & sEnd Sub9.題目: (事件)單擊窗體。(響應(yīng))求 1020 之間所有素?cái)?shù)的乘積并輸出在窗體上。將結(jié)果存入變量 L 中Dim l As Long l=1 For i=10 To 20 For j=2 To i-1 If i Mod j = 0 Th
18、en Exit For End If Next j If j = i Then l=l*i End If Next i Print l 10.題目: 編寫函數(shù) fun 其功能是:判斷一個整數(shù) w 的各位數(shù)字平方之和能否被 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 E
19、nd if Fun=k End Function 11.題目: 編寫函數(shù) fun,函數(shù)的功能是:根據(jù)一個百分制成績mark(整數(shù)),顯示對應(yīng)五級制的評定。條件如下:mark大于等于90顯示優(yōu)秀 mark 小于 90 且大于等于80 顯示良好 mark小于80且大于等于70顯示中等 mark小于70且大于等于60顯示及格 mark小于60顯示不及格 要求使用 IF 語句來實(shí)現(xiàn)。注意:請?jiān)谥付ǖ氖录?nèi)編寫代碼代碼只能寫在兩行標(biāo)識之間不得修改已有代碼 保存時不得修改窗體和工程文件的名稱及路徑, 否則后果自負(fù)。 Private Function fun(mark As Integer) As Stri
20、ng if mark=90 then fun=優(yōu)秀Else if mark=80 then fun=良好 else if mark=70 then fun =中等 else if mark=60 then fun=及格 else fun=不及格 end if End Function 12.題目:(事件)雙擊窗體。(響應(yīng))求出100200之間,能被5整除,但不能被3整除的數(shù)。并求所有數(shù)之和,存入變量中SUM中。Private Sub Form_dblClick() Dim sum As Integer sum = 0 Dim i as integer For i =100 to 200 If
21、(i mod 5)=0) and (i mod 3)0) then Print (CStr(i) sum=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獲得焦點(diǎn)時自動選中。Private Sub Text1_GotFocus() Text1.SelStart = 0 Text1
22、.SelLength = Len(Text1.Text)End Sub3.屏蔽textbox控件自身的右鍵菜單,并顯示自己的菜單。方法一: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方法二:回調(diào)函數(shù)module:Option ExplicitPublic OldWindowP
23、roc As Long 保存默認(rèn)的窗口函數(shù)的地址Public Const WM_CONTEXTMENU = &H7B 當(dāng)右擊文本框時,產(chǎn)生這條消息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 A
24、s Long, ByVal dwNewLong As Long) As LongPrivate Declare 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
25、 As Long, ByVal wp _As Long, ByVal lp As Long) As Long如果消息不是WM_CONTEXTMENU,就調(diào)用默認(rèn)的窗口函數(shù)處理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_Mous
26、eDown(Button As Integer, Shift As Integer, X As Single, Y _As Single)If Button = 1 Then Exit Sub OldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC) 取得窗口函數(shù)的地址 用SubClass_WndMessage代替窗口函數(shù)處理消息 Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf SubClass_WndMessage)End SubPrivate Sub Text1_MouseUp(
27、Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Exit Sub 恢復(fù)窗口的默認(rèn)函數(shù) Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWindowProc) PopupMenu mymenuEnd Sub4. 設(shè)置TEXTBOX為只讀屬性Private Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal hwnd _As Long, ByVal wMsg
28、As Long, ByVal wParam As Long, lParam As Any) As LongPrivate 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 (GetWindowLo
29、ng(Text1.hwnd, GWL_STYLE) And &H800) Then Text1.Text = This is a read/write text box. 文本窗口是只讀窗口,設(shè)置為可讀寫窗口 l = SendMessage(Text1.hwnd, EM_SETREADONLY, False, vbNull) Text1.BackColor = RGB(255, 255, 255) 將背景設(shè)置為白色 Command1.Caption = Read&WriteElse Text1.Text = This is a readonly text box. 文本窗口是可讀寫窗口,設(shè)置為
30、只讀窗口 l = SendMessage(Text1.hwnd, EM_SETREADONLY, True, vbNull) Text1.BackColor = vbInactiveBorder 將背景設(shè)置為灰色 Command1.Caption = &ReadOnly End IfEnd Sub5. 利用API函數(shù)MessageBox代替MSGBOX函數(shù)可以使得Timer控件正常工作Private Declare Function MessageBox Lib user32 Alias MessageBoxA (ByVal hwnd As _ Long, ByVal lpText As St
31、ring, ByVal lpCaption As String, ByVal wType As Long) As LongPrivate Sub Command1_Click() MsgBox 時鐘變的無效了End SubPrivate Sub Command2_Click() MessageBox Me.hwnd, 時鐘正常運(yùn)行, hehe, 0End SubPrivate Sub Timer1_Timer() Static i As Integer i = i + 1 Text1.Text = iEnd SubPrivate Declare Function SetWindowPos Li
32、b user32 (ByVal hwnd As Long, ByVal _ hWndInsertAfter 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 r
33、tn = SetWindowPos(Form1.hwnd, -2, 0, 0, 0, 0, 3) End IfEnd SubPrivate Sub Command1_Click() SetOnTop 1 將窗口置于最上面End SubPrivate Sub Command2_Click() SetOnTop 0End Sub7.只容許運(yùn)行一個程序?qū)嵗ɡ没コ怏w)選擇啟動對象為sub main()module:Public Declare Function CreateMutex Lib kernel32 Alias CreateMutexA _ (lpMutexAttributes As S
34、ECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal 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 s
35、a.lpSecurityDescriptor = 0 sa.nLength = Len(sa) Debug.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.窗體標(biāo)題欄閃爍Option ExplicitPrivate Declare Function FlashWindow
36、 Lib user32 (ByVal hwnd As Long, ByVal bInvert _As Long) 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 Lo
37、ng)Const theScreen = 1Const theForm = 0Private Sub Command1_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,
38、 ByVal id _As Long, ByVal fsModifiers As Long, ByVal 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 L
39、ong, ByVal wMsgFilterMax As Long, ByVal _ wRemoveMsg 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 聲明常數(shù)Private
40、Const MOD_ALT = &H1Private Const MOD_CONTROL = &H2Private 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,
41、 &HBFF2&, MOD_CONTROL, vbKeyU Me.Show Form1.Hide 等待處理消息 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 轉(zhuǎn)讓控制權(quán),允許操作系統(tǒng)處理其他事件 DoEvents LoopEnd SubPrivate Sub Form_Unload(Cancel As Integer) Hot
42、Key_Fg = True 撤銷熱鍵的注冊 Call UnregisterHotKey(Me.hWnd, &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 =
43、&H1Private Const HOTKEYF_ALT = &H4Private Sub Form_Load() 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.在狀態(tài)欄顯示無邊框窗體圖標(biāo)。Private Declare Function SetWindowLong Lib user32 Alias SetWindowLongA (ByVal hWnd _ A
44、s Long, ByVal nIndex As Long, ByVal dwNewLong As Long) 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 taskbarSetW
45、indowLong Me.hWnd, GWL_STYLE, GetWindowLong(Me.hWnd, GWL_STYLE) Or WS_SYSMENUEnd Sub12. 記錄窗體的大小及位置和程序中的一些設(shè)置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
46、 = GetSetting(App.Title, Me.Name, Left, 100) Check1.Value = 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, To
47、p, Me.Top) Call SaveSetting(App.Title, Me.Name, Left, Me.Left) Call SaveSetting(App.Title, Me.Name, check1, Check1.Value)End Sub13. 解決mschart控件數(shù)據(jù)更改時的閃動現(xiàn)象1、在有MSChart控件的窗體中另外加入一個PictureBox控件,如MSChart1和Picture1。2、使Picture1和MSChart1大小一致,位置相同(通過左對齊和頂端對齊)。3、使Picture1在MSChart1前端,設(shè)置Picture1的Visible為False,即不
48、可見。只有刷新數(shù)據(jù)時Picture1才顯示。刷新數(shù)據(jù)過程Private Sub Refresh()Dim V_newchar() n維數(shù)組Picture1.Visible = TrueMSChart1.ChartData = V_newchar 給MSChart1重新賦值,即刷新數(shù)據(jù)MSChart1.EditCopy 將當(dāng)前圖表的圖片復(fù)制到剪貼板中Picture1.Picture = Clipboard.GetData() 給Picture1賦值剪貼板中的圖片End Sub這樣每一次刷新數(shù)據(jù)時Picture1顯示的圖片都不會產(chǎn)生閃爍現(xiàn)象14. 無邊框窗體的右鍵菜單設(shè)計(jì)無邊框窗體時,如果使用菜單
49、編輯器,就會自動改變成有邊框的窗體,此時,可以在另外一個窗體中(一般情況下你的程序應(yīng)該不止一個窗體的吧,如果真的只有一個,可以利用其他人寫的類,添加右鍵)編輯菜單(VISIBLE屬性設(shè)為FALSE),然后在本窗體中調(diào)用。調(diào)用形式如下:Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 ThenPopupMenu Form2.mymenuEnd IfEnd Sub15.創(chuàng)建圓角無邊框窗體Private Declare Function Crea
50、teRoundRectRgn Lib gdi32 (ByVal X1 As Integer, ByVal 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 hw
51、nd As Long, ByVal hrgn As _ Long, ByVal bRedraw As Boolean) 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.拖動沒有標(biāo)題欄的窗體方法
52、一:Private Declare Function ReleaseCapture Lib user32 () 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_Mo
53、useDown(Button As Integer, Shift As Integer, X As Single, 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方法二:回調(diào)函數(shù)module:Public Const GWL_WNDPROC = (-4)Public Const WM_NCHITTEST = &H84Public Con
54、st HTCLIENT = 1Public Const HTCAPTION = 2Declare 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 LongDeclare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal
55、hWnd As _Long, ByVal nIndex As Long) As LongDeclare Function SetWindowLong Lib user32 Alias SetWindowLongA (ByVal hWnd As _Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic prevWndProc As LongFunction WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal _Param
56、As Long) As Long WndProc = CallWindowProc(prevWndProc, hWnd, Msg, wParam, lParam) If Msg = WM_NCHITTEST And WndProc = HTCLIENT Then WndProc = HTCAPTION End IfEnd Function窗體中:Private Sub Form_Load() prevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC) SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf WndPro
57、cEnd SubPrivate Sub Form_Unload(Cancel As Integer) SetWindowLong Me.hWnd, GWL_WNDPROC, prevWndProcEnd Sub17. 半透明窗體Private Declare Function SetLayeredWindowAttributes Lib user32 (ByVal hwnd As Long, _ ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As LongPrivate Const WS_EX_LAYERED
58、 = &H80000Private Const LWA_ALPHA = &H2Private Const GWL_EXSTYLE = (-20)Private Declare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal _hwnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib user32 Alias SetWindowLongA (ByVal _hwnd As Long, ByVal nIndex
59、As Long, ByVal dwNewLong As Long) As LongPrivate Sub Form_Load() Dim rtn As Long rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE) 取的窗口原先的樣式 rtn = rtn Or WS_EX_LAYERED 使窗體添加上新的樣式WS_EX_LAYERED SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn 把新的樣式賦給窗體 SetLayeredWindowAttributes Me.hwnd, 0, 200, LWA_ALPHAEnd Sub18.開機(jī)啟
60、動(函數(shù)及常數(shù)聲明略)Private Sub Form_Load() Dim hKey As Long, SubKey As String, Exe As String SubKey = Software/Microsoft/Windows/CurrentVersion/Run Exe = 可執(zhí)行文件的路徑 RegCreateKey HKEY_CURRENT_USER, SubKey, hKey RegSetvalueEx hKey, autorun, 0, REG_SZ, ByVal Exe,LenB(StrConv(Exe, vbFromUnicode) + 1 RegCloseKey h
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 設(shè)備租賃合同:考古挖掘
- 財(cái)務(wù)管理工具與技能培訓(xùn)管理辦法
- 2024年重組家庭共有財(cái)產(chǎn)處理離婚協(xié)議3篇
- 自動離職員工交接指南
- 橋梁弱電工程承包合同樣本
- 生產(chǎn)能力評估與改進(jìn)
- 2025年度企業(yè)人才引進(jìn)合同主體變更三方協(xié)議3篇
- 游戲設(shè)備租賃合同自行操作手冊
- 長期倉儲租賃合同樣本
- 合同負(fù)債在施工企業(yè)中的應(yīng)對策略
- 煤炭供應(yīng)項(xiàng)目(運(yùn)輸供貨方案)
- “雄鷹杯”全國小動物醫(yī)師技能大賽考試題庫(660題)
- 2024年國家公務(wù)員考試《申論》真題(地市級)及答案解析
- 2024-2030年中國游艇產(chǎn)業(yè)發(fā)展?fàn)顩r規(guī)劃分析報(bào)告權(quán)威版
- 學(xué)前兒童家庭與社區(qū)教育學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 新能源汽車充電樁項(xiàng)目可行性研究報(bào)告模板及范文
- 網(wǎng)絡(luò)安全產(chǎn)品質(zhì)保與售后方案
- 2024版首診負(fù)責(zé)制度課件
- 2024-2025學(xué)年河北省高三上學(xué)期省級聯(lián)測政治試題及答案
- 貴州省貴陽市2023-2024學(xué)年高一上學(xué)期期末考試 物理 含解析
- 幼兒園班級管理中的沖突解決策略研究
評論
0/150
提交評論