大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> 程序开发 -> VB编程的必备技巧

VB编程的必备技巧

时间: 2021-07-31 作者:daque

搜集梦想狂[oicq:8762220]   对编制程序者来说,vb很简单上手,但要深刻、精巧地控制它还要下一番工夫。笔者在这边引见几个较为典范的编制程序本领,蓄意能对宏大vb喜好者有所扶助。 一.还好吗创造自设置的光标 当在安排一个运用步调时,visual basic承诺步调员将很多控件的mousepointer属性安排成12个预先设置好的鼠标光标之一。但是,有些步调员大概会蓄意表露一个预订义形势除外的光标。正文证明怎样创造一个各别的鼠标南针(光标),囊括为一个没有mousepointer属性的控件创造光标。 要在visual basic运用步调中将光标(鼠标南针)改形成个各别的形势,不妨增添代码来变换蓄意监督的控件的mousemove和dragover事变。 mousemove事变所包括的代码用来触发该控件的drag本领。当鼠标在被选中的控件上挪动的功夫,轮番表露新的鼠标南针。 当鼠标南针摆脱该控件时,则dragover事变被触发。在visual basic步调中,不妨从新复位此drag属性,再不往日的鼠标南针不妨被再次表露出来。 底下的样例步调实行当鼠标南针挪动到一个文献列表框控件上时,将其改形成各别的形势。 开始沿用缺省的本领创造form1。在form1上增添一个文献列表框控件,沿用缺省的本领创造file1。将file1控件的dragicon属性树立为所采用的.ico文献。 将如次代码增添到file1的mousemove事变中: private sub file1—mousemove(buttonas integer, shift as integer, x as single, y as single) file1.drag 1 'icon on end sub 将如次代码增添到form1的dragover事变中: private sub form—dragover(source as control, x as single, y as single, state as integer) file1.drag 0 ′icon off end sub 按下f5功效键实行此步调。获得的运转截止是:将鼠标南针挪动到该文献列表框控件上时,步调将把所选中的.ico文献动作缺省的鼠标光标;将鼠标南针摆脱该控件时,光标则会机动回复为缺省的形势。 二.还好吗在窗体上点鼠标右键爆发一个弹出式选单(popup menu)? 大师都领会,在windows95/98/2000的桌面和很多时髦软硬件的视窗中,当咱们点鼠标右键时,会在鼠目标暂时场所弹出一个赶快选单。很多喜好编制程序的伙伴是否也蓄意能在本人的步调里有一致的功效呢?本来,这并不艰巨。笔者过程一番全力,在vb下找到一个通用的本领,供大师瓜分。 要实行上述功效,需分两个办法: 1. 运用vb的选单编纂器(menu editor)编纂你蓄意弹出的选单及子选单,提防,要将选单的visible属性树立为:false。 2. 在窗体(form1)的mousedown事变中编写步调,来激励编纂好的选单,假如选单名为popmenu,步调源码如次: private sub form—mousedown(button as integer, shift as integer, x as single, y as single) if button = vbrightbutton then popmenu.visible = true popupmenu popmenu end if end sub 上述本领是对准窗体的,咱们也不妨对准大肆控件,用鼠标右键点击控件时,也弹出一个赶快选单。本领也很大略,只有把上述代码放到相映控件的mousedown事变中,就不妨了。 三.还好吗动静地在窗体上确定某地区内能否有控件生存? 在笔者的一个小步调中,想在窗体的某个地区输入数据,这就诉求在这个地区内不许有其余控件生存,那么,如何本领领会在窗体的某个地区内,能否有控件生存呢? 为了确定在窗体的某个地区中,能否含有控件,咱们不妨运用以次vb步调来实行: function getcontrol(x1 as single, y1 as single, x2 as single, y2 as single) as control dim control as control for each control in form1 with control if (x1 〈= .left) and (x2 〉= .left) and _ (y1 〈= .top) and (y2 〉= .top) or _ (x1 〈=.left + width) and (x2 〉= .left + width) and _ (y1 〈= .top) and (y2 〉= .top) or _ (x1 〈= .left) and (x2 〉= left) and _ (y1 〈= .top + height) and (y2 〉= .top + height) or _ (x1 〈= . left + width) and (x2 〉= .left + width) and _ (y1 〈= .top + height) and (y2 〉= .top + height) then set getcontrol = control exit function end if end with next set getcontrol = nothing end function 注:(x1, y1)和(x2, y2)辨别为选定矩形地区的左上角和右下角点的坐标值。 该步调经过计划窗体上一切控件的四个角的场所来确定控件能否与选定地区订交,并归来订交的控件。 四.获得和窜改计划机名字的本领 在win 95/98/2000中,计划机有一个名字。运转regedit,在“hkey-local-machine\system\currentcontrolset\control\computername\computername”中将创造“computername”=“default”( 或其它字符串),在regedit下不妨察看和窜改这个名字。咱们还可在步调中经过win32api供给的getcomputername、setcomputername这两个因变量来察看和窜改计划机的名字。底下以vb为例来商量怎样编写一个可察看和窜改计划机名字的步调。 1.插入一个新模块,在个中增添如次代码: ′证明 getcomputername declare function getcomputername lib″kernel 32″alias″getcomputernamea″(byval lpbuffer as string,nsize as long)as long ′证明 setcomputername declare function setcomputername lib″kernel 32″alias ″setcomputernamea″(byval lp computername as string)as long ′设置一个获得计划机名字的因变量 public function getcname (cname) as boolean dim scomputername as string '计划机的名字 dim lcomputername as long '计划机名字的长度 dim lresult as long 'getcomputername的归来值 dim rv as boolean ′getcname归来值,若为true则表白操纵胜利 lcomputernamelen=256 scomputername=space (lcomputernamelen) lresult=getcomputername (scomputername,lcompputernamelen) if lresult 〈〉0 then cname=left$ (scomputername,lcomputernamelen) rv=true else rv=false end if getcname=rv end function ′设置一个窜改计划机名字的因变量 public function setcname (cname ) as boolean dim lresult as long dim rv as boolean lresult=setcomputername (cname) if lresult 〈〉0 then rv=true′窜改胜利 else rv=false end if setcname=rv end function 2.在窗体中增添一吩咐按钮command1,双击该按钮并在个中增添如次代码: sub command1-click () dim cn as string x=getcname (cn) print ″this computer name is :″,cn cn=″mycomputer″ x=setcname (cn ) print ″now the computer name is :″,cn end sub ok, 生存上述树立和代码,而后按f5运转该步调。 五.给vb控件picturebox加震动条的本领 用过picturebox控件的伙伴都领会,在个中咱们不妨加载图片。当图片不是很大时,大概还不会有什么题目,然而,即使所加载的图片比picturebox大时,咱们只能看到图片的一局部,那么,如何本领看到其余的局部呢? 为领会决上述题目,咱们不妨在图片框(picturebox)里面加上行宽厚笔直震动条,运用震动条来表露看得见的图片。简直本领如次: 开始给工程(project)增添一个ocx控件,单击选单上的工程(project)选单项,在弹出的下拉选单中式点心击组件(components),选中个中的“microsoft common dialog control 5.0”,决定实行加载处事;而后画一个picturebox,沿用vb供给的默许名字picture1, 再在picture1上头画一个picturebox,默许名字为picture2,提防别忘了树立:picture2.autosize=true;接着,加上行宽厚笔直震动条,默许名字辨别为:hscroll1,vscroll1;此后加载图形到picture2上,就不妨了;结果,在窗体中引入其它控件:一个按钮(command),默许名为command1和一个“microsoft common dialog control”,默许名为commondialog1。简直vb代码如次: private sub form—load() picture2.left = 0 picture2.top = 0 picture2.width = picture1.width picture2.height = picture1.height vscroll1.min = 0 hscroll1.min = 0 hscroll1.min = 0 vscroll1.max = picture2.height - picture1.height hscroll1.max = picture2.width - picture1.width if hscroll1.max 〈 0 then hscroll1.enabled = false if vscroll1.max 〈 0 then vscroll1.enabled = false end sub private sub command—click() on error goto errexit commondialog1.filter = "bitmap file(*.bmp)|*.bmp|all file(*.*)|*.*" commondialog1.filterindex = 1 commondialog1.showopen picture2.picture = loadpicture(commondialog1.filename) vscroll1.min = 0 hscroll1.min = 0 vscroll1.max = picture2.height - picture1.height hscroll1.max = picture2.width - picture1.width if hscroll1.max 〈 0 then hscroll1.enabled = false if vscroll1.max 〈 0 then vscroll1.enabled = false errexit: end sub private sub hscroll1—change() picture2.left = -hscroll1.value end sub private sub vscroll1—change() picture2.top = -vscroll1.value end sub 该步调经过点击command1按钮,在弹出的对话框中采用一个图形文献加载到picture第22中学,运用水宽厚笔直震动条就不妨实行图片的震动。 六.用vb做谈天步调的本领 所谓“谈天”是指两个步调不妨发送数据给对方。这个步调波及到数据通信的常识,似乎很搀杂,然而,因为vb给咱们供给了一个winsock控件,题目就变得很大略了。 开始编写“谈天(长机)”步调。在窗体里增添winsock控件,并树立其protocol属性为1-sckudpprotocol,其余属性为缺省值。接着增添两个标签和两个文本框,树立两个标签的题目属性辨别为“接受窗”和“发送窗”;两个文本框的题目属性为空。结果编写代码: 1.“谈天(长机)” private sub form—load() ′树立搜集地方 winsock1.localport=1024 winsock1.remotehost="202.96.6.1" winsock1.remoteport=1999 end sub private sub text1—change() ′发送用户输出的实质 winsock1.senddata text1.text end sub private sub winsock1—dataarrival(byval bytestotal as long) dim rec as string ′接受对方数据并在文本框内表露 winsock1.getdata rec, vb string text2.text=rec end sub 2.“谈天(副机)” private sub form_load() ′树立搜集地方 winsock1.localport=1999 winsock1.remotehost="202.96.6.1" winsock1.remoteport=1024 其余局部步调与(长机)沟通。结果将两个步调存盘,并编写翻译成实行(.exe)文献。此刻就不妨运用这个步调举行对话了。 七.文本框华文本的某一一定字符或字符串同声高亮表露的本领 因为普遍textbox控件不扶助不贯串字符串的同声高亮表露,以是咱们采用richtextbox控件。单击工程(project)选单项,在弹出的下拉选单中单击组件(components)选单项,从弹出的对话框中采用microsoft rich textbox control 5.0复选框,决定加载richtextbox控件。 兴建(new)一个工程,在窗体(form)上增添一个richtextbox控件和两个command(按钮)控件,都沿用体例默许的name属性值;树立richtextbox的text属性值为空,command1和command2的caption属性值辨别设为“输出文本”和“采用字符串”。结果,增添如次vb代码: private sub command1—click() dim str as string dim text as string str=″输出文本″ text=inputbox(str) richtextbox1.text=text end sub private sub command2—click() dim str as string dim text as string dim position as integer dim lenth as integer str=″输出要高亮表露的字符串″ text=inputbox(str) if text 〈〉 ″″ then position=instr(richtextbox1.text, text)-1 lenth=len(text) richtextbox1.selstart=position richtextbox1.sellength=lenth richtextbox1.selcolor=rgb(255,0,0) do while instr(position+lenth+1, richtextbox1.text, text) 〈〉 0 position=instr(position+lenth+1, richtextbox1.text, text)-1 richtextbox1.selstart=position richtextbox1.sellength=lenth richtextbox1.selcolor=rgb(255,0,0) loop end if end sub 按f5执路途序,单击“输出文本”按钮,在弹出的对话框中输出少许文本,决定后,方才输出的文本将表露在richtextbox中;再单击“采用字符串”按钮,在弹出的对话框中输出你蓄意高亮表露的字符串,决定。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.