From 4fa35b22391d56ba22725229f8d99f0061c910d0 Mon Sep 17 00:00:00 2001 From: papplampe Date: Tue, 10 Jun 2014 14:34:26 +0200 Subject: [PATCH] added Center function to window --- common_windows.go | 1 + sysdata_windows.go | 12 ++++++++++++ window.go | 11 +++++++++++ zconstants_windows_386.go | 2 ++ zconstants_windows_amd64.go | 2 ++ 5 files changed, 28 insertions(+) diff --git a/common_windows.go b/common_windows.go index 8324e2e..2b3c5c8 100644 --- a/common_windows.go +++ b/common_windows.go @@ -91,6 +91,7 @@ var ( _setWindowPos = user32.NewProc("SetWindowPos") _setWindowText = user32.NewProc("SetWindowTextW") _showWindow = user32.NewProc("ShowWindow") + _getWindowRect = user32.NewProc("GetWindowRect") ) type _MINMAXINFO struct { diff --git a/sysdata_windows.go b/sysdata_windows.go index 4a4f69f..76c068c 100644 --- a/sysdata_windows.go +++ b/sysdata_windows.go @@ -685,3 +685,15 @@ func (s *sysData) repaintAll() { } <-ret } + +func (s *sysData) center() { + var ws _RECT + _getWindowRect.Call(uintptr(s.hwnd), uintptr(unsafe.Pointer(&ws))) + dw, _, _ := _getSystemMetrics.Call(_SM_CXFULLSCREEN) + dh, _, _ := _getSystemMetrics.Call(_SM_CYFULLSCREEN) + ww := ws.right - ws.left + wh := ws.bottom - ws.top + wx := (int32(dw) / 2) - (ww / 2) + wy := (int32(dh) / 2) - (wh / 2) + s.setRect(int(wx), int(wy), int(ww), int(wh), 0) +} diff --git a/window.go b/window.go index 02b33e9..f9c9337 100644 --- a/window.go +++ b/window.go @@ -120,3 +120,14 @@ func (w *Window) Hide() { w.sysData.hide() } + +// Centers the window +func (w *Window) Center() { + w.lock.Lock() + defer w.lock.Unlock() + + if !w.created { + return + } + w.sysData.center() +} diff --git a/zconstants_windows_386.go b/zconstants_windows_386.go index 0dfbf22..1db9e22 100644 --- a/zconstants_windows_386.go +++ b/zconstants_windows_386.go @@ -81,6 +81,8 @@ const _SIF_RANGE = 1 const _SIF_TRACKPOS = 16 const _SM_CXDOUBLECLK = 36 const _SM_CYDOUBLECLK = 37 +const _SM_CXFULLSCREEN = 16 +const _SM_CYFULLSCREEN = 17 const _SPI_GETNONCLIENTMETRICS = 41 const _SRCCOPY = 13369376 const _SS_LEFTNOWORDWRAP = 12 diff --git a/zconstants_windows_amd64.go b/zconstants_windows_amd64.go index de3a31e..a8bed71 100644 --- a/zconstants_windows_amd64.go +++ b/zconstants_windows_amd64.go @@ -81,6 +81,8 @@ const _SIF_RANGE = 1 const _SIF_TRACKPOS = 16 const _SM_CXDOUBLECLK = 36 const _SM_CYDOUBLECLK = 37 +const _SM_CXFULLSCREEN = 16 +const _SM_CYFULLSCREEN = 17 const _SPI_GETNONCLIENTMETRICS = 41 const _SRCCOPY = 13369376 const _SS_LEFTNOWORDWRAP = 12