SetWindowPos (VB.NET)

C

Craig

Hi,

I'm attempting to use SetWindowPos to flip between a few apps and I've
hit a problem. Whenever I use the following it doesn't seem to ignore
the nomove and nosize flags and moves the app to the top left and makes
it 0 x 0. Any suggestions please?

Const SWP_NOSIZE As Short = &H1
Const SWP_NOMOVE As Short = &H2
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

SetWindowPos(iHandle, -1, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
 
A

Arne Janning

Hi Craig!

I'm attempting to use SetWindowPos to flip between a few apps and I've hit
a problem. Whenever I use the following it doesn't seem to ignore the
nomove and nosize flags and moves the app to the top left and makes it 0 x
0. Any suggestions please?

Const SWP_NOSIZE As Short = &H1
Const SWP_NOMOVE As Short = &H2
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

SetWindowPos(iHandle, -1, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)

First of all your Declaration of SetWindowPos is wrong seems to be an old
VB6 one):

use this:

Imports System.Runtime.InteropServices

Public Const SWP_NOSIZE As Int32 = &H1
Public Const SWP_NOMOVE As Int32 = &H2

<DllImport( _
"user32.dll", _
CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall _
)> _
Public Shared Function SetWindowPos( _
ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal cx As Int32, _
ByVal cy As Int32, _
ByVal uFlags As Int32) _
As Boolean
End Function


To combine the SWP_NOSIZE and SWP_NOMOVE-Flags use the binary "Or"-Operator
as shown in the example:

Dim b As Boolean = _
SetWindowPos( _
Me.Handle, _
New IntPtr(-1), _
0, 0, 0, 0, _
SWP_NOMOVE Or SWP_NOSIZE)

Cheers

Arne Janning
 
C

Craig

Arne,

Great it works, thanks for your help.



Arne said:
Hi Craig!




First of all your Declaration of SetWindowPos is wrong seems to be an old
VB6 one):

use this:

Imports System.Runtime.InteropServices

Public Const SWP_NOSIZE As Int32 = &H1
Public Const SWP_NOMOVE As Int32 = &H2

<DllImport( _
"user32.dll", _
CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall _
)> _
Public Shared Function SetWindowPos( _
ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal cx As Int32, _
ByVal cy As Int32, _
ByVal uFlags As Int32) _
As Boolean
End Function


To combine the SWP_NOSIZE and SWP_NOMOVE-Flags use the binary "Or"-Operator
as shown in the example:

Dim b As Boolean = _
SetWindowPos( _
Me.Handle, _
New IntPtr(-1), _
0, 0, 0, 0, _
SWP_NOMOVE Or SWP_NOSIZE)

Cheers

Arne Janning
 
H

Herfried K. Wagner [MVP]

Arne Janning said:
Imports System.Runtime.InteropServices

Public Const SWP_NOSIZE As Int32 = &H1
Public Const SWP_NOMOVE As Int32 = &H2

<DllImport( _
"user32.dll", _
CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall _
)> _
Public Shared Function SetWindowPos( _
ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal cx As Int32, _
ByVal cy As Int32, _
ByVal uFlags As Int32) _
As Boolean
End Function

Mhm... No need for 'Charset.Auto'. I prefer this declaration in VB.NET:

\\\
Public Declare Function SetWindowPos Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal cx As Int32, _
ByVal cy As Int32, _
ByVal uFlags As Int32 _
) As Boolean
///
SetWindowPos( _
Me.Handle, _
New IntPtr(-1), _

=>

\\\
Private ReadOnly HWND_TOPMOST As New IntPtr(-1)
..
..
..
.... = SetWindowPos(Me.Handle, HWND_TOPMOST, ...)
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top