From 81b667a95e360efcdf9d28abc79dfe6c3365b681 Mon Sep 17 00:00:00 2001 From: tsuji Date: Tue, 19 Oct 2021 09:21:35 +0900 Subject: [PATCH] =?UTF-8?q?GetADocGUI=EF=BC=88Framework4.6.1ver=EF=BC=89?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GetADoc/CustomMsgBox.cs | 168 +++++++++++++++++++++++++++++++++++++ GetADoc/GetADoc.csproj | 2 + GetADoc/MainWindow.xaml | 59 ++++++------- GetADoc/MainWindow.xaml.cs | 78 ++++------------- GetADoc/PushedButton.cs | 100 ++++++++++++++++++++++ Release/GetADocGUI.exe | Bin 15360 -> 18432 bytes Release/GetADocGUI.pdb | Bin 36352 -> 42496 bytes 7 files changed, 319 insertions(+), 88 deletions(-) create mode 100644 GetADoc/CustomMsgBox.cs create mode 100644 GetADoc/PushedButton.cs diff --git a/GetADoc/CustomMsgBox.cs b/GetADoc/CustomMsgBox.cs new file mode 100644 index 0000000..0530740 --- /dev/null +++ b/GetADoc/CustomMsgBox.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Interop; +using System.Runtime.InteropServices; + +namespace GetADoc +{ + //親(メイン)ウインドウの中心にメッセージを表示するためのクラス + class CustomMsgBox + {/// + /// 親ウィンドウ + /// + private Window ownerWindow = null; + + /// + /// フックハンドル + /// + private IntPtr hHook = IntPtr.Zero; + + /// + /// メッセージボックスを表示する + /// + /// + /// + /// + /// + /// + /// + public static MessageBoxResult Show( + Window owner, + string messageBoxText, + string caption, + MessageBoxButton button, + MessageBoxImage icon) + { + + if (owner.WindowState == WindowState.Minimized) + { + return MessageBox.Show(owner, messageBoxText, caption, button, icon); + } + else + { + CustomMsgBox mbox = new CustomMsgBox(owner); + return mbox.Show(messageBoxText, caption, button, icon); + } + } + + /// + /// コンストラクタ + /// + /// Owner Window + private CustomMsgBox(Window window) + { + ownerWindow = window; + } + + /// + /// メッセージボックスを表示する + /// + /// + /// + /// + /// + /// + private MessageBoxResult Show( + string messageBoxText, + string caption, + MessageBoxButton button, + MessageBoxImage icon) + { + // フックを設定する。 + HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(ownerWindow); + IntPtr hInstance = WinAPI.GetWindowLong(hwndSource.Handle, WinAPI.GWL_HINSTANCE); + IntPtr threadId = WinAPI.GetCurrentThreadId(); + hHook = WinAPI.SetWindowsHookEx(WinAPI.WH_CBT, new WinAPI.HOOKPROC(HookProc), hInstance, threadId); + + return MessageBox.Show(ownerWindow, messageBoxText, caption, button, icon); + } + + /// + /// フックプロシージャ + /// + /// + /// + /// + /// + private IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam) + { + + if (nCode == WinAPI.HCBT_ACTIVATE) + { + WinAPI.RECT rcForm = new WinAPI.RECT(0, 0, 0, 0); + WinAPI.RECT rcMsgBox = new WinAPI.RECT(0, 0, 0, 0); + + HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(ownerWindow); + WinAPI.GetWindowRect(hwndSource.Handle, out rcForm); + WinAPI.GetWindowRect(wParam, out rcMsgBox); + + // センター位置を計算する。 + int x = (rcForm.Left + (rcForm.Right - rcForm.Left) / 2) - ((rcMsgBox.Right - rcMsgBox.Left) / 2); + int y = (rcForm.Top + (rcForm.Bottom - rcForm.Top) / 2) - ((rcMsgBox.Bottom - rcMsgBox.Top) / 2); + + WinAPI.SetWindowPos(wParam, 0, x, y, 0, 0, WinAPI.SWP_NOSIZE | WinAPI.SWP_NOZORDER | WinAPI.SWP_NOACTIVATE); + + IntPtr result = WinAPI.CallNextHookEx(hHook, nCode, wParam, lParam); + + // フックを解除する。 + WinAPI.UnhookWindowsHookEx(hHook); + hHook = IntPtr.Zero; + + return result; + + } + else + { + return WinAPI.CallNextHookEx(hHook, nCode, wParam, lParam); + } + } + } + + internal class WinAPI + { + [DllImport("user32.dll")] + public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex); + [DllImport("kernel32.dll")] + public static extern IntPtr GetCurrentThreadId(); + [DllImport("user32.dll")] + public static extern IntPtr SetWindowsHookEx(int idHook, HOOKPROC lpfn, IntPtr hInstance, IntPtr threadId); + [DllImport("user32.dll")] + public static extern bool UnhookWindowsHookEx(IntPtr hHook); + [DllImport("user32.dll")] + public static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam); + [DllImport("user32.dll")] + public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); + [DllImport("user32.dll")] + public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); + + public delegate IntPtr HOOKPROC(int nCode, IntPtr wParam, IntPtr lParam); + + public const int GWL_HINSTANCE = (-6); + public const int WH_CBT = 5; + public const int HCBT_ACTIVATE = 5; + + public const int SWP_NOSIZE = 0x0001; + public const int SWP_NOZORDER = 0x0004; + public const int SWP_NOACTIVATE = 0x0010; + + public struct RECT + { + public RECT(int inLeft, int inTop, int inRight, int inBottom) + { + Left = inLeft; + Top = inTop; + Right = inRight; + Bottom = inBottom; + } + + public int Left; + public int Top; + public int Right; + public int Bottom; + } + } +} diff --git a/GetADoc/GetADoc.csproj b/GetADoc/GetADoc.csproj index d4988d7..9253625 100644 --- a/GetADoc/GetADoc.csproj +++ b/GetADoc/GetADoc.csproj @@ -72,6 +72,8 @@ App.xaml Code + + MainWindow.xaml Code diff --git a/GetADoc/MainWindow.xaml b/GetADoc/MainWindow.xaml index eb5ff6a..a2979fe 100644 --- a/GetADoc/MainWindow.xaml +++ b/GetADoc/MainWindow.xaml @@ -5,32 +5,35 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GetADoc" mc:Ignorable="d" - Title="GetADoc" Height="250" Width="400"> - - - - - - - - - - - -