C#中可以固定MessageBox.Show的方法

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using System.Windows.Forms;

using System.Drawing;

namespace Updater{  

struct RECT {

public int left;

public int top;

public int right;

public int bottom;

}; //实现MessageBox居中owner窗体显示

class MessageBoxEx {

public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]

private static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid);

[DllImport("user32.dll")]

private static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);

[DllImport("kernel32.dll")]

private static extern IntPtr GetModuleHandle(string modName);

[DllImport("user32.dll")]

private static extern bool UnhookWindowsHookEx(IntPtr hhook);

[DllImport("user32.dll")]

private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);

[DllImport("user32.dll")]

private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

[DllImport("user32.dll")]

private static extern bool MoveWindow( IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

private const int WH_CBT = 5;

private const int HCBT_ACTIVATE = 5;

private const int GW_OWNER = 4;

private static IntPtr hookHandle = IntPtr.Zero;

private static RECT GetOwnerRect(IntPtr hwnd)

{

RECT ownerRect = new RECT();

IntPtr ownerHwnd = GetWindow(hwnd, GW_OWNER);

GetWindowRect(ownerHwnd, ref ownerRect);

return ownerRect; }

private static IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam)

{

switch (nCode)

{

case HCBT_ACTIVATE:

RECT vRectangle = new RECT();

RECT ownerRect = GetOwnerRect(wParam);

GetWindowRect(wParam, ref vRectangle);

int width = vRectangle.right - vRectangle.left;

int height = vRectangle.bottom - vRectangle.top; 

int ownerWidth = ownerRect.right - ownerRect.left;

int ownerHeight = ownerRect.bottom - ownerRect.top;

int left = Math.Max(ownerRect.left + (ownerWidth - width) / 2, 0);

int top = Math.Max(ownerRect.top + (ownerHeight - height) / 2, 0); 

MoveWindow(wParam, left, top, width, height, false);

UnhookWindowsHookEx(hookHandle);

break;

}

return CallNextHookEx(hookHandle, nCode, wParam, lParam);

}

private static void Lock()

{

hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback), GetModuleHandle(null), 0);

} //根据需要重载

public static DialogResult Show(string text)

{

Lock(); 

return MessageBox.Show(text);

}

public static DialogResult Show(IWin32Window owner, string text)

{

Lock();

return MessageBox.Show(owner, text);

}

public static DialogResult Show(string text, string caption)

{

Lock();

return MessageBox.Show(text, caption);

}

public static DialogResult Show(IWin32Window owner, string text, string caption)

{

Lock();

return MessageBox.Show(owner, text, caption);

}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)

{

Lock();

return MessageBox.Show(text, caption, buttons);

}

public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)

{

Lock();

return MessageBox.Show(owner, text, caption, buttons);

}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)

{

Lock();

return MessageBox.Show(text, caption, buttons, icon);

}

public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) 

{

Lock();

return MessageBox.Show(owner, text, caption, buttons, icon);

}

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)

{

Lock();

return MessageBox.Show(text, caption, buttons, icon, defaultButton);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#,可以使用自定义消息框的方式来实现修改MessageBox字体颜色的效果。以下是一个示例代码: ```csharp using System.Drawing; using System.Windows.Forms; public static class MessageBoxWithColor { public static DialogResult Show(string text, string caption, Color textColor) { DialogResult result = DialogResult.None; using (Form form = new Form()) { form.FormBorderStyle = FormBorderStyle.FixedDialog; form.MaximizeBox = false; form.MinimizeBox = false; form.StartPosition = FormStartPosition.CenterParent; form.ShowIcon = false; form.BackColor = Color.White; form.Text = caption; Label label = new Label(); label.TextAlign = ContentAlignment.MiddleCenter; label.Font = new Font("Microsoft Sans Serif", 10F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); label.ForeColor = textColor; label.Text = text; label.AutoSize = true; label.Location = new Point(20, 20); label.MaximumSize = new Size(400, 0); form.ClientSize = new Size(Math.Min(label.Width + 40, 400), label.Height + 100); form.Controls.Add(label); Button button = new Button(); button.DialogResult = DialogResult.OK; button.Location = new Point(form.ClientSize.Width / 2 - 50, label.Height + 50); button.Size = new Size(100, 30); button.Text = "OK"; button.UseVisualStyleBackColor = true; form.Controls.Add(button); form.AcceptButton = button; result = form.ShowDialog(); } return result; } } ``` 你可以在需要使用MessageBox的地方,调用MessageBoxWithColor.Show方法,来显示自定义的消息框,并设置字体颜色。例如: ```csharp MessageBoxWithColor.Show("这是一条自定义颜色的消息!", "提示", Color.Red); ``` 在上述示例,自定义了一个名为MessageBoxWithColor的静态类,其的Show方法接受三个参数:消息文本、标题和字体颜色。在Show方法,使用了一个Label控件来显示消息文本,并设置了Label的字体颜色。最后,将Label控件添加到Form,以显示自定义的消息框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值