VB keeps the form at the beginning

Source: Internet
Author: User
You can use the setwindowpos function to set the position and status of a window. In this example, you can use this function to place a form on all forms so that it is always at the beginning.

Let's take a look at the definition and parameters of the setwindowpos function:

Before using the API function, you must declare the following in the program:

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
The meanings of parameters are as follows:

It indicates the hwndinsertafter long window handle of the window to be located by hwnd long. In the window list, the window hwnd will be placed behind the window handle x, y long window new x, y coordinate CX, cy long specifies the new window width and height. wflags long contains an integer of the flag. If it is one of the following returned values: Long, non-zero, indicating success, and zero, indicating failure
The following are some wflags parameters and their meanings:

Parameter meaning and usage method swp_drawframe draw a box around the window swp_hidewindow hide the window swp_noactivate do not activate the window swp_nomove to keep the current position (X and Y settings will be ignored) & h2swp_noredraw window does not automatically redraw swp_nosize to keep the current size (CX and Cy will be ignored) & h1swp_nozorder keep the current position of the window in the list (hwndinsertafter will be ignored) swp_showwindow display window & h40swp_framechanged forces a wm_nccalcsize message to enter the window, even if the window size is not changed
The function is easy to use. You only need to add the following statement to form_load:

Retvalue = setwindowpos (Me. hwnd, hwnd_topmost, me. currentx, me. currenty, 300,300, swp_showwindow)
In this way, the form can be kept in front of all forms.

For ease of learning, the following provides the source code:

/'-------------------------------------------
/'Example of keeping a form always at the beginning
/'-------------------------------------------
/'Program description:
/'In this example, the setwindowpos function is used to set the display attribute of the form.
/'To Make A form always in front of other forms rather
/'Is covered by other forms.
/'-------------------------------------------
Option explicit

/'[VB Declaration]
/'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

/'[Description]
/'This function can specify a new position and status for the window. It can also change the position of the window in the internal window list. This function is similar to the deferwindowpos function, but its function is displayed immediately (used in VB: for VB forms, such as shielding or minimizing them under Win32, you need to reset the top status. If necessary, use a subclass processing module to reset the top state.

/'[Return value]
/'Long, non-zero indicates success, and zero indicates failure. Getlasterror is set.

/'[Remarks]
/'When the window becomes the top-level window, all its subordinate windows will also enter the top-level window. Once it is set to a non-top-level window, all its subordinates and owner windows will be converted to a non-top-level window. Z sequence quantifies the window sequence arranged from top to bottom with an imaginary Z axis perpendicular to the screen

/'[Parameter table]
/'Hwnd ----------- long, the window to be located

/'Hwndinsertafter-long, window handle. In the window list, the window hwnd is placed behind the window handle. You may also choose one of the following values:
/'Hwnd_bottom: place the window at the bottom of the window list
/'Hwnd_top: place the window at the top of the Z sequence. The Z sequence indicates the order in the hierarchical structure that the window displays for a given level of window.
/'Hwnd_topmost: place the window at the top of the list and in front of any top window-1
/'Hwnd_notopmost: place the window at the top of the list and behind any top window-2

/'X -------------- long, the new x coordinate of the window. If hwnd is a subwindow, X is represented by the customer zone coordinate of the parent window.

/'Y -------------- long, the New Y coordinate of the window. If hwnd is a subwindow, Y is represented by the customer zone coordinate of the parent window.

/'Cx ------------- long, specify the new window width

/'Cy ----------- long, specify the new window height

/'Wflags --------- long, which contains an integer of the flag, is one of the following:
/'Swp_drawframe draw a box around the window
/'Swp_hidewindow hide the window
/'Swp_noactivate does not activate the window & H10
/'Swp_nomove keep current position (the X and Y settings will be ignored) & H2
/The 'swp_noredraw window does not automatically redraw
/'Swp_nosize keep the current size (CX and Cy will be ignored) & H1
/'Swp_nozorder keep the current position of the window in the list (hwndinsertafter will be ignored)
/'Swp_showwindow display window & h40
/'Swp_framechanged forces a wm_nccalcsize message to enter the window, even if the window size does not change
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
Const hwnd_topmost =-1
Const swp_showwindow = & h40
Private sub form_load ()
Dim retvalue as long
/'Set the form to the top layer of all windows. Note that when running in VB,
'May not work, but compile it into exe.
Retvalue = setwindowpos (Me. hwnd, hwnd_topmost, me. currentx,
Me. currenty, 300,300, swp_showwindow)
End sub

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.