Form Control special effects code collection in Visual basic

Source: Internet
Author: User
Tags constant advantage
visual| controls how to drag a form or control in Visual Basic
Many of today's Windows applications use a graphical interface, such as Winamp! The advantage of this is that you can make the program interface more beautiful and lively and more attractive. But in such an interface can not use the original Windows title bar, otherwise it will affect the aesthetics of the interface. So how do you drag a form with your mouse without a title bar? Or are you dragging other controls?
In fact, the use of API function call is easy to implement, the following we take the Drag form (FORM1) as an example to specifically describe the implementation method:
1. Create a new project, the name is: Test.vbp.
2. Fill in a module with the name: Test.bas, and add the following code in the Declaration section:
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 Lo Ng, LParam as any) as Long
Public Const Wm_syscommand = &h112
Public Const Sc_move = &hf012
Where the ReleaseCapture function is used to release mouse capture, the SendMessage function is a very important function in WindowsAPI, where it is used to send messages to Windows that move the form. Wm_syscommand is a constant that sends messages to Windows. Sc_move is the constant that controls the mobile form. Please note: Here the Sc_move assignment must be &hf012, which represents the mouse object.
3. Add the following code to the MouseDown process of the form:
ReleaseCapture
SendMessage Form1.hwnd, Wm_syscommand, Sc_move, 0
Where the ReleaseCapture function is used to release mouse capture, the SendMessage function sends a message to Windows to move the form.
4. Save and run the program, press the mouse, you will find that the form will move with the mouse movement. Just like any other famous application.
The above describes how to use the mouse in VB drag the form, in fact, the control of the principle of drag is the same.
For example, the following code can drag the picture box:
ReleaseCapture
SendMessage Picture1.hwnd, Wm_syscommand, Sc_move, 0
In fact, using API functions can achieve many of your unexpected functions. If you need more information about VB and other experience and skills. It is recommended that you visit the http://vb2000.kstar.com site.


Drag a window without a system standard title bar
Wu
As you know, in VB you can set the properties of the form to make a window without the system title bar. However, because of the loss of the system title bar, how to use the mouse to drag the window becomes a thorny issue. In fact, with the help of API functions ReleaseCapture and SendMessage, this problem can be solved.
First, add the following declaration statement to the module file:
Declare Sub releasecapture Lib "User" ()
Declare Function sendmessage Lib "User" (ByVal hWnd _
As Integer,byval wmsg as Integer,byval WParam as integer,_lparam as any) as Long
Public Const wm_syscommand=&h112
Public Const sc_move=&hf010
Public Const htcaption=2
Then, add the following code to the MouseDown event in the form:
ReleaseCapture
Ret&=sendmessage (Me. hwnd,wm_syscommand,_sc_move+htcaption,0)
......
When the program is running, you can drag the window as soon as the cursor falls in the form area while holding down the left mouse button. In the design of programs that require a lively interface, developers often want a unique title bar in their own style to meet the requirements of the entire interface. By this method, you can make homemade title bar to achieve the point of genuine. However, a control that is used as a homemade title bar must have a MouseDown event to display the above code.


Move a window without a title bar
We generally use the mouse to hold down the title bar of the window, and then move the window, when the window does not have a title bar, we can use the following methods to move the window:
 
In the BAS file, declare:
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 long
Public Const htcaption = 2
Public Const Wm_nclbuttondown = &HA1
 
Then, in the Form_mousedown event:
Private Sub form_mousedown (Button As Integer, Shift As Integer, X as single, Y as single)
ReleaseCapture
SendMessage hwnd, Wm_nclbuttondown, Htcaption, 0&
End Sub


The control technique of form in VB programming
In Visual Basic, each application starts with a form (form), which is a container for all the controls that make up the user interface. By using the various properties and operations of the form, the user can design a pleasing operation interface.
1. The form is displayed full screen with the current screen resolution
During the debugging of the application, used for accessibility tools such as menus, toolbars, and toolbars, which are generally not set to full screen for easy operation, and when the screen resolution changes, the form may not display correctly, and the following code can be inserted into the form's Load event. Makes the form appear full screen at the current screen resolution when it starts:
Private Sub Form_Load ()
Form1.top=0 ' Sets the position of the border on the form
Form1.left=0 ' Sets the left edge position of the form
Form1.width=screen.width ' Sets the width of the form to screen width
Form1.height=screen.height ' Set form height to screen height
End Sub
2. Keep the form on top
Sometimes an application needs a message or the total of the query window to remain at the top, that is, using reading to switch to other forms, you can see the form, such as the Find form in Microsoft Word, which is implemented by calling Windows API function SetWindowPos (), as shown in the following example:
Option Explicit
Private Declare Function setwindowpos Lib "User32" (Byvalhwnd as Long,byval H Wndinsertafter as Long,byval x as Long,byval y As Long,byval CX as long,byval Cy as Long,byval wflags as Long
Const Hwnd_topmost=-1
Const swp_showwindow=&h40
Private Sub Form_Load ()
Dim RetValue as Long
Retvalue=setwindowpos (Me.hwnd,hwnd_topmost,me.currentx,me.currenty,300,300,swp_showwindow)
End Sub
3. The processing skill of the form loading speed is slow
By default, Visual basic loads and displays the first form of the project. If the form is loaded for more than a second, the user has to wait patiently, especially if the form contains large bitmaps or many controls. In fact, Microsoft has handled this problem well in Office components. The following program demonstrates the loading techniques for handling low-speed forms. When the application starts, start a simple quick load form, display the company logo or other information on it, and then load the slow form back into the quick form and unload the quick form when the slow form is loaded, which solves the waiting problem that bores the user. 
Private Sub Form_Load ()
Form1.show
Form1.refresh
Load Form2 ' Load slow form
Form2.show ' Show Slow form
End Sub
If you omit the Form1.show method, you need to mount a slow form to display the information form. The Refresh method lets Visual Basic update the display and then executes the following command, by default, Visual Basic only has the opportunity to refresh the display without executing other code. After the slow form Form2 loaded, use the following code to unload the quick form FOMR1:
Private Sub Form_Load ()
UnLoad Form1 ' Unload quick form
End Sub
Once the form is loaded, it takes up the resources it wants, so forms that are no longer needed should be unloaded in time to return the resources they occupy to the system. In addition, because the form is loaded slowly, especially if large files that contain bitmaps or other resources are loaded more slowly, you should avoid loading/unloading the form frequently during the application run, preferably loading the form into memory when the application is started, and then displaying it as needed. 


Using VB to control the display style of Windows
Guangzhou Guo Shao Yue
Whether the software interface is vivid or not depends largely on your design and control of the window. In fact, Windows programmers can manipulate any window in the system, which means that Windows programmers can manipulate any running application window directly; You can find out if a particular application is running and you can start it if it's not running; You can rearrange all the windows on the screen ; You can also maximize or minimize the windows of other applications. Here we explore a very meaningful application.
We'd like to keep the cover window in the foreground when the program activates other windows, which means that the window has the feature "Always in front" ("Always on Top"). In fact, Microsoft software is doing this, and when you run Word, Excel, or PowerPoint, you'll find that their cover window doesn't disappear with the system activating other windows.
Visual Basic for Windows (hereinafter referred to as VB) is a visual programming software that is renowned for its excellent graphical interface design style. It can shorten the software development cycle, but the powerful function of VB to the Windows DLL (dynamic link library) is easily ignored by many programmers. In fact, the flexible application of the Windows DLL API (application interface) makes the system you develop more vibrant. It is because the VB support DLL allows us to control the window display style at will, so we can let the system cover is always in the foreground, the following description of the API functions used.
. SetWindowPos
VB definition:
Declare Sub setwindowposlib "User" (Bybal hWnd As Integer, hwndinsertafter as Integer,byval x as Integer,byval y As Integer , ByVal cx As Integer, ByVal cy as Integer,byval wflags as Integer)
Description: Can change the position and size of the window, and can modify the window in the internal window in the list of places,
To control the display order.
Parameter type/description
HWnd integer-window to locate
Hwndinsertafter integer-window handle, in the window list,
The window HWND will be placed behind the window handle, and it can take the following values:
Hwnd_bottom: Place the window at the bottom of the list of Windows;
Hwnd-top: Place the window at the top of the z-order. Z-order is the order in which the window is displayed at the hierarchy level;
Hwnd_top (MOST): Place the window at the top of the list, after all the top windows.
x integer-the new X coordinate of the window, if the HWND is a child window, X gives the client coordinates of the parent window.
Y integer-the new y-coordinate of the window, if the HWND is a child window, Y gives the client coordinates of the parent window.
CX integer-Specifies the new window width.
Cy integer-Specifies the new window height.
Wflags integer-An integer that contains one of the following flags:
Swp_drawframe: Draw a border around the window;
Swp_hidewindow: Hide the window;
Swp_noactivate: Does not activate the window;
Swp_nomove: Keep current position (x and y are ignored);
Swp_noredraw: The window does not automatically repaint;
Swp_nosize: Keep current size (CX and CY are ignored);
Swp_nozorder: keeps the current position in the window list (Hwndinsertafter is ignored);
Swp_showwindow: Displays the window.
Note: When a window becomes the topmost window, all of its subordinate Windows become the topmost window, and when it becomes a non topmost window, all its subordinate windows and owning Windows become non topmost windows, and Z-order represents the imaginary Z-axes that the window extends outwards from the screen, from top to bottom.
The VB implementation window has the "Always on Top" feature:
First, define global constants and functions.
Global Const Hwnd_topmost=-1
Global Const Swp_noactivate=&h10
Global Const swp_showwindow=&h40
Declare Sub setwindowposlib "User" Bybal hWnd as integer,hwndinsertafter as Integer,byval x as Integer,byval y as Integer, ByVal CX as integer,byval Cy as Integer,byval wflags as Integer)
The main program is written as follows:
Sub Main 0
Screen. Mousepointer=11 ' make the mouse into a funnel-like
Load Systemcover ' mount system cover form
SetWindowPos Systemcover. Hwnd,hwnd_topmost,0,0,0,0,swp_noactivate Or Swp_showwindow ' makes the cover always at the front
Load InitWindow1 ' Mount initialization form 1
Load InitWindow2 ' Mount initialization form 2


Unload systemcover ' Close system cover form
Screen. Mousepointer=0
' Make the mouse into the default shape
End Sub


Keep the window on top
A lot of popular software has this option: Always on top. It allows the window to be on top and other Windows cannot overwrite it. In VB, we can use the following methods to achieve:
Private Const swp_nosize = &h1
Private Const swp_nomove = &h2
Private Const Swp_nozorder = &h4
Private Const Swp_noredraw = &h8
Private Const swp_noactivate = &h10
Private Const swp_framechanged = &h20
Private Const Swp_showwindow = &h40
Private Const swp_nocopybits = &h80
Private Const Swp_noownerzorder = &h200
Private Const swp_drawframe = swp_framechanged
Private Const swp_noreposition = Swp_noownerzorder
Private Const hwnd_top = 0
Private Const Hwnd_bottom = 1
Private Const hwnd_topmost =-1
Private Const hwnd_notopmost =-2
Private 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 Mbontop as Boolean
Private Property Let Ontop (Setting as Boolean)
If Setting Then
SetWindowPos hwnd,-1, 0, 0, 0, 0, swp_nomove Or swp_nosize
Else
SetWindowPos hwnd,-2, 0, 0, 0, 0, swp_nomove Or swp_nosize
End If
Mbontop = Setting
End Property
 
Private Property Get Ontop () as Boolean
' Return of the private variable set in
Ontop = Mbontop
End Property
Call Ontop=true to have the window Always on top.


Create a window without Icon
We often need that kind of window without Icon, such as "About ..." "Find" and so on. In VB, we can create this type of window by following these steps:
1, set the window BorderStyle = 3;
2. Add in Form_Load: Me.icon = LoadPicture ("")


Create an irregular window
The Win32 API has a number of features that make you unexpected. It may seem difficult to create a special irregular window. But if we say we can do it with a few lines of code, it seems incredible. But that's the way it is! Please try:
Private Declare Function createellipticrgn Lib "gdi32" (ByVal X1 as Long, ByVal Y1 as Long, ByVal X2 as Long, ByVal Y2 as Long) as Long
Private Declare Function setwindowrgn Lib "user32" (ByVal hWnd as Long, ByVal hrgn as Long, ByVal Bredraw as Boolean) as L Ong
 
Private Sub Form_Load ()
Show ' The form!
SetWindowRgn hWnd, createellipticrgn (0, 0, MB), True
End Sub
The code above can create an oval-shaped window. What do you think??


Use VB5.0 to get the title of the window that is running in the system
Hu Zhiqiang, Dezhou, Shandong
Readers familiar with Visual Basic know that a AppActivate statement is a string of application window title bars that are activated by a quilt by activating an application window and then taking a parameter. If you are activating a common application, we can write the program's window title directly when we write it. For example, we want to activate the WINDOWS95 calculator, run the program X=shell ("Calc.exe", 1), and then activate the calculator window AppActivate "Calculator", then the program focus will automatically go to the Calculator window. However, the simple use of this method has very limited limitations, such as the above example, if we put on the English Windows95, the Calculator window title is not "calculator", but "Calculator". Another limitation is that in many cases we do not know to activate the program window title name, how to compile a program to automatically find the title of the program window is the problem to be solved in this article.
We can take the following steps to get the window title that is running in the system.
There is a function in the WINDOWSAPI:
Get Windows (ByVal hWnd as Long, ByVal wcmd as long) as long
Where the HWND is the current window handle, the Wcmd is a constant associated with the HWND, and has the following meanings:
Wcmd value
Meaning
Gw-ghild
First child window
Gw-hwndfirst
The first sibling window of the child window, its first top-level window
Gw-hwndlast
The last sibling window of a child window, or the last top-level window
Gw-hwndnext
Successor window
Gw-hwndprcv
Previous window
Gw-owner
Window owner
The return value of this function is the handle of the window that Wcmd refers to.
We take advantage of this handle and then use the function get Windowstext (ByVal hwnd as Long, ByVal ipstring as String, ByVal CCH as Long) as long to put the window caption specified by the handle hWnd into a word String variable ipstring, CCH refers to the maximum number of characters placed in the ipstring. Returns the string length when this function succeeds, and returns zero if the window has no caption.
Before you use the Get Windowstext function, you also use the function gets Windowstextlength (ByVal hWnd as long as long) to obtain the length of the HWND's specified window caption, and put it into the CCH.
Make a process findtitle () to find all the headings running in the system, first get the first top-level window handle Currwnd, and then use the while ... Wend loop structure, when the Currwnd is not zero and the title text length is not zero, will get the title into the list box Combo1, and then find the handle of the successor window, when the handle currwnd=0 said there is no successor window, exit the loop. This will find all the window handles and headers in the system.
However, there are a lot of window headers found in this method in debugging, which means that Windows systems run with many hidden windows that we do not need, and that there are errors when activated with AppActivate.
So we're going to make a process sift () to find the active window. The method is to use AppActivate to activate all the windows, the wrong throw, keep the active window title, put in the list box Combo2.
First create a new form form1,caption= "Get window title", create two labels on the form Form1, label1,caption= "All window titles", label2.caption= "active window Titles", and create two Drop-down list boxes. Combo1 holds all the title names in the system, Combo2 the name of the active title, and then creates two command buttons command1.caption= "Activate form" to test the listed form, and the command button command2.caption= "Refresh". Click it to find all form names in the system again, and use this button when the program is run and a new program is run.
From the VB system menu, select Add Module in Project, and the following API functions and some constants are entered.
' Module modules
Declare Function GetWindow Lib "user32" (ByVal hwnd as Long, ByVal wcmd as long) as long
Declare Function getwindowtext Lib "user32" AIAs "Getwindowtexta" (ByVal hwnd as Long, ByVal lpstring as String,byval CCH A s long) as long
Declare Function getwindowtext Length Lib "user32" AIAs "Getwindowtextlengtha" (ByVal hwnd as long) as long
Public Const gw-hwndfirst=0
Public Const Gw-hwndlast=1
Public Const gw-hwndnext=2
Public Const gw-hwndprev=3
Public Const gw-owner=4
Establish two subroutines:
Sub Findtitle ()
' Find all window titles on the desktop
Dim Currwnd as Integer
Combo1.clear
Currwnd=getwindow (hwnd, Gw-hwndfirst)
While Currwnd<>0
Length= GetWindow textlength (Currwnd)
Listitem$=space $ (length + 1)
Length= GetWindow Text (currwnd,listitem$,length+1)
If Length>0 Then
Combo1.addltem ListItem $
End If
Currwnd= GetWindow (Currwnd, Gw-hwndnext)
If Combl1.listcount>0 Then
Combo1.text=combol.list (0)
Combo1.listindex =0
Else
MsgBox "No active window found", 16, "activity"
End If
Wend
End Sub
Sub Sift ()
' Whether the test window can be active
I=0
Combo2.clear
Todo
On local Error Resume Next
AppActivate Combo1.list (i)
If ERR =0 Then
Combo2.additem Combo1.list (i)
End If
I=i+1
Loop Unti 1 i=combo1.listcount-1
AppActivate form1.caption
If Combo2.listcount>0 Then
Combo2.text =combo2.list (0)
Combo2.listindex =0
Else
MsgBox "No movable window found", 16, "active"
End If
End Sub
Private Sub Form-load ()
Form1.show ' first display this form, otherwise find out the form title without itself
MsgBox "Start looking for window title"
Call Findtitle
Call Sift
End Sub
Private Sub Command1-click ()
F $=combo2.text
On local Error Resume Next
AppActivate F $
End Sub
Private Sub Command2-click ()
Call Findtitle
Call Sift
End Sub
This program is debugged on visual Basic5.0.


Use Form_QueryUnload to ask if the user closes the window
How to check if the user is sure to close when the window is closed, if not, cancel the shutdown action, or end the program!
This is a very important question, think about, if there are editors in the file has not been archived, but forgot to press the "Shutdown" button, then the editor of the file is not archived it? To avoid this, Windows will consult each window before shutting down to see if they agree to shut down, and for VB programs, the "Consent to Shutdown" event is Form _queryunload, if the program does not agree to shutdown, you can set the Cancel in the three numbers to True, as follows:
Private Sub form_queryunload (Cancel As Integer, UnloadMode as Integer)
Cancel = True
End Sub
Windows then terminates the shutdown action. Windows really shuts down only if all windows agree to shut down the computer.



How to restore a window that is blocked from being covered
If the form has performed a window that refuses to be covered, how should it be restored to the normal form?
Refusal to be covered ret = SetWindowPos (Me.hwnd,-1, 0, 0, 0, 0, 3)
Restore normal ret = SetWindowPos (Me.hwnd,-2, 0, 0, 0, 0, 3)




Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.