We can add a number of different controls, such as text box controls, to a form on visual Basic. During design time, you can resize and position the control. Text describes how to modify the dimensions of these controls at run time from a visual Basic application.
Resize a control at run time----when you design a visual Basic application, you can simply add the control to the form as needed, such as a text box control that provides the program with a The characteristics of a word processor with minimal functionality.
The dimensions of the----control must be set during design time. However, by using two windows API functions: GetWindowLong and SetWindowLong, we can implement changing a control at run time, such as the size of a text box control 。
----When you add a control, such as a text box control, to a visual Basic application, you are essentially creating a new window. Each window created in the Windows operating system has a style attribute associated with it, for example, a text box control might have a Es_multiline window style. This tells Windows that the control is a multiline edit control.
----Typically, a text box control cannot be resized at run time. However, by changing the style properties of the control, users will be able to adjust the physical dimensions of the text box while the program is running.
----This can be done by calling the GetWindowLong and SetWindowLong functions. First, call the GetWindowLong function to extract the current window style properties of the text box control. Second, you can use the or bitwise operator to set the Ws_thickframe property for the text box control. A window with the Ws_thickframe property draws a bold box over its bounds. You can use this border to change the size of the window.
----then run the SetWindowLong function, which tells Windows to modify the style properties of the text box control.
----Finally, fix the most recent size of the text box so that its new location and size are registered in the form under it, and you can call the SetWindowPos function to do this work.
Sample program----The program shows how to create a variable-size text box control at run time in Visual Basic.
----1. Start a new project in Visual Basic and use the default method to establish Form1.
----2. Add the following constants and declaration statements to the Common Declarations section of FORM1 (note that each declaration statement needs to be written in one line):
----Private Declare Function getwindowlong Lib "User" (ByVal hWnd As Integer, ByVal nindex as Integer) as Long
----Private Declare Function setwindowlong Lib "User" (ByVal hWnd As Integer, ByVal nindex As Integer, ByVal Dwnewlong as Long) as Long
----Private Declare Sub setwindowpos Lib "User" (ByVal hWnd As Integer, ByVal hwndinsertafter As Integer, ByVal X as inte GER, ByVal Y As Integer, ByVal CX As Integer, ByVal CY As Integer, ByVal wflags As Integer
----Const Swp_nosize = &h1
----Const Swp_nozorder = &h4
----Const Swp_nomove = &H2
----Const Swp_drawframe = &h20
----Const Gwl_style = (-16)
----Const Ws_thickframe = &h40000
----for users who use Visual Basic 5.0 in 32-bit environments, you need to add the following declaration statements to the General Declarations section of FORM1 (note that each declaration statement needs to be written in a In-line):
----Private Declare Function getwindowlong Lib "user32" Alias "Getwindowlonga" (ByVal hwnd as Long, ByVal nindex as Long) As Long
----Private Declare Function setwindowlong Lib "user32" Alias "Setwindowlonga" (ByVal hwnd as Long, ByVal nindex as Long, ByVal Dwnewlong as Long) as long
----Private Declare Function setwindowpos Lib "user32" (ByVal hwnd as Long, ByVal hwndinsertafter as Long, ByVal x as Lon G, ByVal y as Long, ByVal CX as Long, ByVal Cy as Long, ByVal wflags as long) as long
----Const Swp_nosize = &h1
----Const Swp_nozorder = &h4
----Const Swp_nomove = &H2
----Const Swp_drawframe = &h20
----Const Gwl_style = (-16)
----Const Ws_thickframe = &h40000
----3. Add a command button control to the Form1 and use the default method to establish the COMAND1. Set its Caption property to "Change the size of the text box control"
----4. Add the following code to the Command1 click event:
----Private Sub Command1_Click ()
----Resizecontrol Text1, Form1
----End Sub
----5. Add a text box control to the Form1, using the default method to establish the TEXT1.
----6. Create a new function named Resizecontrol, and add the following code to the function:
----Sub Resizecontrol (controlname as control, FormName as Form)
----Dim NewStyle as Long
----NewStyle = GetWindowLong (Controlname.hwnd, Gwl_style)
----NewStyle = NewStyle Or ws_thickframe
----NewStyle = SetWindowLong (Text1.hwnd, Gwl_style, NewStyle)
----SetWindowPos Controlname.hwnd, Formname.hwnd, 0, 0, 0, 0, swp_nozorder or swp_nosize or Swp_nomove or swp_drawframe
----End Sub
----Press F5 to run the sample program, click the command button to change the size of the text box as needed.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.