Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep Form on top 2

Status
Not open for further replies.

mrMikee

Technical User
Jun 17, 2005
8
US
I want to my VB 6 form on the top of other running programs. I am writing a program to send keystrokes to another application and when I AppActivate the other program it pops up over mine. The last time I tried this many years ago I think I came to the conclusion it was part of Windows API and not VB, which would have been too much for me.

It's been a long time since I have done much programing, but it would be nice to get this to work.

Thanks,
-Mike
 
Place the following code in a module
Code:
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

Public Function PutMyAppOnTop(pFrm As Form)
  Dim lngWindowPosition As Long
  
  lngWindowPosition = SetWindowPos(pFrm.hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

End Function
Place the following code in your main form (assuming your module is named Module1)
Code:
Call Module1.PutMyAppOnTop(Me)

Maybe this world is another planet’s Hell.
 
Thanks bigtimmin for the help. I will give this a try.

-Mike
 
It is worth nowing that the '-1' in the above function is generally known as HWND_TOPMOST
 
Yes you can place a constant in the declration section.
Const HWND_TOPMOST = -1

Maybe this world is another planet’s Hell.
 
bigtimmin, It works just like you said. Thanks again.

It's just as if I knew what I was doing.

-Mike
 
Why do you have to declare HWND_TOPMOST, but not the nomove or nosize constants? Are the latter two declared inside of SetWindowPos, whereas the former evolved as a convemtion subsequent to the creation of SetWindowPos?

Bob
 
Technically, you don't need to declare any constant to make this function work.

The following code does the job equally good like the one mentioned above.

[tt]SetWindowPos pFrm.hwnd, -1, 0, 0, 0, 0, 3[/tt]

But it is always a good and recommended programming practice to declare constants and then use them in your code. This makes the program easier to read and understand, not only by other programmers but for yourself too.

See another example.
[tt]
SendMessage hwnd, 274, 61536, ByVal 0&

SendMessage hwnd, WM_SYSCOMMAND, SC_CLOSE, ByVal 0&
[/tt]
Both lines do the same but the latter one explicitly tells the reader what message is being sent with what options.
 
Yes, but my question is why don't you have to declare some of the functions, and yet do have to declare others?
 
>why don't you have to declare some of the functions...

I don't really get an idea what functions are you talking about. You have to declare all API functions that you use in your program.

There might be some exceptions. The API function might have been wrapped by some VB method. You might have some type-library containing function declarations or some other scenario, but that is a different story.

Normally, you have to declare all API functions, prior to their use.
 
>why don't you have to declare some of the functions, and yet do have to declare others?

You do, and nobody is saying that you don't. It is just the way this thread has developed that makes it look as if that is the case. If you are going to use SWP_NOMOVE, SWP_NOSIZE, or HWND_TOPMOST by name then you have to declare them
 
>why don't you have to declare some of the functions, and yet do have to declare others?

Acutally those are constants, which I forgot to put in my original thread (Sorry). The following constants should be placed in the declaration section. -or- Subsitute the constant names in the Function with their values.
Code:
[COLOR=blue]Const[/color] HWND_TOPMOST = -1
[COLOR=blue]Const[/color] SWP_NOMOVE = &H2
[COLOR=blue]Const[/color] SWP_NOSIZE = &H1

Maybe this world is another planet’s Hell.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top