Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Everywhere I look the answer is use the API SetWindowsPos. This works great in VB6 and I have been using it for years. It seems to have no effect what so ever in VB.Net (VS 2008) has anybody else found this and what is the solution?
Posted

What's wrong with using the Form.TopMost[^] property?

Edit: Is there any reason why you want the form to be topmost?
To me it seems like you just want to bring the form/window in front of every other window to draw the user's attention to it, am I right?
In that case, you would want to use the SetForegroundWindow[^] function.
 
Share this answer
 
v2
Comments
Graham Eady 8-Jul-10 10:46am    
Thanks for the answers I was aware of "TopMost" but it didn't work quite as I expected. My project is that a window/form is created by the arrival of a file. The tag is flashed in the task bar to draw the users attention to it. If he does nothing then after a certain time then the form is made TopMost. It doesn't work. It only seems to work if the TopMost is set when the form is created not after a dely using a timer. I have done a work around by closing the original form and opening a new one with the TopMost set.

But doesn't answer my original question about the API SetWindowsPos. Why does it work in VB6 and not VB.Net. I thought an API was programming language independent as long as the parameters were passed OK it should work regardless.
Hi Guys

Thanks for the answers I was aware of "TopMost" but it didn't work quite as I expected. My project is that a window/form is created by the arrival of a file. The tag is flashed in the task bar to draw the users attention to it. If he does nothing then after a certain time then the form is made TopMost. It doesn't work. It only seems to work if the TopMost is set when the form is created not after a dely using a timer. I have done a work around by closing the original form and opening a new one with the TopMost set.

But doesn't answer my original question about the API SetWindowsPos. Why does it work in VB6 and not VB.Net. I thought an API was programming language independent as long as the parameters were passed OK it should work regardless.
 
Share this answer
 
Did you try

this.Topmost = true;


It makes the form appear topmost of all other forms.
 
Share this answer
 
Comments
tiggerc 6-Jul-10 4:41am    
you can also set windows position always on top in the form designer.
Something in your code is not right. Here's what I just tried (using both SetWindowPos and TopMost)

I added a Timer to the form and set the Interval to 5000 (5 seconds). On form Load, I start the Timer, then I click a bunch of other programs to bring them on top of the form. After the 5 seconds, the form pops to the top and you can't bring another window in front of it. Here are the two methods:

VB
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, _
                                     ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, _
                                     ByVal cy As Integer, ByVal uFlags As UInt32) As Boolean
End Function

ReadOnly HWND_TOPMOST As New IntPtr(-1)
Shared ReadOnly SWP_NOSIZE As UInt32 = Convert.ToUInt32(&H1)
Shared ReadOnly SWP_NOMOVE As UInt32 = Convert.ToUInt32(&H2)

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'Either one of these does the trick
    SetWindowPos(Me.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    'Me.TopMost = True
End Sub


So, either something is wrong with your code, or your system is a bit messed up.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900