Essential techniques for VB programming

zhaozj2021-02-08  258

---- For the programmer, VB is easy to get started, but it is necessary to deepen, flexibly control it. The author introduces several more typical programming skills, hoping to help vb enthusiasts.

---- I. How to create a custom cursor

---- 1. When you design an application, Visual Basic allows programmers to design one of the MousePointer properties of many controls into one of the pre-defined mouse cursors. However, some programmers may want to display a cursor other than a predefined shape. This article shows how to create a different mouse pointer (cursor), including creating a cursor for a control without a mousepoint attribute.

---- To change the cursor (mouse pointer) into a different shape in the Visual Basic application, you can add code to change the Mousemove and Dragover events of the control you want to monitor.

---- The code included in the MouseMove event is used to trigger the DRAG method of the control. When the mouse moves on the selected control, the new mouse pointer is displayed. When the mouse pointer leaves the control, the Dragover event is triggered. In the Visual Basic program, this DRAG property can be reset so that the previous mouse pointer can be displayed again.

---- 2. The following sample program implements the mouse pointer to a file list box control, change it into a different shape.

---- First use the default method to establish FORM1. Add a file list box control on Form1 to establish file1 by default. Set the Dragicon property of the File1 control to the selected .ico file.

---- Add the following code to the MouseMove event of File1:

Private Sub File1-Mousemove (Buttonas)

Integer, Shift As Integer, x as single, y as single)

File1.drag 1 'icon on

End Sub

---- Add the following code to the Dragover event of Form1:

Private Sub Form-Dragover (Source As Control,

X as single, y as single, state as integer

File1.drag 0 'icon OFF

End Sub

---- Press the F5 function key to execute this program. The result of the result is: When moving the mouse pointer to the list box control, the program will use the selected .ico file as the default mouse cursor; when the mouse pointer leaves the control, the cursor will automatically restore it. The shape of the province.

---- II. How do I right-click a popup menu?

---- Everyone knows that in the Windows95 / 98/2000 desktop and many popular software's window, when we click the right mouse button, a shortcut menu will pop up in the current location of the mouse. Many friends who have programs are also hoped to have similar features in their own programs? In fact, this is not difficult. After some effort, I find a general method under VB for everyone to share.

---- To achieve the above functions, you need to divide two steps:

---- 1. Edit the menu and sub-menu you want to pop up with the VB menu editor (Menu Editor), pay attention to setting the Visible property of the menu to: false.

---- 2. Write a program in the MouseDown event of the form (Form1) to inspire the editing menu, assume that the order is selected as POPMENU, the program source code is as follows:

Private Sub Form-MouseDown

(Button as integer, shift as integer, x askLE, Y askLE)

If Button = Vbrightbutton Then

PopMenu.visible = TRUE

Popupmenu Popmenu

END IF

End Sub

---- The above method is for the form, and we can also use the mouse to right click on the control for any control, and a shortcut menu will also pop up. The method is also very simple, as long as the above code is placed in the mousedown event of the corresponding control, it can be.

---- III. How to dynamically determine if there is a control in a region?

---- In a small program of the author, I want to output data in a region of the form, which requires that there is no other control in this area, then, how can I know in an area of ​​the form, Is there any control existence?

---- In order to determine whether or not to include a control, we can use the following VB program to implement:

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

---- Note: (x1, y1) and (x2, y2) are the coordinate values ​​of the left upper left corner and the lower right angle point of the selected rectangular region, respectively.

---- This program determines whether the control intersects the control with the selected area by calculating the position of all controls on the form, and returns the intersecting control.

---- IV. Get and modify the computer name

---- In WIN 95/98/2000, the computer has a name. Running regedit, will discover "ComputerName" = "default" (or other string) in "HKEY-local-machine / system / computername / computename", you can view and modify this name under Regedit. We can also view and modify the name of the computer through the GetComputername, SetComputername, which is available in the program through Win32API. The following is VB as an example to explore how to write a program that can view and modify the computer name. ---- 1. Insert a new module, add the following code:

'Declaration getcomputername

Declare function getcomputername lib "kernel 32" alias "

GetComputernamea "(Byval LPBuffer AS)

String, NSIZE AS Long) As Long

'Declaration setcomputername

Declare function setcomputername lib "kernel 32" alias "

SetComputernamea (Byval LP Computername As String) As Long

'Define a function that gets the computer name

Public function getcname (cname) as boolean

DIM SCOMPUTERNAME AS STRING 'computer name

DIM LCOMPUTERNAME AS Long

'The length of the computer name

DIM LRESULT AS Long

'Return value of' getcomputername

DIM RV As Boolean

'Getcname returns a value, if true, the operation is successful

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

'Define a function that modifies the computer name

Public function setcname (CNAME) as boolean

DIM LRESULT AS Long

DIM RV As Boolean

LResult = setcomputername (CNAME)

IF LRESULT <> 0 THEN

RV = true 'modification success

Else RV = FALSE

END IF

Setcname = rv

END FUNCTION

---- 2. Add a command button Command1 in the form, double-click the button and add the following code:

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, save the above settings and code, then press F5 to run the program.

---- Five. Method for the VB control Picturebox plus roll bar ---- Friends who have used the PictureBox control know, where we can load pictures. When the picture is not very big, there may be no problem, but if the picture loaded is bigger than Picturebox, we can only see a part of the picture, then how can you see other parts? In order to solve the above problem, we can add horizontal and vertical scroll bars inside the picture box (PictureBox), using scroll bars to display images. The specific method is as follows:

---- First, add an OCX control to the project, click the Project option item on the menu, click Components in the pop-up order, select "Microsoft Common Dialog Control 5.0", Determine the load work; then draw a PictureBox, use the default name Picture1 provided by VB, then draw a PictureBox on Picture1, the default name is Picture2, pay attention to the setting: Picture2.autosize = true; then, plus horizontal and vertical The scroll bar, the default name is: hscroll1, vscroll1; after loading graphics to Picture2, it is possible; finally, other controls are introduced in the form: a button (Command), the default is Command1 and a "Microsoft Common Dialog Control ", The default is CommonDialog1. The specific VB code is as follows:

Private sub flow-loading ()

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 & brvbarel file (*. *) | *. *"

CommonDialog1.filterIndex = 1

CommonDialog1.showopen

Picture2.Picture = loadingPicture (Commondialog1.FileName)

Vscroll1.min = 0

HScroll1.min = 0

Vscroll1.max = Picture2.Height - Picture1.height

HScroll1.max = Picture2.Width - Picture1.Widthif 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

---- This program selects a graphical file to Picture2 in the pop-up dialog box by clicking on the Command1 button, using the horizontal and vertical scroll bars to implement the scroll.

---- 6. Method for chatting with VB

---- The so-called "chat" means that the two programs can send data to each other. This program involves the knowledge of data communications, as if it is very complicated, but because VB gives us a Winsock control, the problem is very simple.

---- Write "Chat (Host)" program first. Add a Winsock control in the form and set its protocol attribute to 1-SckUDPPROTOCOL, and other properties are default. Then add two tags and two text boxes, set the title properties of the two tags to "Receive Windows" and "Send Windows"; the title attribute of the two text boxes is empty. Finally write code:

---- 1. "Chat (Host)"

Private sub flow-loading ()

'Setting up the network address

Winsock1.localport = 1024

Winsock1.Remotehost = "202.96.6.1"

Winsock1.remoteport = 1999

End Sub

Private sub text1-change ()

'Send the content entered by the user

Winsock1.senddata text1.text

End Sub

Private Sub Winsock1-DataArrival

(Byval Bytestotal As Long)

Dim Rec as string

'Receive other data and display in text box

Winsock1.Getdata REC, VB String

TEXT2.TEXT = Rec

End Sub

---- 2. "Chat (Depart)"

Private sub flow_load ()

'Setting up the network address

Winsock1.localport = 1999

Winsock1.Remotehost = "202.96.6.1"

Winsock1.remoteport = 1024

---- Other partial procedures are the same as (host). Finally, two programs are stored, and compiled into an execution (.exe) file. You can use this program to make a conversation.

---- Seven. Methods of a particular character or string of text in text box

---- We select the RichTextBox control because the normal TextBox control does not support discontinuous strings. Click the Project to select a single item, click the component option in the pop-up order, select the Microsoft Rich TextBox Control 5.0 check box from the pop-up dialog box, and determine the load RichTextBox control. ---- New (New) a project, add a RichTextBox control and two Command controls on the form (FORM), all use the system default Name property value; set the value of RichtextBox's text attribute, Command1 And the CAPTION attribute value of Command2 is set to "Enter Text" and "Select String". Finally, add the following VB code:

Private subss1-click ()

Dim Str As String

Dim text as string

Str = "Enter Text"

Text = InputBox (STR)

RichtextBox1.text = text

End Sub

Private sub urns2-click ()

Dim Str As String

Dim text as string

DIM Position as in

DIM LENTH AS INTEGER

Str = "Enter the string to be highlighted"

Text = InputBox (STR)

IF text <> "" "

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

---- Press F5 to execute the program, click the "Enter Text" button, enter some text in the pop-up dialog, after confirmation, the text just entered will be displayed in RichTextBox; click the "Select String" button. In the pop-up dialog box, enter the string you want to highlight. After confirming, the corresponding string in RichTextBox will be highlighted in red.

---- 8. Programming Implementation of Windows 95/98 operating system thermal start-up

---- To use the program to realize the restart of the system, you can call the API function in your program. Build a subunies: (take VB as an example)

Declare function systemparametersInfo lib "

User32 "Alias ​​-

"SystemParametersInfo" (ByVal UAS Long,

Byval Uparam As Long, Byval LPVParam as Any, ByVal

Fuwinini as long?

Sub DisableCtrlaltdelete (bdisabled as boolean)

DIM X as long

X = SystemParametersInfo (97, BDISABED, CSTR (1), 0)

End Sub

Call Disablectrlaltdelete (TRUE) 'Prohibition of heat

Call Disablectrlatdlete (false) Allows hot

---- Nine. Methods of automatically launching procedures after Windows 95/98

---- We all know that there is a "start" option in the "Start" → "Program" menu of Windows 95/98. When Windows 95 or Windows 98 is started, the system will automatically start placing the "start" menu. The executable program in the column.

---- Now there are many software, like a software, ICQ, and most real-time detection of viruses, etc. After installation, it is not placed in the "start" menu, and can automatically start the operating system start up. How to achieve it?

---- In fact, just know some of the knowledge of the Windows registry, this problem cannot be called a problem. Click Start with the mouse, open the start menu, then click "Run", then enter "regedit", then open the system registry editor, find hkey-local-machine? Software? Microsoft? Windows? Currentversion? Run, join your program's entry, you can. If you don't know how to add, refer to the key value already existing.

---- Ten. How do I output data files into the Text control? If the amount of data is relatively large, the form is full screen is not big enough, how to solve it?

---- There is a relatively simple method, putting data into a text box (text) and plus horizontal and vertical scroll bars. The specific implementation step is: first add a text box in the form (Form), use the default name Text1; then set the properties of text box text1: Text property setting is empty, multiline property is set to true, the scrollbars property is set to 3- Both; then add the following VB code:

Private sub flow-loading ()

DIM HANDLE AS INTEGER

DIM FileName As String

ON Error Goto Errexit

Begin:

'Enter the name of the data file you want to display

Filename = InputBox $ ("Input FileName",

"Open file")

ON Error Goto Fileerr

Handle = freefile

Open filename for Input as #hannelle

'Output data in the data file into the text box

Text1.text = INPUT $ (Lof (Handle), Handle

Close #handle

EXIT SUB

FILERR:

Dim errnum as in

IF err.number = 53 THEN

Errnum = msgbox ("File Not Exist",

VBOKCANCEL, "Error Information") if errnum = 1 THEN

Goto Begin

Else

EXIT SUB

END IF

END IF

MsgBox Err.Description,, "File Open Failed"

Errexit:

EXIT SUB

End Sub

'Make the text box full of the entire form

Private sub flow-resize ()

Text1.left = 0

TEXT1.TOP = 0

TEXT1.WIDTH = Form1.Width-100

Text1.height = Form1.Height-400

End Sub

---- Through this process, not only solve the problem, but the user can edit the data in the text box.

--- Eleven. Related File List Box, Directory List Box, Drive List Box

---- I want to be a dialogue form, contain the drive list box, the directory list box, and the file list box, and can implement the synchronization operation of the three, how to do it? This is what we often encounter in practical applications. It is very simple to solve this problem in VB, which can be implemented by changing the Change event through the change of the PATH attribute. E.g:

SUB Dir1-change ()

File1.path = dir1.path

End Sub

---- This event process causes synchronization of the directory list box DIR1 and file list box file1 on the form. Because the change of the directory list box Path property will generate a Change event, in the Dir1-Change event process, the synchronization effect can be generated in the Dir1.Path. Similarly, add the following event procedure, you can synchronize three list boxes:

Sub drive1-change ()

Dir1.path = drive1.drive

End Sub

---- This process synchronizes the driver list box and directory list box, the previous process synchronizes the directory list box and file list box, so that three list boxes are synchronized, and the problem can be resolved.

Huang Liwei (Beijing West Railway Station)

转载请注明原文地址:https://www.9cbs.com/read-2356.html

New Post(0)