For example, to show Form2 in this way from Form1, you would use code like this:
VB Code:
Form2.Show vbModal
Method 2: If you want to make the form go on top of others in your program at a particlar place in your code, but allow the user to switch to another form if they want to (and bring that one on top), you can just change the ZOrder of the form.
ZOrder is the "depth" position on screen, with 0 being the one which is "on top". To bring Form1 to the front you would do this:
VB Code:
Form1.ZOrder 0
Method 3: If you want your form to stay on top of all programs that are running you need to use the SetWindowPos API call, in order to change the ZOrder within Windows and not just in your program.
To use this API you need to add the following declarations (in the "General" "Declarations" section of a form/module):
VB Code:
Public 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 Public Const HWND_TOPMOST = -1 Public Const HWND_NOTOPMOST = -2 Public Const SWP_NOMOVE = &H2 Public Const SWP_NOSIZE = &H1
VB Code:
Call SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
To return the form to normal, this is the code you need:
VB Code:
Call SetWindowPos(Form1.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
Method 4: If you want the form to stay on top of others in your program (but not on top of other programs), and allow the user to use other forms whilst keeping this one on top, you show the form in a non-modal state and specify the owner.
This code shows Form2, with Form1 being its owner:
VB Code:
Form2.Show vbModeless, Form1
Note that any code after this line will run as soon as Form2 has finished being shown (after the Form_Load event if it isn't already loaded).
Comments (0)
Đăng nhận xét