C#自定义MessageBox 按钮的Text
运行效果:
代码:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms; namespace Sphinx.Utils
{
public class MessageBoxEx
{
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles)
{
MessageForm frm = new MessageForm(buttons, buttonTitles);
frm.Show();
frm.WatchForActivate = true;
DialogResult result = MessageBox.Show(frm, text, caption, buttons);
frm.Close(); return result;
} public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles)
{
MessageForm frm = new MessageForm(buttons, buttonTitles);
frm.Show();
frm.WatchForActivate = true;
DialogResult result = MessageBox.Show(frm, text, caption, buttons, icon, defaultButton);
frm.Close(); return result;
} class MessageForm : Form
{
IntPtr _handle;
MessageBoxButtons _buttons;
string[] _buttonTitles = null; bool _watchForActivate = false; public bool WatchForActivate
{
get { return _watchForActivate; }
set { _watchForActivate = value; }
} public MessageForm(MessageBoxButtons buttons, string[] buttonTitles)
{
_buttons = buttons;
_buttonTitles = buttonTitles; // Hide self form, and don't show self form in task bar.
this.Text = "";
this.StartPosition = FormStartPosition.CenterScreen;
this.Location = new Point(-, -);
this.ShowInTaskbar = false;
} protected override void OnShown(EventArgs e)
{
base.OnShown(e);
// Hide self form, don't show self form even in task list.
NativeWin32API.SetWindowPos(this.Handle, IntPtr.Zero, , , , , );
} protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (_watchForActivate && m.Msg == 0x0006)
{
_watchForActivate = false;
_handle = m.LParam;
CheckMsgbox();
}
base.WndProc(ref m);
} private void CheckMsgbox()
{
if (_buttonTitles == null || _buttonTitles.Length == )
return; // Button title index
int buttonTitleIndex = ;
// Get the handle of control in current window.
IntPtr h = NativeWin32API.GetWindow(_handle, GW_CHILD); // Set those custom titles to the three buttons(Default title are: Yes, No and Cancle).
while (h != IntPtr.Zero)
{
if (NativeWin32API.GetWindowClassName(h).Equals("Button"))
{
if (_buttonTitles.Length > buttonTitleIndex)
{
// Changes the text of the specified window's title bar (if it has one).
// If the specified window is a control, the text of the control is changed.
// However, SetWindowText cannot change the text of a control in another application.
NativeWin32API.SetWindowText(h, _buttonTitles[buttonTitleIndex]); buttonTitleIndex++;
}
} // Get the handle of next control in current window.
h = NativeWin32API.GetWindow(h, GW_HWNDNEXT);
}
}
} public const int GW_CHILD = ;
public const int GW_HWNDNEXT = ; public class NativeWin32API
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, int flags);
[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, Int32 wCmd);
[DllImport("user32.dll")]
public static extern bool SetWindowText(IntPtr hWnd, string lpString);
[DllImport("user32.dll")]
public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); public static string GetWindowClassName(IntPtr handle)
{
StringBuilder sb = new StringBuilder(); // Retrieves the name of the class to which the specified window belongs
GetClassNameW(handle, sb, sb.Capacity);
return sb.ToString();
}
} } }
C#自定义MessageBox 按钮的Text的更多相关文章
- C#自定义按钮、自定义WinForm无边框窗体、自定义MessageBox窗体
C#自定义按钮.自定义WinForm无边框窗体.自定义MessageBox窗体 C#自定义Button按钮控件 效果展示 C#自定义Winform无边框窗体 效果展示 C#自定义无边框MessageB ...
- WPF 自定义 MessageBox (相对完善版 v1.0.0.6)
基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...
- Mono自定义图片按钮
首先,我们编写一个MyImageButton类,继承自LinearLayout public class MyPhoneImageButton:LinearLayout { private Image ...
- Android 自定义Button按钮显示样式(正常、按下、获取焦点)
现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的.今天学习自定义Button按钮样式.Button样式修改的是Button的背景 ...
- GridView控件中插入自定义删除按钮并弹出确认框
GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...
- WPF 自定义 MessageBox (相对完善版)
WPF 自定义 MessageBox (相对完善版) 基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...
- C#自定义Button按钮控件
C#自定义Button按钮控件 在实际项目开发中经常可以遇到.net自带控件并不一定可以满足需要,因此需要自定义开发一些新的控件,自定义控件的办法也有多种,可以自己绘制线条颜色图形等进行重绘,也可以采 ...
- flutter 隐藏返回按钮 自定义返回按钮
自定义返回按钮 //改变颜色 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leading: BackBu ...
- 自定义 MessageBox 组件
效果: 公共组件页面: js部分: <script> export default { props: { title: { type: String ...
随机推荐
- HDU5726 GCD
Give you a sequence of N(N≤100,000)N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000)a1,...,an( ...
- JVM虚拟机系列(二)虚拟机的逻辑结构
这个节日,终于把之前看的断断续续的JVM看的差不多了,在这里做一份笔记吧. JVM支持的数据类型: JVM运行时的数据区:
- Javascript 表达式和运算符
属性访问表达式: var o = {x:1, y:{z:3}};//示例对象 var a = [o, 4, [5,6]];//包含对象的数组 console.log(o["x"]) ...
- 【转】UGUI文本字体颜色和大小等代码修改
http://blog.csdn.net/alayeshi/article/details/51842195 由于项目需求用UGUI在同个文本中显示出颜色和大小都不同的文字,我上网找个很久并没有见到相 ...
- wcf获取最新版本之后自己的东西没了
大概意思,点击显示所有文件,然后在那些文件上右击 包括在项目中就行了 图呢.......我当时添加进来的图呢
- BZOJ 3437:小P的牧场(DP+斜率优化)
小P的牧场[题目描述]背景:小P 是个特么喜欢玩MC 的孩纸...小P 在MC 里有n 个牧场,自西向东呈一字形排列(自西向东用1…n 编号),于是他就烦恼了:为了控制这n 个牧场,他需要在某些牧场上 ...
- WS-*协议栈及相关概念
1. 什么是WS-Security? WS-Security 是一个 SOAP 的扩展,它提供了对 SOAP 消息的认证和加密. 在介绍 WS-Security 之前,我们有必要了解一下 WS-Sec ...
- http长短连接和长短轮询
http长连接 http长连接是指http的请求头和响应头的均有connection: keep-alive的请求,也就是客户端和服务端均为keep-alive的请求. 实际上,http是请求/响应式 ...
- php函数注释
https://segmentfault.com/q/1010000003087072 phpstorm /**+回车
- set基本用法---1
#include<cstdio> #include<iostream> #include<cstdlib> #include<cmath> #inclu ...
