Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:44:17+00:00 2026-05-31T19:44:17+00:00

I am trying to center a Window to the owner window. I also need

  • 0

I am trying to center a Window to the owner window. I also need the child window to move along the owner window. A cross-post on the MSDN WPF forum’s can be found here.

To achieve this I subscribe to the LocationChanged and SizeChanged events (and also the StateChanged event) of the owner of my child window. When those events are triggered I recompute the location of child window. I do this in the code-behind of the child window.

The code is very straight forward:

Top = Owner.Top + ((Owner.ActualHeight - ActualHeight) / 2);
Left = Owner.Left + ((Owner.ActualWidth - ActualWidth) / 2);

If you compile and run the sample program I provided you will see that it works when the main window is as-is, and moved around. So that part works.

The problem arises when the owner window is maximized. (And after being maximized, set back to normal.) Because I subscribe to three events I enter the relocate function three times. After printing out the owner data I get different results. Most annoyingly the Top and Left values of the owner window are off. It seems it gets the correct Top and Left values when the state changes, but then the ActualWidth and ActualHeight values are wrong. When the LocationChanged or SizeChanged events are triggered the ActualWidth and ActualHeight values are OK, but the Top and Left values are incorrect. It seems these are the previous values. How can this be? What is causing this? And is there a proper fix for this?

Since the same code seemed to have worked in .net 3.5 I am under the impression something changed in .net 4. (Or I had a weird timing issue which caused the problem not to appear.) But I cannot find any documented change in this part.

.NET 3.5:

OnOwnerLocationChanged
T: -8; L: -8; W: 640; H: 480
OnOwnerStateChanged
T: -8; L: -8; W: 640; H: 480
OnOwnerSizeChanged
T: -8; L: -8; W: 1936; H: 1066

.NET 4.0:

OnOwnerLocationChanged
T: -8; L: -8; W: 640; H: 480
OnOwnerStateChanged
T: 494; L: 33; W: 640; H: 480
OnOwnerSizeChanged
T: 494; L: 33; W: 1936; H: 1066

So the main question remains: why are the Top and Left values of the owner incorrect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:44:18+00:00Added an answer on May 31, 2026 at 7:44 pm

    The comment from Mataniko regarding the migration issues in .NET 4.0 was correct. Since my code was working when the WindowState was set to Normal, I could keep that. I just had to foresee something when the WindowState was Maximized.

    I implemented the native GetWindowRect() function to achieve this, as it could give me the proper dimensions.

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    
    // Make sure RECT is actually OUR defined struct, not the windows rect.
    public static RECT GetWindowRectangle(Window window)
    {
        RECT rect;
        GetWindowRect((new WindowInteropHelper(window)).Handle, out rect);
    
        return rect;
    }
    

    Next, when the Owner.WindowState is Maximized, we use the GetWindowRectangle function to get the actual dimensions. We don’t care about a border at this point, but if needed it can be incorporated using the GetSystemMetrics function.

    if (Owner.WindowState == WindowState.Maximized)
    {
        var rect = GetWindowRectangle(Owner);
    
        Top = rect.Top + ((rect.Bottom - ActualHeight) / 2);
        Left = rect.Left + ((rect.Right - ActualWidth) / 2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to center a tkinter window. I know I can programatically get the
I am trying to center a launched window in flex, there is a NativeWindow.x
I'm trying to horizontally and vertically center a modal window inside a div. I
I am trying to built a window located in the center of the screen
I am trying to inherit application-level styles for a certain Window in my WPF
I have a problem trying to develop a WPF page. I have a window
I'm trying to have "Hello SWT" show up in the center of a window
I am trying to showing one image in bootstrap modal, also that pop-up window
I'm currently trying to write a Windows Media Center Application (Vista) that can restart
I'm trying to create a WPF application where I can drag an image around.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.