日期:2014-05-18  浏览次数:21025 次

C# MessageBox处理
在MessageBox上我想实现几个效果:
1、正常情况下,弹出MessageBox后会把焦点放在某个按钮上,请问有没有办法把MessageBox上的按钮都去掉或者把焦点移开, 不然回车后会关闭MessageBox;
2、能不能在MessageBox里添加一处理—只能通过某个快捷键(如F3)退出MessageBox,其他任何操作都是无效的。
还请各位朋友指点一下!!!谢谢...

------解决方案--------------------
不要用messageBox 就用自定义窗体,[form2]
C# code

//有关自定义窗体的代码。
//关闭 this.Close()l
//按键 Key_Press 事件
//....


Form2 f2 = new Form2();
f2.ShowDialog();

------解决方案--------------------
MessageBox太简单,你可以自定义一个弹出窗体,快捷键和样式可以自己随便定义
------解决方案--------------------
1、不能,顶多只是默认焦点在那个按钮。
2、不能,自己弄个Form吧
------解决方案--------------------
MessageBox功能有限 还是ShowDialog();一下吧 想怎么搞就怎么搞
------解决方案--------------------
这个真办不到.
刚用reflector跟踪了下MessageBox.Show()的源码.
最后是一个WindowsAPI,而不是集成自System.Window.Form;

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox(HandleRef hWnd, string text, string caption, int type);

建议楼主如果想实现上边的要求,单独做一个Form.只要添加KeyDown事件,根据输入作出操作就行了.

public class Box : Form
{
void Form_Load(object sender, EventArgs e)
{
//接收键盘消息
this.KeyPreview = true;
 
//默认焦点是按钮2,按钮2回车的时候关闭窗口,并返回结果DialogResult.Cancel;
Button b1 = new Button()
{Text="Button1"};
Button b2 = new Button()
{Text="Button2"};
b2.Click += delegate(object sender,EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
this.Controls.Add(b1);
this.Controls.Add(b2);
b2.Focus();
 
}

void Form_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.F3)
this.Close();
}

}