vb常用代碼大全_第1頁(yè)
vb常用代碼大全_第2頁(yè)
vb常用代碼大全_第3頁(yè)
vb常用代碼大全_第4頁(yè)
vb常用代碼大全_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余11頁(yè)可下載查看

付費(fèi)下載

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、VB常同題為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.移動(dòng)無(wú)標(biāo)題欄的窗體dimm(borderstyle=none)ouseXasintegerdimmouseYasintegerdimmoveXasintegerdimmoveYasintegerdimdownasbooleanform_mousedown:'mousedown事件down=truemouseX=xmouseY=yform_mouseup:'mouseup事件down=falseform_mousemoveifdown=truethenmoveX=me.left-mouseX+XmoveY=me.to

2、p-mouseY+Yme.movemoveX,moveYendif*閃爍控件比如要閃爍一個(gè)label(標(biāo)簽)添加一個(gè)時(shí)鐘控件間隔請(qǐng)根據(jù)實(shí)際需要設(shè)置enabled屬性設(shè)為true代碼為:label1.visible=notlabel1.visible*禁止使用Alt+F4關(guān)閉窗口PrivateDeclareFunctionDeleteMenuLib"user32"(ByValhMenuAsLong,ByValnPositionAsLong,ByValwFlagsAsLong)AsLongPrivateDeclareFunctionGetMenuItemCountLib&quo

3、t;user32"(ByValhMenuAsLong)AsLongPrivateConstMFBYPOSITION=&H400&PrivateSubForm_Load()DimhwndMenuAsLongDimcAsLonghwndMenu=GetSystemMenu(Me.hwnd,0)c=GetMenuItemCount(hwndMenu)DeleteMenuhwndMenu,c-1,MF_BYPOSITIONc=GetMenuItemCount(hwndMenu)DeleteMenuhwndMenu,c-1,MF_BYPOSITIONEndSub啟動(dòng)控制面板大全

4、打開控制面板CallShell("rundll32.exeshell32.dll,Control_RunDLL",9)輔助選項(xiàng)屬性-鍵盤CallShell("rundll32.exeshell32.dll,Control_RunDLLaccess.cpl,1",9)輔助選項(xiàng)屬性-聲音CallShell("rundll32.exeshell32.dll,Control_RunDLLaccess.cpl,2",9)輔助選項(xiàng)屬性-顯示CallShell("rundll32.exeshell32.dll,Control_RunDLL

5、access.cpl,3",9)扁助選項(xiàng)屬性-鼠標(biāo)CallShell("rundll32.exeshell32.dll,Control_RunDLLaccess.cpl,4",9)扁助選項(xiàng)屬性-常規(guī)CallShell("rundll32.exeshell32.dll,Control_RunDLLaccess.cpl,5",9)展加/刪除程序?qū)傩?安裝/卸載CallShell("rundll32.exeshell32.dll,Control_RunDLLAppwiz.cpl,1",9)'添加/刪除程序?qū)傩?Window

6、s安裝程CallShell("rundll32.exeshell32.dll,Control_RunDLLAppwiz.cpl,2",9)蘇加/刪除程序?qū)傩?啟動(dòng)盤CallShell("rundll32.exeshell32.dll,Control_RunDLLAppwiz.cpl,3",9)'.示屬性-背景CallShell("rundll32.exeshell32.dll,Control_RunDLLdesk.cpl,0",9)'示屬性-屏幕保護(hù)程序CallShell("rundll32.exeshell

7、32.dll,Control_RunDLLdesk.cpl,1",9)'顯示屬性-外觀CallShell("rundll32.exeshell32.dll,Control_RunDLLdesk.cpl,2",9)'顯示屬性-設(shè)置CallShell("rundll32.exeshell32.dll,Contro文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.l_RunDLLdesk.cpl,3",9)'Internet屬性-常規(guī)CallShell("rundll32.exeshell32.dll,C

8、ontrol_RunDLLInetcpl.cpl,。',9)'Internet屬性-安全CallShell("rundll32.exeshell32.dll,Control_RunDLLInetcpl.cpl,1",9)'Internet屬性-內(nèi)容CallShell("rundll32.exeshell32.dll,Control_RunDLLInetcpl.cpl,2",9)'Internet屬性-連接CallShell("rundll32.exeshell32.dll,Control_RunDLLInetcp

9、l.cpl,3",9)*怎樣關(guān)閉一個(gè)程序你可以使用API函數(shù)FindWindow和PostMessag”尋找一個(gè)窗口并且關(guān)閉它。下面的范例演示如何關(guān)閉一個(gè)標(biāo)題為"Calculator"的窗口。DimwinHwndAsLongDimRetValAsLongwinHwnd=FindWindow(vbNullString,"Calculator")Debug.PrintwinHwndIfwinHwnd<>0ThenRetVal=PostMessage(winHwnd,WM_CLOSE,0&,0&)IfRetVal=0The

10、nMsgBox"Errorpostingmessage."EndIfElseMsgBox"TheCalculatorisnotopen."EndIfForthiscodetowork,youmusthavedeclaredtheAPIfunctionsinamoduleinyourproject.Youmustputthefollowinginthedeclarationssectionofthemodule.DeclareFunctionFindWindowLib"user32"Alias_"FindWindowA&quo

11、t;(ByVallpClassNameAsString,_ByVallpWindowNameAsString)AsLongDeclareFunctionPostMessageLib"user32"Alias"PostMessageA"(ByValhwndAsLong,ByValwMsgAsLong,_ByValwParamAsLong,lParamAsAny)AsLongPublicConstWM_CLOSE=&H10*如何使Form的背景圖隨Form大小改單純顯示圖形用Image即可,而且用Image也正好可解決你的問(wèn)題設(shè)定Image的Stre

12、tch=true在加入以下的codePrivateSubForm_Resize()Image1.Move0,0,ScaleWidth,ScaleHeightEndSub或者使用以下的方式來(lái)做也可以PrivateSubForm_Paint()Me.PaintPictureMe.Picture,0,0,ScaleWidth,ScaleHeightEndSub*軟件的注冊(cè)可用注冊(cè)表簡(jiǎn)單地保存已用的天數(shù)或次數(shù)'次數(shù)限制(如30次)如下:PrivateSubForm_Load()DimRemainDayAsLongRemainDay=GetSetting("MyApp",&q

13、uot;set","times",0)IfRemainDay=30ThenMsgBox"式用次數(shù)已滿,請(qǐng)注冊(cè)"UnloadMeEndIfMsgBox'現(xiàn)在剩下:"&30-RemainDay&惱用次數(shù),好好珍惜!"RemainDay=RemainDay+1SaveSetting"MyApp","set","times",RemainDayEndSub'時(shí)間限制的(如30天)PrivateSubForm_Load()DimRemainDa

14、yAsLongRemainDay=GetSetting("MyApp","set","day",0)IfRemainDay=30ThenMsgBoxT式用期已過(guò),請(qǐng)注冊(cè)"UnloadMeEndIfMsgBox'現(xiàn)在剩下:"&30-RemainDay&1M用天數(shù),好好珍惜!"ifday(now)-remainday>0thenRemainDay=RemainDay+1SaveSetting"MyApp","set","times

15、",RemainDayEndSub*MMControl控件全屏播放OptionExplicitPrivateDeclareFunctionmciSendStringLib"winmm.dll"_Alias"mciSendStringA"(ByValIpstrCommandAs_String,ByVallpstrReturnStringAsAny,ByVal_uReturnLengthAsLong,ByValhwndCallbackAs_Long)AsLongPrivateDeclareFunctionmciSendCommandLib"

16、;winmm.dll"_Alias"mciSendCommandA"(ByValwDevicelDAsLong,_ByValuMessageAsLong,ByValdwFramiAsLong,_dwParam2AsMCI_OVLY_RECT_PAMS)AsLongPrivateDeclareFunctionGetShortPathNameLib"kernel32"_Alias"GetShortPathNameA"(ByVallpszLongPathAs_String,ByVallpszShortPathAsStringByV

17、al_cchBufferAsLong)AsLongPrivateTypeRECTLeftAsLong文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.TopAsLongRightAsLongBottomAsLongEndTypePrivateTypeMCI_OVLY_RECT_PARMSdwCallbackAsLongrcAsRECTEndTypeConstMCI_OVLY_WHERE_SOURCE=&H20000ConstMCI_OVLY_WHERE_DESTINATION=&H40000ConstMCI_WHERE=&H843DimPlayAsBoole

18、anPrivateSubForm_Load()MMControll.Wait=TrueMMControll.Updatelnterval=50MMControll.hWndDisplay=Picturel.hWndPicturel.ScaleMode=3Timerl.Interval=50EndSubPrivateSubForm_Unload(CancelAsInteger)MMControll.Command="stop"MMControll.Command="close"EndSubaPrivateSubCommand1_Click()MMContr

19、oll.Command="stop"RMMControll.Command="close"Play=FalseCommonDialogl.Filter=("VB-Dateien(*.avi)|*.avi;")CommonDialogl.InitDir=App.PathCommonDialogl.ShowOpenIfCommonDialogl.filename<>""ThenMMControll.DeviceType="avivideo"MMControli.filename=Comm

20、onDialo文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.gl.filenameMMControll.Command="open"MMControll.Notify=TrueLabel4.Caption=MMControl1.LengthIfCheck2.Value=vbCheckedAndOpticn2ThenCallAdaptPictureEndIfIfOption3.ValueThenCallOption3_ClickMe.Caption=CommonDialogl.filenameEndIfEndSubPrivateSubCommand2_Clic

21、k()IfNotOption3.ValueThenIfPlay=FalseAndMMControll.filename<>""ThenMMControll.Command="play"Play=TrueEndIfElseCallOption3_ClickEndIfEndSubPrivateSubCommand3_Click()Play=FalseMMControll.Command="stop"EndSubPrivateSubCommand4_Click()MMControll.Command="pause&qu

22、ot;EndSubPrivateSubMMControl1_Done(NotifyCodeAsInteger)IfPlayAndCheckl.Value=vbCheckedThenPlay=FalseMMControll.Command="stop"MMControll.Command="prev"MMControll.Command="play"Play=TrueEndIfEndSubPrivateSubMMControl1_StatusUpdate()Label2.Caption=MMControll.PositionEndSub

23、PrivateSubOption1_Click()Checkl.Enabled=TrueCheck2.Enabled=FalseMMControl1.hWndDisplay=0EndSubPrivateSubOption2_Click()Check1.Enabled=TrueCheck2.Enabled=TrueMMControl1.hWndDisplay=Picture1.hWndEndSubPrivateSubOption3_Click()注意這里DimR&,AA$Check1.Enabled=FalseCheck2.Enabled=FalseMMControl1.Command=

24、"stop"Play=FalseAA=Space$(255)R=GetShortPathName(CommonDialog1.filename,AA,Len(AA)AA=Mid$(AA,1,R)R=mciSendString("play"&AA&"fullscreen",0&,0,0&)EndSubPrivateSubCheck2_Click()IfCheck2.Value=vbCheckedAndMMControl1.filename<>""ThenCallAdaptP

25、ictureEndIfEndSub文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.PrivateSubTimer1_Timer()Dimx%,AA$x=MMControll.ModeSelectCasexCase 524: AA="NotOpen"Case 525: AA="Stop"Case 526: AA="Play"Case 527: AA="Record"Case 528: AA="Seek"Case 529: AA="Pause"Case 530: A

26、A="Ready"EndSelectLabel6.Caption=AAEndSubPrivateSubAdaptPicture()DimResult&,ParAsMCI_OVLY_RECT_ARMSPar.dwCallback=MMControll.hWndResult=mciSendCommand(MMControll.DevicelD,_MCI_WHERE,MCI_OVLY_WHERSOURCE,Par)IfResult<>0ThenMsgBox("Fehler")ElsePicturel.Width=(-*15+4*15Pict

27、urel.Height=(-*15+4*15EndIfEndSub*通用對(duì)話框?qū)]?全)使用API調(diào)用Winodws各種通用對(duì)話框(CommonDiaglog)勺方法()1 .文件屬性對(duì)話框TypeSHELLEXECUTEINFOcbSizeAsLongfMaskAsLonghwndAsLonglpVerbAsStringlpFileAsStringlpParametersAsStringlpDirectoryAsStringnShowAsLonghInstAppAsLonglpIDListAsLong可選參數(shù)lpClassAsString可選參數(shù)hkeyClassAsLon。選參數(shù)dwHot

28、KeyAsLong可選參數(shù)hIconAsLong可選參數(shù)hProcessAsLong可選參數(shù)EndTypeConstSEE_MASK_INVOKEIDLIST=&HCConstSEE_MASK_NOCLOSEPROCESS=&H40ConstSEE_MASK_FLAG_NO_UI=&H400DeclareFunctionShellExecuteEXLib"shell32.dll"Alias"ShellExecuteEx"_(SEIAsSHELLEXECUTEINFO)AsLongPublicFunctionShowProperti

29、es(filenameAsString,OwnerhWndAsLong)AsLong'打開指定文件的屬性對(duì)話框,如果返回E_S<=32則出錯(cuò)DimSEIAsSHELLEXECUTEINFODimrAsLongWithSEI.cbSize=Len(SEI).fMask=SEE_MASK_NOCLOSEPROCESSOrSEE_MASK_INVOKEIDLISTOrSEE_MASK_FLAG_NO_UI.hwnd=OwnerhWnd.lpVerb="properties".lpFile=filename.lpParameters=vbNullChar.lpDire

30、ctory=vbNullChar.nShow=0.hInstApp=0.lpIDList=0EndWithr=ShellExecuteEX(SEI)ShowProperties=SEI.hInstAppEndFunction新建一個(gè)工程,添加一個(gè)按鈕和名為Text1的文本框文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.把以下代碼置入CommandbButton_Click中DimrAsLongDimfnameAsString'從Text1中獲取文件名及路徑fname=(Text1)r=ShowProperties(fname,Me.hwnd)Ifr<=32Then

31、MsgBox"Error"2 .使用Win95的關(guān)于對(duì)話框PrivateDeclareFunctionShellAboutLib"shell32.dll"_Alias"ShellAboutA"(ByValhwndAsLong,ByValszAppAsString,_ByValszOtherStuffAsString,ByValhIconAsLong)AsLong示例:DimxAsLongx=shellabout(Form1.hwnd,"VisualBasic6.0",_"AlpStudioMouseTra

32、ckerVer1.0",Form1.icon)2 .調(diào)用"捕獲打印機(jī)端口”對(duì)話框PrivateDeclareFunctionWNetConnectionDialogLib"mpr.dll"_(ByValhwndAsLong,ByValdwTypeAsLong)AsLong示MDimxAsLongx=WNetConnectionDialog(Me.hwnd,2)3 .調(diào)用顏色對(duì)話框PrivateTypeChooseColorlStructSizeAsLonghwndOwnerAsLonghInstanceAsLongrgbResultAsLonglpCus

33、tColorsAsStringflagsAsLonglCustDataAsLonglpfnHookAsLonglpTemplateNameAsStringEndTypePrivateDeclareFunctionChooseColorLib"comdlg32.dll"Alias"ChooseColorA"(pChoosecolorAsChooseColor)AsLong將以下代碼置入某一事件中:DimccAsChooseColorDimCustC010r(16)AsLongcc.lStructSize=Len(cc)cc.hwndOwner=Form1.

34、hWndcc.hInstance=App.hInstancecc.flags=0cc.lpCustColors=String$(16*4,0)DimaDimxDimclDimc2Dimc3Dimc4a=ChooseColor(cc)ClsIf(a)ThenMsgBox"Colorchosen:"&Str$(cc.rgbResult)Forx=1ToLen(cc.lpCustColors)Step4cl=Asc(Mid$(cc.lpCustColors,x,1)c2=Asc(Mid$(cc.lpCustColors,x+1,1)c3=Asc(Mid$(cc.lpCus

35、tColors,x+2,1)c4=Asc(Mid$(cc.lpCustColors,x+3,1)CustColor(x/4)=(c1)+(c2*256)+(c3*65536)+(c4*)MsgBox"CustomColor"&Int(x/4)&"="&CustColor(x/4)NextxElseMsgBox"Cancelwaspressed"EndIf4 .調(diào)用復(fù)制磁盤對(duì)話框PrivateDeclareFunctionSHFormatDriveLib"shell32"(ByValhwndA

36、sLong,ByValDriveAsLong,ByValfmtIDAsLong,ByValoptionsAsLong)AsLongPrivateDeclareFunctionGetDriveTypeLib"kernel32"Alias"GetDriveTypeA"(ByValnDriveAsString)AsLong文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.示例:向窗體中添加一個(gè)名為Drivel的DriveListBox將以下代碼置入某一事件中DimDriveLetter$,DriveNumber&,DriveType&

37、;DimRetVal&,RetFromMsg&DriveLetter=UCase(Drive1.Drive)DriveNumber=(Asc(DriveLetter)-65)DriveType=GetDriveType(DriveLetter)IfDriveType=2Then'Floppies,etcRetVal=Shell("rundll32.exediskcopy.dll,DiskCopyRunDll"_&DriveNumber&","&DriveNumber,1)'Noticespaceaf

38、terElse'Justincase'DiskCopyRunDllRetFromMsg=MsgBox("Onlyfloppiescan"&vbCrLf&_"bediskcopied!",64,"DiskCopyExample")EndIf5.調(diào)用格式化軟盤對(duì)話框PrivateDeclareFunctionSHFormatDriveLib"shell32"(ByValhwndAsLong,ByValDriveAsLong,ByValfmtIDAsLong,ByValoptionsAsL

39、ong)AsLongPrivateDeclareFunctionGetDriveTypeLib"kernel32"Alias"GetDriveTypeA"(ByValnDriveAsString)AsLong參數(shù)設(shè)置:fmtID-3.5"5.25"01.44M1.2M11.44M1.2M21.44M1.2M31.44M360K41.44M1.2M5720K1.2M61.44M1.2M71.44M1.2M81.44M1.2M91.44M1.2M選項(xiàng)0快速1完全2只復(fù)制系統(tǒng)文件3只復(fù)制系統(tǒng)文件4快速5完全6只復(fù)制系統(tǒng)文件7只復(fù)制系統(tǒng)文件8

40、快速9完全示例:要求同上DimDriveLetter$,DriveNumber&,DriveType&DimRetVal&,RetFromMsg%DriveLetter=UCase(Drive1.Drive)DriveNumber=(Asc(DriveLetter)-65)'ChangelettertoNumber:A=0DriveType=GetDriveType(DriveLetter)IfDriveType=2Then'Floppies,etcRetVal=SHFormatDrive(Me.hwnd,DriveNumber,0&,0&

41、;)ElseRetFromMsg=MsgBox("ThisdriveisNOTaremoveable"&vbCrLf&_"drive!Formatthisdrive?",276,"SHFormatDriveExample")SelectCaseRetFromMsgCase6'Yes'UnCommenttodoit.'RetVal=SHFormatDrive(Me.hwnd,DriveNumber,0&,0&)Case7'No'DonothingEndSelectE

42、ndIf*使用API調(diào)用Winodws各種通用對(duì)話框(CommonDiaglog)勺方法(二)1 .選擇目錄/文件夾對(duì)話框?qū)⒁韵麓a置于一模塊中OptionExplicit調(diào)用方文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.式二string=BrowseForFolders(Hwnd,TitleOfDialog)例如:String1=BrowseForFolders(Hwnd,"Selecttargetfolder.")PublicTypeBrowseInfohwndOwnerAsLongpIDLRootAsLongpszDisplayNameAsLongl

43、pszTitleAsLongulFlagsAsLonglpfnCallbackAsLonglParamAsLongiImageAsLongEndTypePublicConstBIF_RETURNONLYFSDIRS=PublicConstMAX_PATH=260PublicDeclareSubCoTaskMemFreeLib"ole32.dll"(ByValhMemAsLong)PublicDeclareFunctionlstrcatLib"kernel32"Alias"lstrcatA"(ByVallpString1AsString

44、,ByVallpString2AsString)AsLongPublicDeclareFunctionSHBrowseForFolderLib"shell32"(lpbiAsBrowseInfo)AsLongPublicDeclareFunctionSHGetPathFromIDListLib"shell32"(ByValpidListAsLong,ByVallpBufferAsString)AsLongPublicFunctionBrowseForFolder(hwndOwnerAsLong,sPromptAsString)AsStringDimiNu

45、llAsIntegerDimlpIDListAsLongDimlResultAsLongDimsPathAsStringDimudtBIAsBrowseInfo'初始化變量WithudtBI.hwndOwner=hwndOwner.lpszTitle=lstrcat(sPrompt,"").ulFlags=BIF_RETURNONLYFSDIRSEndWith'調(diào)用APIlpIDList=SHBrowseForFolder(udtBI)IflpIDListThensPath=String$(MAX_PATH,0)lResult=SHGetPathFromID

46、List(lpIDList,sPath)CallCoTaskMemFree(lpIDList)iNull=InStr(sPath,vbNullChar)IfiNullThensPath=Left$(sPath,iNull-1)EndIf如果選擇取消,sPath=""BrowseForFolder=sPathEndFunction2 .調(diào)用”映射網(wǎng)絡(luò)驅(qū)動(dòng)器”對(duì)話框Private/PublicDeclareFunctionWNetConnectionDialogLib"mpr.dll"_(ByValhwndAsLong,ByValdwTypeAsL1ong)

47、AsLongx%=WNetConnectionDialog(Me.hwnd,1)3 .調(diào)用“打開文件”對(duì)話框PrivateTypeOPENFILENAMElStructSizeAsLonghwndOwnerAsLonghInstanceAsLonglpstrFilterAsStringlpstrCustomFilterAsStringnMaxCustFilterAsLongnFilterIndexAsLonglpstrFileAsStringnMaxFileAsLonglpstrFileTitleAsStringnMaxFileTitleAsLonglpstrInitialDirAsStrin

48、glpstrTitleAsStringflagsAsLongnFileOffsetAsIntegernFileExtensionAsIntegerlpstrDefExtAsStringlCustDataAsLonglpfnHookAsLonglpTemplateNameAsStringEndTypePrivateDeclareFunctionGetOpenFileNameLib"comdlg32.dll"Alias"GetOpenFileNameA"(pOpenfilenameAsOPENFILENAME)AsLong文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word

49、版本可編輯.歡迎下載支持.將以下代碼置于某一事件中DimofnAsOPENFILENAMEofn.lStructSize=Len(ofn)ofn.hwndOwner=Form1.hWndofn.hInstance=App.hInstanceofn.lpstrFilter="TextFiles(*.txt)"+Chr$(0)+"*.txt"+Chr$(0)+"RichTextFiles(*.rtf)"+Chr$(0)+"*rtf"+Chr$(0)ofn.lpstrFile=Space$(254)ofn.nMaxFil

50、e=255ofn.lpstrFileTitle=Space$(254)ofn.nMaxFileTitle=255ofn.lpstrInitialDir=curdirofn.lpstrTitle="OurFileOpenTitle"ofn.flags=0Dimaa=GetOpenFileName(ofn)If(a)ThenMsgBox"FiletoOpen:"+Trim$(ofn.lpstrFile)ElseMsgBox"Cancelwaspressed"EndIf4 .調(diào)用“打印”對(duì)話框PrivateTypePrintDlglStru

51、ctSizeAsLonghwndOwnerAsLonghDevModeAsLonghDevNamesAsLonghdcAsLongflagsAsLongnFromPageAsIntegernToPageAsIntegernMinPageAsIntegernMaxPageAsIntegernCopiesAsIntegerhInstanceAsLonglCustDataAsLonglpfnPrintHookAsLonglpfnSetupHookAsLonglpPrintTemplateNameAsStringlpSetupTemplateNameAsStringhPrintTemplateAsLo

52、nghSetupTemplateAsLongEndTypePrivateDeclareFunctionPrintDlgLib"comdlg32.dll"Alias"PrintDlgA"(pPrintdlgAsPrintDlg)AsLong將以下代碼置于某一事件中DimtPrintDlgAsPrintDlgtPrintDlg.lStructSize=Len(tPrintDlg)tPrintDlg.hwndOwner=Me.hwndtPrintDlg.hdc=hdctPrintDlg.flags=0tPrintDlg.nFromPage=0tPrintDlg

53、.nToPage=0tPrintDlg.nMinPage=0tPrintDlg.nMaxPage=0tPrintDlg.nCopies=1tPrintDlg.hInstance=App.hInstancelpPrintTemplateName="PrintPage"Dimaa=PrintDlg(tPrintDlg)IfaThenlFromPage=tPrintDlg.nFromPagelToPage=tPrintDlg.nToPagelMin=tPrintDlg.nMinPagelMax=tPrintDlg.nMaxPagelCopies=tPrintDlg.nCopies

54、PrintMyPage'CustomprintingSubroutineEndIf*用WinSock控件下載文件1增加一個(gè)Winsock控件,名稱為Winsock1。2建立連接:Winsock1.RemoteHost=""Winsock1.RemotePort=80Winsock1.Connect3在Winsock1.Connect事件中加入:DimstrCommandasStringDimstrWebPageasStringstrWebPage="kenj/index.html"strCommand="GET"+strWeb

55、Page+"HTTP/1.0"+vbCrLfstrCommand=strCommand+"Accept:*/*"+vbCrLf文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.strCommand=strCommand+"Accept:text/html"+vbCrLfstrCommand=strCommand+vbCrLfWinsockl.SendDatastrCommand4Winsock開始下載,在收到數(shù)據(jù)時(shí),發(fā)生DataArrival事件。DimwebDataAsStringWinsockl.GetDatawebD

56、ata,vbStringTxtWebPage.Text=TxtWebPage.Text+wbData*用VB實(shí)現(xiàn)客戶一一服務(wù)器(TCP/IP編程實(shí)例現(xiàn)在大多數(shù)語(yǔ)言都支持客戶-服務(wù)器模式(C/S闔程,其中VB給我們提供了很好的客戶-服務(wù)器編程方式。下面我們用VB來(lái)實(shí)現(xiàn)TCP/IP網(wǎng)絡(luò)編程。TCP/IP協(xié)議是Internet最重要的協(xié)議。VB提供了WinSock控件,用于在TCP/IP的基礎(chǔ)上進(jìn)行網(wǎng)絡(luò)通信。當(dāng)兩個(gè)應(yīng)用程序使用Socket進(jìn)行網(wǎng)絡(luò)通信時(shí),其中一個(gè)必須創(chuàng)建Socket服務(wù)器偵聽(tīng),而另一個(gè)必須創(chuàng)建Socket客戶去連接服務(wù)器。這樣兩個(gè)程序就可以進(jìn)行通信了。1 .創(chuàng)建服務(wù)器,首先創(chuàng)建一個(gè)服

57、務(wù)端口號(hào)。并開始偵聽(tīng)是否有客戶請(qǐng)求連接。建立一窗體,并向其增加一個(gè)Winsock控件(可在工程菜單中的部件項(xiàng)來(lái)添加此控件)添加兩文本框Text1,Text左口一按鈕Command1PrivateSubForm_Load()SockServer.LocalPort=2000R務(wù)器端口號(hào),最好大于1000SockServer.ListeniJ干始偵聽(tīng)EndSubPrivateSubForm_Unload(CancelAsInteger)SockServer.CloseEndSubPrivateSubSockServer_Close()SockServer.CloseEndSubPrivateSub

58、SockServer_ConnectionRequest(ByValrequestIDAsLong)SockServer.CloseSockServer.AcceptrequestI榮示客戶請(qǐng)求連接的ID號(hào)EndSub'當(dāng)客戶向服務(wù)器發(fā)送數(shù)據(jù)到達(dá)后,產(chǎn)生DataArrival事件,在事件中接收數(shù)據(jù),GetData方法接收數(shù)據(jù)。PrivateSubSockServer_DataArrival(ByValbytesTotalAsLong)DimsAsStringSockServer.GetDatasTextl.Text=sEndSub當(dāng)我需要向客戶發(fā)送數(shù)據(jù)時(shí),只需調(diào)用SendData方法。

59、PrivateSubCommand1_Click()SockServer.SendDataText2.TextEndSub2 .創(chuàng)建客戶。要?jiǎng)?chuàng)建客戶連接服務(wù)器,首先設(shè)置服務(wù)器主機(jī)名,如IP地址、域名或計(jì)算機(jī)名,然后設(shè)置服務(wù)器端口,最后連接服務(wù)器。建立一窗體,并向其增加一個(gè)Winsock控件(可在工程菜單中的部件項(xiàng)來(lái)添加此控件),取名為:SockCl添加兩文本框Text1,Text2f口一按鈕CommandlPrivateSubForm_Load()SockCl.RemoteHos='表示服務(wù)器主機(jī)名SockCl.RemotePort=2000表示服務(wù)器端口名SockCl.Connect

60、連接到服務(wù)器EndSubPrivateSubForm_Unload(CancelAsInteger)SockCl.CloseEndSub-10-文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.PrivateSubSockCl_Close()SockCl.CloseEndSubPrivateSubSockCl_DataArrival(ByValbytesTotalAsLong)DimsAsStringSockCl.GetDatas接收數(shù)據(jù)到文本框中Text1.Text=sEndSubPrivateSubCommand1_Click()SockCl.SendDataText2.Te姆服務(wù)器發(fā)送數(shù)據(jù)EndSub3 .進(jìn)行通信。把這兩個(gè)窗體分別編譯成兩個(gè)EXEDt件,服務(wù)器Server.exe和客戶Client.exe程序,并把它們分別安裝在服務(wù)器端和客戶端,這樣就可以實(shí)現(xiàn)兩者通信了。*PING一個(gè)I

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論