Skip to content

Instantly share code, notes, and snippets.

@tylerlong
Last active December 18, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerlong/7bdd18af120a6de3b471 to your computer and use it in GitHub Desktop.
Save tylerlong/7bdd18af120a6de3b471 to your computer and use it in GitHub Desktop.

Update 2015-12-18

SetWindowPos can make window larger than screen. But for WPF apps it doesn't work.

Note

Above code is a very simple WPF app, it contains a window with a button at the center (HorizontalAlignment="Center" VerticalAlignment="Center").

Click the button will call SetWindowPos(with NOSENDCHANGING option) to resize window to 6000 * 6000.

Now question is: what's the real size of the window? I think it is the same size as screen (instead of 6000*6000).

Why? because the button is now at the center of screen! It is supposed to be at the center of the window.

how to reproduce

  1. run the WPF application above and click the button.
  2. current window will resize to 6000*6000
  3. but button is at the center of screen instead of the window. Which means the window contect size actually equals to screen size, not larger than screen
<Window x:Class="ResizeWindowSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ResizeWindowSample"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Button x:Name="button" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="button_Click"/>
</Window>
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace ResizeWindowSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(String className, String windowName);
[DllImport("USER32.DLL", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int left, int top, int width, int height, uint flags);
private void button_Click(object sender, RoutedEventArgs e)
{
var TOP = new IntPtr(0);
uint SHOWWINDOW = 0x0040, NOCOPYBITS = 0x0100, NOSENDCHANGING = 0x0400;
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowPos(hwnd, TOP, 0, 0, 6000, 6000, NOCOPYBITS | NOSENDCHANGING | SHOWWINDOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment