Form on top VB6

Method 1: If you want a form to stay on top of the other forms in your program (and not allow the user to use any other forms until it is closed), you can show it with the vbModal option.

For example, to show Form2 in this way from Form1, you would use code like this:
VB Code:
  1. Form2.Show vbModal
Note that any code in Form1 after this line will not run until Form2 has been closed.


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:
  1. Form1.ZOrder 0
(to send it to the back, you can use a value of 1 rather than 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:
  1. Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
  2. ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  3. Public Const HWND_TOPMOST = -1
  4. Public Const HWND_NOTOPMOST = -2
  5. Public Const SWP_NOMOVE = &H2
  6. Public Const SWP_NOSIZE = &H1
To set Form1 to be "on top" of other forms use this code:
VB Code:
  1. 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:
  1. 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:
  1. 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

Welcome to my blog !