VB编程的必备技巧(3) 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
----首先给工程(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)