Keep the form at the beginning of the Method

Source: Internet
Author: User
Keep the form at the beginning

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 first Program The statement is as follows:

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:

Parameter meaning
Windows to be located by hwnd long
Hwndinsertafter long window handle. In the window list, the window hwnd is placed behind the window handle.
New X and Y coordinates of the X and Y long windows
CX, Cy long specify the new window width and height
Wflags long contains an integer of the flag, which is one of the following
If the return value is long or non-zero, the operation is successful. If the return value is zero, the operation fails.

The following are some wflags parameters and their meanings:

Parameter meaning and usage
Swp_drawframe draws a box around the window
Swp_hidewindow hide window
Swp_noactivate does not activate the window
Swp_nomove to keep the current position (the X and Y settings will be ignored) & H2
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 force display window & h40
Swp_framechanged forces a wm_nccalcsize message to enter the window, even if the window size does not change

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 is the source code:

Note :-------------------------------------------
Note: An Example of keeping a form always at the beginning
Note :-------------------------------------------
Note: program description:
Note: In this example, the setwindowpos function is used to set the display attribute of the form.
Note: To make a form always in front of other forms rather
Note: It is covered by other forms.
Note :-------------------------------------------
Option explicit

Note: [VB Declaration]
Note: 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

Note: [description]
Note: 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.

Note: [return value]
Note: long. If it is not zero, the operation is successful. If it is zero, the operation fails. Getlasterror is set.

Note: [Remarks]
Note: When a window becomes the top-level window, all its subordinate windows 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

Note: [parameter table]
Note: hwnd ----------- long, the window to be located

Note: 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:
Note: hwnd_bottom places the window at the bottom of the window list
Note: hwnd_top places the window at the top of the Z sequence. The Z sequence indicates the sequence in which the window is displayed for a given level in the hierarchical structure.
Note: hwnd_topmost places the window at the top of the list and is located at the front of any top window-1
Note: hwnd_notopmost places the window at the top of the list, and is located behind-2 of any top window.

Note: 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.

Note: 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.

Note: Cx ------------- long, specifying the new window width

Note: Cy ------------- long, specifying the new window height

Note: wflags --------- long contains an integer of the flag, which is one of the following:
Note: swp_drawframe draws a box around the window.
Note: swp_hidewindow hides the window.
Note: swp_noactivate does not activate the window & H10
Note: swp_nomove retains the current position (the X and Y settings will be ignored) & H2
Note: The swp_noredraw window does not automatically redraw
Note: swp_nosize retains the current size (CX and Cy will be ignored) & H1
Note: swp_nozorder keeps the current position of the window in the list (hwndinsertafter will be ignored)
Note: swp_showwindow display window & h40
Note: 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
Note: Set the form to the top layer of all windows. 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)

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.