How to determine correctly with VBNET or C# code the Non-Client Area Size when Aero is activated for a COMPILED application? (Yes, this problem only occurs when running a compiled application, not when launching the app from the IDE)
When I resize my form or I do any operation relationed with the height/width of the form I never get the expected result.
For example this is a part of a code of a simple Docking of two forms:
VB-NET:
Me.Location = New Point((form1.Location.X + form1.Width), form1.Location.Y)
C#:
this.Location = new Point((form1.Location.X + form1.Width), form1.Location.Y);
To give an example I'll show a program of mine.
The code above runs perfectlly when Aero is not activated:
...But if Aero is activated then this is the result:
Notice how the form of the right is under the Non-Client border of the left Form.
...Or here is other image where the left form is under the Non-Client border of the right form:
My question is Which is the method to solve this?
UPDATE:
Extending the frame solution is not working.
Form1:
Imports System.Runtime.InteropServices
Public Class Form1
Public Moving_From_Secondary_Form As Boolean = False
<DllImport("dwmapi.dll")> _
Private Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef margins As MARGINS) As Integer
End Function
<StructLayout(LayoutKind.Sequential)> _
Public Structure MARGINS
Public leftWidth As Integer
Public rightWidth As Integer
Public topHeight As Integer
Public bottomHeight As Integer
End Structure
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim margins As New MARGINS()
margins.leftWidth = -1
margins.rightWidth = -1
margins.topHeight = -1
margins.bottomHeight = -1
DwmExtendFrameIntoClientArea(Me.Handle, margins)
Form2.Show()
End Sub
Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Move
If Not Moving_From_Secondary_Form Then Form2.Location = New Point(Me.Right, Me.Top)
End Sub
End Class
Form2:
Public Class Form2
Private Sub Form2_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Move
Form1.Moving_From_Secondary_Form = True
Form1.Location = New Point(Me.Left - Form1.Width, Me.Top)
Form1.Moving_From_Secondary_Form = False
End Sub
End Class
Result:
http://img824.imageshack.us/img824/3176/prtscrcapture2q.jpg
Also I want to remember: this problem only occurs when running a compiled application, not when launching the app from the IDE
**
UPDATE:
**
Tested the GetWindowRect solution and always return a 0, not working for me, maybe I'm doing something wrong:
Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function GetWindowRect Lib "user32" (ByVal Handle As IntPtr, Rect As RECT) As Long
Private Declare Function CopyRect Lib "user32" (DestRect As RECT, SourceRect As RECT) As Long
<StructLayout(LayoutKind.Sequential)> _
Structure RECT
Public Left As Int32
Public Top As Int32
Public Right As Int32
Public Bottom As Int32
End Structure
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rectWindow As RECT, rectCopy As RECT
'Get the bounding rectangle of this window
GetWindowRect(Me.Handle, rectWindow)
'Copy the rectangle
CopyRect(rectCopy, rectWindow)
MsgBox("This form's width:" & (rectCopy.Right - rectCopy.Left).ToString & " pixels")
Form2.Location = New Point(rectCopy.Right, rectCopy.Top)
End Sub
End Class
**
UPDATE:
**
Another try with GetWindowRect, This time the code is correctly wrote but don't solve the problem:
Imports System.Runtime.InteropServices
Public Class Form1
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Int32
Public Top As Int32
Public Right As Int32
Public Bottom As Int32
End Structure
Private Declare Function GetWindowRect Lib "user32" (ByVal HWND As Integer, ByRef lpRect As RECT) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rc As RECT
GetWindowRect(MyBase.Handle, rc)
Dim width As Integer = rc.Right - rc.Left
Form2.Show()
Form2.Location = New Point(rc.Right, rc.Top)
End Sub
End Class
I want to remember: this problem only occurs when running a compiled application on win7/Vista, not when launching the app from the IDE