①フォームにボタン(button1)を加える
②button1のクリックイベントにbutton1_Clickを設定する
③usingにCustomMsgBox(当プログラムのnamespace)を加える
④以下のコードをフォームのコードに加える
private void button1_Click(object sender, EventArgs e)
{
MsgBox.Show(this, "wow");
MsgBox.Show(this, "wow", "cap");
MsgBox.Show(this, "wow", "cap", MessageBoxButtons.OK);
MsgBox.Show(this, "wow", "cap", MessageBoxButtons.OK, MessageBoxIcon.Information);
MsgBox.Show(this, "wow", "cap", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
MsgBox.Show(this, "wow", "cap", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
MsgBox.Show(this, "wow", "cap", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign, "");
MsgBox.Show(this, "wow", "cap", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign, "", HelpNavigator.Index);
MsgBox.Show(this, "wow", "cap", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign, "", HelpNavigator.Index, null);
}
- Owner Window中央に表示されるMessageBox
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows;
namespace CustomMsgBox
{
/// <summary>
/// Owner Window中央に表示されるMessageBox
/// </summary>
public class MsgBox
{
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(IntPtr hHook);
[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 hMod, IntPtr dwThreadId);
[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 [[enum]] HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14,
}
public enum GWL : int
{
GWL_HINSTANCE = (-6),
GWL_HWNDPARENT = (-8),
GWL_ID = (-12),
GWL_EXSTYLE = (-20),
GWL_STYLE = (-16),
GWL_WNDPROC = (-16),
GWL_USERDATA = (-21),
}
public enum SWP : uint
{
SWP_ASYNCWINDOWPOS = 0x4000,
SWP_DEFERERASE = 0x2000,
SWP_DRAWFRAME = 0x0020,
SWP_FRAMECHANGED = 0x0020,
SWP_HIDEWINDOW = 0x0080,
SWP_NOACTIVATE = 0x0010,
SWP_NOCOPYBITS = 0x0100,
SWP_NOMOVE = 0x0002,
SWP_NOOWNERZORDER = 0x0200,
SWP_NOREDRAW = 0x0008,
SWP_NOREPOSITION = 0x0200,
SWP_NOSENDCHANGING = 0x0400,
SWP_NOSIZE = 0x0001,
SWP_NOZORDER = 0x0004,
SWP_SHOWWINDOW = 0x0040,
}
public enum HCBT : int
{
HCBT_ACTIVATE = 5,
HCBT_CLICKSKIPPED = 6,
HCBT_CREATEWND = 3,
HCBT_DESTROYWND = 4,
HCBT_KEYSKIPPED = 7,
HCBT_MINMAX = 1,
HCBT_MOVESIZE = 0,
HCBT_QS = 2,
HCBT_SETFOCUS = 9,
HCBT_SYSCOMMAND = 8,
}
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public int Width
{
get
{
return Right - Left;
}
}
public int Height
{
get
{
return Bottom - Top;
}
}
}
/// <summary>
/// 親ウィンドウ
/// </summary>
private IWin32Window OwnerWindow;
/// <summary>
/// フックハンドル
/// </summary>
private IntPtr HHook;
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="owner">親ウィンドウ</param>
private MsgBox(IWin32Window owner)
{
OwnerWindow = owner;
}
public static DialogResult Show(
IWin32Window owner,
string text)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption, buttons);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption, buttons, icon);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption, buttons, icon, defaultButton);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption, buttons, icon, defaultButton, options);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption, buttons, icon, defaultButton, options, helpFilePath);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption, buttons, icon, defaultButton, options, helpFilePath, navigator);
}
public static DialogResult Show(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator,
object param)
{
MsgBox mb = new MsgBox(owner);
return mb.ShowMsgDialog(text, caption, buttons, icon, defaultButton, options, helpFilePath, navigator, param);
}
private DialogResult ShowMsgDialog(
string text)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text);
}
private DialogResult ShowMsgDialog(
string text,
string caption)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption);
}
private DialogResult ShowMsgDialog(
string text,
string caption,
MessageBoxButtons buttons)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption, buttons);
}
private DialogResult ShowMsgDialog(
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption, buttons, icon);
}
private DialogResult ShowMsgDialog(
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption, buttons, icon, defaultButton);
}
private DialogResult ShowMsgDialog(
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption, buttons, icon, defaultButton, options);
}
private DialogResult ShowMsgDialog(
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption, buttons, icon, defaultButton, options, helpFilePath);
}
private DialogResult ShowMsgDialog(
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption, buttons, icon, defaultButton, options, helpFilePath, navigator);
}
private DialogResult ShowMsgDialog(
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator,
object param)
{
// フック設定
SetHook(OwnerWindow);
return MessageBox.Show(OwnerWindow, text, caption, buttons, icon, defaultButton, options, helpFilePath, navigator, param);
}
/// <summary>
/// フックプロシージャ
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
private IntPtr CBTProc(
int nCode,
IntPtr wParam,
IntPtr lParam)
{
if (nCode == (int)HCBT.HCBT_ACTIVATE)
{
RECT rectOwner;
RECT rectMsgBox;
int x, y;
// Window位置・サイズ取得
GetWindowRect(OwnerWindow.Handle, out rectOwner);
GetWindowRect(wParam, out rectMsgBox);
// MessageBox出力位置設定
x = rectOwner.Left + (rectOwner.Width - rectMsgBox.Width) / 2;
y = rectOwner.Top + (rectOwner.Height - rectMsgBox.Height) / 2;
SetWindowPos(wParam, 0, x, y, 0, 0, (uint)(SWP.SWP_NOSIZE | SWP.SWP_NOZORDER | SWP.SWP_NOACTIVATE));
// フック解除
UnhookWindowsHookEx(HHook);
}
// 次のプロシージャへのポインタ
return CallNextHookEx(HHook, nCode, wParam, lParam);
}
/// <summary>
/// フック設定
/// </summary>
/// <param name="owner"></param>
private void SetHook(IWin32Window owner)
{
// フック設定
IntPtr hInstance = GetWindowLong(OwnerWindow.Handle, (int)GWL.GWL_HINSTANCE);
IntPtr threadId = GetCurrentThreadId();
HHook = SetWindowsHookEx((int)HookType.WH_CBT, new HOOKPROC(CBTProc), hInstance, threadId);
}
}
}
最終更新:2012年03月04日 02:33