已阅读5页,还剩79页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
更多学习资源请登录()VB程序设计基础:窗体属性程序设计是指为计算机编制一些软件和程序。学习的目的是理解计算机的工作方式和运行的方式。VB是运行在Windows视窗操作系统上,设计程序的思想在于在窗口中放置对象和针对对象设计代码。(一)启动VB:单击“开始”“程序”“Microsoft Visual Basic 6.0 中文版”选择“标准EXE”,单击“打开”进入窗口。(二)VB窗口1、标题栏:在最上面蓝色标题是“工程1.”,是我们当前正在设计的程序,被称作“工程1”。2、菜单栏:菜单栏在第二行,包含一组菜单,菜单中有各种命令,我们应该理解操作计算机就是向计算机提供一系列的指令。(1)“文件”菜单,要求记住“保存”命令,VB要保存两次,注意保存文件到自己的文件夹中。(本书中双引号当中均是指操作命令)(2)“视图”菜单,显示各个窗口,如果一个窗口找不着了,就到这个菜单中找出来。(3)“运行”菜单,第一个“启动”命令要记住,可以运行我们的程序。3、工具栏:第三行是工具栏,工具栏是一些最常用的命令,而且是用图标表示的,因而便于形象记忆和操作,要求记住“启动”按钮。(在视窗操作系统中,我们不仅要会认识文字说明,还要特别留意图标含义。)4、工作区:工作区占据窗口的绝大部分,从左向右包括工具箱、窗体、和属性窗口。(1) 工具箱:在工作区的左侧,包含许多编程用到的基本对象,被称作“控件”。(2) 窗体:中间的灰色的窗口,是我们设计的程序的基本框架,所有的对象都放在这个窗口当中。(3)小窗口:在右侧有三个纵向排列的窗口。最上面是工程窗口,上边有三个按钮,中间的按钮是显示对象窗口;下面是属性窗口,属性是一个对象的名称、大小、颜色等等;最下面是布局窗口,里面显示程序运行时的位置,拖动里面的小框可以改变程序的显示位置。4、下面修改窗体的属性,在右侧的属性窗口中找到Caption项,在它右边的格子里点一下鼠标左键,然后输入“窗体”两个字,观察一下工作区中的窗体的标题是否已经改变,Caption是设置一个应用程序的窗口标题,再在属性窗口中找到BackColor项,单击右边格子里的黑色三角按钮上点击一下鼠标左键,在弹出的面板上边的一行文字中选择“调色板”标签,选中浅紫色,观察一下工作区中间的窗体的变化。单击工具栏上的启动按钮,运行一下我们的程序,看一下这个窗口,还不错吧?单击“关闭”按钮(或者AltF4),回到窗体中,在属性窗口中找到Icon项,在右边的格子里点一下鼠标左键再点一下三个小点,在弹出的“加载图标”对话框中,点击“向上”按钮退一步,再选择打开Common,再选择打开Graphics,再选择打开Icon,再打开Misc,在里面找一个图标,单击打开,再看一下工作区中的窗体的标题栏左侧是否有一个图标。单击“启动”按钮,运行一下程序,然后最小化,看一下任务栏上的图标找到我们的程序,单击一下图标恢复窗口。关闭程序回到属性窗口,找到Picture属性,在右边的格子里单击一下鼠标左键,打开“加载图片”对话框,找一幅较大的背景图片打开,观察一下工作区窗体的变化。单击“启动”按钮运行一下程序,很漂亮的一个窗口是吧?关闭程序,同样再修改Font属性和ForeColor属性,属性都是按照字母排序的,在最后的WindowState属性中选择2Maxing最大化,运行一下程序看一下效果。如何保存自己的程序呢?方法是在“文件”菜单中选择“保存工程”或者点击工具栏上的磁盘按钮,保存文件到自己的文件夹(如果没有就新建一个)以“MyForm”为文件名(如果能用中文就输入汉字“窗体”)。保存两次,一次是窗体,另一次是工程。保存好的程序要编译成可以在Windows下运行的可执行程序,方法是选择“文件”菜单中的“生成工程1.exe”命令,以“窗体”为文件名保存文件到自己的文件夹,这个程序关闭VB后也可以运行,退出VB,打开自己的文件夹,找到程序,运行一下看看。本节主要讲述了VB的基本窗口和窗体属性,以后会经常用到。API在VB中应用之技巧集锦API在VB中应用之技巧集锦API函数在VB中得到了充分的运用,同时也让无数VB爱好者沉溺于其中。以下是笔者几年来收集整理的几十个API函数在VB中应用的实例,现在写出来与大伙分享,希望能对大伙有所帮助。1、如何让窗体总在最前面?*API函数声明Declare Function SetWindowPos Lib 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 Long注释:常量声明Private Const SWP_NOSIZE = &H1Private Const SWP_NOMOVE = &H2Private Const HWND_TOPMOST = -1Private Const HWND_NOTOPMOST = -2注释: 在某个form里写:SetWindowPos me.hWnd,WND_TOPMOST,0,0,0,0, SWP_NOMOVE 注释:或下面SetWindowPos me.hWnd,WND_TOPMOST,0,0,0,0, SWP_NOSIZE2、使用API函数sendmessage,获得光标所在行和列。Sub getcaretpos(byval TextHwnd&,LineNo&,ColNo&)注释:TextHwnd为TextBox的hWnd属性值,LineNo为所在行数,ColNo为列数dim i&,j&,k& 注释:获取起始位置到光标所在位置字节数i=SendMessage(TextHwnd,&HB0&,0,0) j=i/216 注释:确定所在行LineNo=SendMessage(TextHwnd,&HC9&,j,0)+1注释:确定所在列k=SendMessage(TextHwnd,&HBB&,-1,0)ColNo=j-k+1End sub3、如何以某种颜色填充某区域?*API函数声明Private Declare Sub FloodFill Lib gdi32 _ (ByVal hDC As Long, ByVal X As Long, ByVal Y As _ Long, ByVal crColor As Long注释:设(fillx,filly)为此区域内任一点注释:Color为某种颜色FloodFill Picture1.hDC, fillx, filly,Color4、如何关闭计算机?*API函数声明Declare Function ExitWindows Lib User (ByVal dwReturnCode As Long, ByVal wReserved As Integer) As Integer注释:执行Dim DUMMYDUMMY=ExitWindows(0,0)5、如何获取Windows目录和System目录?注释:复制以下代码到一模块中Public Declare Function GetWindowsDirectory Lib kernel32 Alias GetWindowsDirectoryA (ByVal lpBuffer As String, ByVal nSize As Long) As LongPublic Declare Function GetSystemDirectory Lib kernel32 Alias GetSystemDirectoryA (ByVal lpBuffer As String, ByVal nSize As Long) As Long注释:在程序中调用Dim WindowsDirectory As String, SystemDirectory As String, x As LongWindowsDirectory = Space(255)SystemDirectory = Space(255)x = GetWindowsDirectory(WindowsDirectory, 255)x = GetSystemDirectory(SystemDirectory, 255)MsgBox Windows的安装目录是: + WindowsDirectory+,系统目录是: + SystemDirectory6、如何建立简单的超级连接?*API函数声明Private Declare Function ShellExecute Lib shell32.dll Alias ShellExecute A (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd A s Long) As Long注释:打开某个网址ShellExecute 0, open, , vbNullString, vbNullString, 3注释:给某个信箱发电子邮件ShellExecute hwnd, open, mailto:, vbNullString, vbNullString, 07、如何得知TextBox中文字所有的行数?*API函数声明Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Const EM_GETLINECOUNT = &HBA注释:在程序中调用LineCnt = SendMessage(ctl.hwnd, EM_GETLINECOUNT, 0, 0)注释:LineCnt即为此TextBox的行数。8、如何设置ListBox的水平卷动轴的宽度?*API函数声明Const LB_SETHORIZONTALEXTENT = &H194Private Declare Function SendMessage Lib user32 Alias SendMessageA _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As Long注释:调用Call SendMessage(List1.hwnd, LB_SETHORIZONTALEXTENT, 400, ByVal 0&)注释:注意400是以象素为单位,你可以根据情况自行设定。9、如何交换鼠标按键?*API函数声明Declare Function SwapMouseButton& Lib user32 _ (ByVal bSwap as long)要交换鼠标按键,将bSwap参数设置为True。要恢复正常设置,将bSwap设置为False。 然后调用函数就可以交换和恢复鼠标按键了。10、如何让窗体的标题条闪烁以引起用户注意?在窗体中放一个Timer控件Timer1,设置其Inteval=200*API函数声明Private Declare Function FlashWindow Lib user32 (ByVal hwnd As Long, ByVal bInvert As Long) As Long注释:在窗体中写下如下代码:Private Sub Timer1_Timer()FlashWindow Me.hwnd, TrueEnd Sub11、怎样找到鼠标指针的XY坐标?*API函数声明Type POINTAPIx As Longy As LongEnd TypeDeclare Function GetCursorPos Lib user32 (lpPoint As POINTAPI) As Long调用:GetCursorPos zprint z.xprint z.y12、怎样获得和改变双击鼠标的时间间隔?获得鼠标双击间隔时间:Public Declare Function GetDoubleClickTime Lib user32 Alias _ GetDoubleClickTime () As Long获得鼠标双击间隔时间:Declare Function SetDoubleClickTime Lib user32 Alias SetDoubleClickTime (ByVal wCount As Long) As Long注释:注意:这种改变将影响到整个操作系统以上两个函数都可精确到毫秒级。13、在程序中如何打开和关闭光驱门?*API函数声明如下:Private Declare Function mciSendString Lib winmm.dll Alias mciSendStringA (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long注释:调用时的代码如下Dim Ret As LongDim RetStr As String注释:打开光驱门Ret = mciSendString(set CDAudio door open, RetStr, 0, 0)注释:关闭光驱门Ret = mciSendString(set CDAudio door closed, RetStr, 0, 0)14、如何获得Windows启动方式?在Form1中加入一个CommandButton、一个Label并加入如下代码:Private Declare Function GetSystemMetrics Lib user32 (ByVal nIndex As Long) As LongConst SM_CLEANBOOT = 67Private Sub Command1_Click()Select Case GetSystemMetrics(SM_CLEANBOOT)Case 1Label1 = 安全模式.Case 2Label1 = 支持网络的安全模式.Case ElseLabel1 = Windows运行在普通模式.End SelectEnd Sub15、怎样使Ctrl-Alt-Delete无效?*API函数声明Private Declare Function SystemParametersInfo Lib user32 Alias SystemParametersInfoA (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long编写如下函数:Sub DisableCtrlAltDelete(bDisabled As Boolean)Dim X As LongX = SystemParametersInfo(97, bDisabled, CStr(1), 0)End Sub使Ctrl-Alt-Delete无效 :Call DisableCtrlAltDelete(True)恢复Ctrl-Alt-Delete :Call DisableCtrlAltDelete(False)16、如何移动没有标题栏的窗口?我们一般是用鼠标按住窗口的标题栏,然后移动窗口,当窗口没有标题栏时,我们可以用下面的方法来移动窗口:*API函数声明: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 LongPublic Const HTCAPTION = 2Public Const WM_NCLBUTTONDOWN = &HA1在 Form_MouseDown 事件中:Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)ReleaseCapture SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION,0&End Sub17、VB中如何使用延时函数?*API函数声明:Declare Sub Sleep Lib kernel32 (ByVal dwMilliseconds As Long)调用:注释:延时1秒Call Sleep(1000)18、调用修改屏幕保护口令的窗口:Private Declare Function PwdChangePassword Lib mpr Alias PwdChangePasswordA (ByVal lpcRegkeyname As String, ByVal hwnd As Long, ByVal uiReserved1 As Long, ByVal uiReserved2 As Long) As Long调用:Call PwdChangePassword(SCRSAVE, Me.hwnd, 0, 0)19、使Windows开始屏幕保护:*API函数声明Private Declare Function SendMessage Lib user32Alias SendMessageA (ByVal hWnd As Long, ByVal wMsgAs Long, ByVal wParam As Long, ByVal lParam As Long)As LongConst WM_SYSCOMMAND = &H112&Const SC_SCREENSAVE = &HF140&注释:调用Dim result As Longresult = SendMessage(Form1.hWnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0&)20、如何改变Windows桌面背景?*API函数声明Const SPI_SETDESKWALLPAPER = 20Const SPIF_UPDATEINIFILE = &H1Declare Function SystemParametersInfo Lib user32 Alias SystemParametersInfoA (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long注释:调用Call SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, C:windowsClouds.bmp, SPIF_UPDATEINIFILE)21、怎样确定系统是否安装了声卡?*API函数声明:Declare Function waveOutGetNumDevs Lib winmm.dll Alias waveOutGetNumDevs () As Long代码如下:Dim i As Integeri = waveOutGetNumDevs()If i 0 Then MsgBox 你的系统可以播放声音。, vbInformation, 声卡检测ElseMsgBox 你的系统不能播放声音。, vbInformation, 声卡检测End If22、如何找到CD-ROM驱动器的盘号?下面的函数将检查你计算机所有的驱动器看是否是 CD-ROM,如果是就返回驱动器号,如果没有就返回空字符Public Function GetCDROMDrive() As StringDim lType As Long,i As Integer,tmpDrive as String,found as BooleanOn Error GoTo errLFor i = 0 To 25tmpDrive = Chr(65 + i) & :lType = GetDriveType(tmpDrive) 注释:Win32 API 函数If (lType = DRIVE_CDROM) Then 注释:Win32 API 常数found = TrueExit ForEnd IfNextIf Not found Then tmpDrive = BI_GetCDROMDrive = tmpDriveexit FunctionerrL: msgbox error$End Function23、如何将文件放入回收站?*API函数声明Public Type SHFILEOPSTRUCThwnd As LongwFunc As LongpFrom As StringpTo As StringfFlags As IntegerfAnyOperationsAborted As LonghNameMappings As LonglpszProgressTitle As LongEnd TypePublic Declare Function SHFileOperation Lib _ shell32.dll Alias SHFileOperationA (lpFileOp As SHFILEOPSTRUCT) As LongPublic Const FO_DELETE = &H3Public Const FOF_ALLOWUNDO = &H40注释:调用Dim SHop As SHFILEOPSTRUCT, strFile as stringWith SHop.wFunc = FO_DELETE.pFrom = strFile + Chr(0).fFlags = FOF_ALLOWUNDOEnd With24、VB中如何使用未安装的字体?Declare Function AddFontResource Lib gdi32 Alias AddFontResourceA (ByVal lpFileName As String) As LongDeclare Function RemoveFontResource Lib gdi32 Alias RemoveFontResourceA (ByVal lpFileName As String) As Long增加字体:Dim lResult As LonglResult = AddFontResource(c:myAppmyFont.ttf)删除字体:Dim lResult As LonglResult = RemoveFontResource(c:myAppmyFont.ttf)MCI播放器在VB中实现用MCI命令来实现多媒体的播放功能下面的内容几乎有播放器软件的各种功能,你只是引用这些函数就能做出一个播放器来Public Declare Function mciSendString Lib winmm.dll Alias mciSendStringA (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As LongPublic Declare Function mciGetDeviceID Lib winmm.dll Alias mciGetDeviceIDA (ByVal lpstrName As String) As LongPublic Declare Function waveOutGetVolume Lib winmm.dll (ByVal uDeviceID As Long, lpdwVolume As Long) As LongPublic Declare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal hwnd As Long, ByVal nIndex As Long) As LongPublic 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 Declare Function SetWindowLong Lib user32 Alias SetWindowLongA (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic Declare Function GetShortPathName Lib kernel32 Alias GetShortPathNameA (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As LongEnum PlayTypeNameFile = 1CDAudio = 2VCD = 3RealPlay = 4End EnumDim PlayType As PlayTypeNameEnum AudioSourceAudioStereo = 0 stereoAudioLeft = 1 leftAudioRight = 2 rightEnd EnumDim hWndMusic As LongDim prevWndproc As Long=打开MCI设备,urlStr为网址,传值代表成功与否=Public Function OpenURL(urlStr As String, Optional hwnd As Long) As BooleanOpenMusic = FalseDim MciCommand As StringDim DriverID As StringCloseMusicMCI命令DriverID = GetDriverID(urlStr)If DriverID = RealPlayer ThenPlayType = RealPlayExit FunctionEnd IfMciCommand = open & urlStr & type & DriverID & alias NOWMUSICIf DriverID = AVIVideo Or DriverID = MPEGVideo Or DriverID = MPEGVideo2 ThenIf hwnd 0 ThenMciCommand = MciCommand + parent & hwnd & style childhWndMusic = GetWindowHandleprevWndproc = GetWindowLong(hWndMusic, -4)SetWindowLong hWndMusic, -4, AddressOf WndProcElseMciCommand = MciCommand + style overlapped End IfEnd IfRefInt = mciSendString(MciCommand, vbNull, 0, 0)mciSendString set NOWMUSIC time format milliseconds, vbNullString, 0, 0If RefInt = 0 Then OpenMusic = TrueEnd Function=打开MCI设备,FILENAME为文件名,传值代表成功与否=Public Function OpenMusic(FileName As String, Optional hwnd As Long) As BooleanOpenMusic = FalseDim ShortPathName As String * 255Dim RefShortName As StringDim RefInt As LongDim MciCommand As StringDim DriverID As StringCloseMusic获取短文件名GetShortPathName FileName, ShortPathName, 255RefShortName = Left(ShortPathName, InStr(1, ShortPathName, Chr(0) - 1)MCI命令DriverID = GetDriverID(RefShortName)If DriverID = RealPlayer ThenPlayType = RealPlayExit FunctionEnd IfMciCommand = open & RefShortName & type & DriverID & alias NOWMUSICIf DriverID = AVIVideo Or DriverID = MPEGVideo Or DriverID = MPEGVideo2 ThenIf hwnd 0 ThenMciCommand = MciCommand + parent & hwnd & style childhWndMusic = GetWindowHandleprevWndproc = GetWindowLong(hWndMusic, -4)SetWindowLong hWndMusic, -4, AddressOf WndProcElseMciCommand = MciCommand + style overlapped End IfEnd IfRefInt = mciSendString(MciCommand, vbNull, 0, 0)mciSendString set NOWMUSIC time format milliseconds, vbNullString, 0, 0If RefInt = 0 Then OpenMusic = TrueEnd FunctionFunction WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongIf Msg = &H202 ThenMsgBox OKEnd IfWndProc = CallWindowProc(prevWndproc, hwnd, Msg, wParam, lParam)End Function=根据文件名,确定设备=Public Function GetDriverID(ff As String) As StringSelect Case UCase(Right(ff, 3)Case MID, RMI, IDIGetDriverID = SequencerCase WAVGetDriverID = WaveaudioCase ASF, ASX, IVF, LSF, LSX, P2V, WAX, WVX, .WM, WMA, WMX, WMPGetDriverID = MPEGVideo2Case .RM, RAM, .RAGetDriverID = RealPlayerCase ElseGetDriverID = MPEGVideoEnd SelectEnd Function=播放文件=Public Function PlayMusic() As BooleanDim RefInt As LongPlayMusic = FalseRefInt = mciSendString(play NOWMUSIC, vbNull, 0, 0)If RefInt = 0 Then PlayMusic = TrueEnd Function=获取媒体的长度=Public Function GetMusicLength() As LongDim RefStr As String * 80mciSendString status NOWMUSIC length, RefStr, 80, 0GetMusicLength = Val(RefStr)End Function=获取当前播放进度=Public Function GetMusicPos() As LongDim RefStr As String * 80mciSendString status NOWMUSIC position, RefStr, 80, 0GetMusicPos = Val(RefStr)End Function=获取媒体的当前进度=Public Function SetMusicPos(Position As Long) As BooleanDim RefInt As LongSetMusicPos = FalseRefInt = mciSendString(seek NOWMUSIC to & Position, vbNull, 0, 0)If RefInt = 0 Then SetMusicPos = TrueEnd Function=暂停播放=Public Function PauseMusic() As BooleanDim RefInt As LongPauseMusic = FalseRefInt = mciSendString(pause NOWMUSIC, vbNull, 0, 0)If RefInt = 0 Then PauseMusic = TrueEnd Funct
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论