Skip to content

Instantly share code, notes, and snippets.

@yuka1984
Created July 20, 2015 01:11
Show Gist options
  • Select an option

  • Save yuka1984/39fa18ea706c4ac20851 to your computer and use it in GitHub Desktop.

Select an option

Save yuka1984/39fa18ea706c4ac20851 to your computer and use it in GitHub Desktop.
等倍でウィンドウサイズを変更するように強制する。 ref: http://qiita.com/yu_ka1984/items/b4a3ce9ed7750bd67b86
public partial class WindowEx : Window
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
//using System.Runtime.InteropServices;
const double fixedRate = (double)1024 / 737;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr handle = (new WindowInteropHelper(this)).Handle;
HwndSource hwndSource =
(HwndSource)HwndSource.FromVisual(this);
hwndSource.AddHook(WndHookProc);
}
const int WM_SIZING = 0x214;
const int WMSZ_LEFT = 1;
const int WMSZ_RIGHT = 2;
const int WMSZ_TOP = 3;
const int WMSZ_TOPLEFT = 4;
const int WMSZ_TOPRIGHT = 5;
const int WMSZ_BOTTOM = 6;
const int WMSZ_BOTTOMLEFT = 7;
const int WMSZ_BOTTOMRIGHT = 8;
private IntPtr WndHookProc(
IntPtr hwnd, int msg, IntPtr wParam,
IntPtr lParam, ref bool handled)
{
if (msg == WM_SIZING)
{
RECT r = (RECT)Marshal.PtrToStructure(
lParam, typeof(RECT));
RECT recCopy = r;
int w = r.right - r.left;
int h = r.bottom - r.top;
int dw;
int dh;
dw = (int)(h * fixedRate + 0.5) - w;
dh = (int)(w / fixedRate + 0.5) - h;
switch (wParam.ToInt32())
{
case WMSZ_TOP:
case WMSZ_BOTTOM:
r.right += dw;
break;
case WMSZ_LEFT:
case WMSZ_RIGHT:
r.bottom += dh;
break;
case WMSZ_TOPLEFT:
if (dw > 0) r.left -= dw;
else r.top -= dh;
break;
case WMSZ_TOPRIGHT:
if (dw > 0) r.right += dw;
else r.top -= dh;
break;
case WMSZ_BOTTOMLEFT:
if (dw > 0) r.left -= dw;
else r.bottom += dh;
break;
case WMSZ_BOTTOMRIGHT:
if (dw > 0) r.right += dw;
else r.bottom += dh;
break;
}
Marshal.StructureToPtr(r, lParam, false);
}
return IntPtr.Zero;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment