AABBbaby

导航

Winforms平台界面开发技巧分享——如何允许用户跳过消息

下载DevExpress v19.2完整版 

DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅、美观且易于使用的应用程序。想要体验?点击下载>>

Message Boxes是DevExpress WinForms产品组合中高价值实用程序控件的完美示例。由于大多数应用程序都是通过消息框与用户进行通信的,所以WinForms Message Box控件将在即将发布的v20.1中出现,本文主要将为大家阐述此功能。

DevExpress XtraMessageBox对象与默认的WinForms消息相对应,XtraMessageBox的主要优势在于其使用DevExpress应用程序皮肤的功能,尽管这是一个重要的优势,但这绝不是XtraMessageBox的唯一优势。

DevExpress Winforms使用技巧教程

虽然您可以使用标准方法显示消息(将文本字符串和按钮设置为静态XtraMessageBox.Show()方法重载),但您也可以创建XtraMessageBoxArgs对象并将其作为唯一的Show方法参数传递。 此对象允许您合并其他操作,例如您可以显示嵌入式计时器到期时自动关闭的消息。

DevExpress Winforms使用技巧教程

 

XtraMessageBoxArgs args = new XtraMessageBoxArgs();
args.AutoCloseOptions.Delay = 5000;
args.Caption = "Auto-close message";
args.Text = "This message closes automatically after 5 seconds.";
args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel };
//show a countdown on a default button
args.AutoCloseOptions.ShowTimerOnDefaultButton = true;
XtraMessageBox.Show(args);

 

XtraMessageBoxArgs.Showing事件允许您在屏幕上显示消息表单之前对其进行访问和修改,您可以将其用于各种各样的事情,例如限制消息宽度。

DevExpress Winforms使用技巧教程

 

XtraMessageBoxArgs args = new XtraMessageBoxArgs();
args.Caption = "A very long message";
args.Text = "A message box attempts to show all of its text content in one line. " +
"If you do not limit the form size, this message will be very long and thin. " +
"Set the maximum form width and the form will wrap this long text.";
args.Buttons = new DialogResult[] { DialogResult.OK, DialogResult.Cancel};
args.Showing += Args_Showing;
XtraMessageBox.Show(args);

private void Args_Showing(object sender, XtraMessageShowingArgs e)
{
e.Form.MaximumSize = new Size(600, 600);
}

 

...或自定义消息按钮(更改标题或向其提供矢量图像)。

DevExpress Winforms使用技巧教程

 

XtraMessageBoxArgs args = new XtraMessageBoxArgs();
args.Caption = "A message with icons";
args.Text = "This message displays custom SVG images in its buttons";
args.Buttons = new DialogResult[] {
DialogResult.OK, DialogResult.Cancel, DialogResult.Retry };
args.Showing += Args_Showing;
XtraMessageBox.Show(args);

void Args_Showing(object sender, XtraMessageShowingArgs e) {
foreach (var control in e.Form.Controls)
{
SimpleButton button = control as SimpleButton;
if (button != null)
{
button.ImageOptions.SvgImageSize = new Size(16, 16);
button.ImageOptions.ImageToTextAlignment = ImageAlignToText.LeftCenter;
//button.Height = 25;
switch (button.DialogResult)
{
case (DialogResult.OK):
button.ImageOptions.SvgImage = svgImageCollection1[0];
break;
case (DialogResult.Cancel):
button.ImageOptions.SvgImage = svgImageCollection1[1];
break;
case (DialogResult.Retry):
button.ImageOptions.SvgImage = svgImageCollection1[2];
break;
}
}
}
}

 

使用v20.1,您将能够轻松地在消息中包含"Do not show this message again" 复选框,如果您想在项目中加入此功能,只需将布尔值XtraMessageBoxArgs.DoNotShowAgainCheckBoxVisible设置为true,复选框文本也是可自定义的。

DevExpress Winforms使用技巧教程

 

XtraMessageBoxArgs args = new XtraMessageBoxArgs();
args.Caption = "Message";
args.Text = "You are using a trial version. The trial period expires in 30 days";
args.DoNotShowAgainCheckBoxVisible = true;
args.DoNotShowAgainCheckBoxText = "Do not remind me again";
XtraMessageBox.Show(args);

 

此复选框本身不会执行任何操作,当消息出现在屏幕上(或被消除)时,它将引发Load和Closed事件。 您需要处理这些事件来存储和检索e.Visible属性值,此属性值指定用户是否选择隐藏消息。如果Load事件参数收到e.Visible属性值为false,则该消息被取消。

 

args.Load += Args_Load;
args.Closed += Args_Closed;

void Args_Closed(object sender, XtraMessageBoxClosedArgs e) {
//save e.Visible to a database or a local storage file
}

void Args_Load(object sender, XtraMessageBoxLoadArgs e) {
//retireve the value from a database or a local storage file
e.Visible = value;
}

 

与e.Visible一起,您还可以存储e.DialogResult属性值,它对应于关闭消息时使用的最后一个已知DialogResult。如果消息被抑制,则可以使用此值,以便Show方法返回上一个用户选择,而不是DialogResult.None。

 

void Args_Load(object sender, XtraMessageBoxLoadArgs e) {
e.Visible = _restoredVisibleValue_;
if (!e.Visible)
{
//restore the e.DialogResult property
e.DialogResult = _restoredDialogResultValue_;
}
}

 

对于那些不想手动保存和还原这些参数的用户,为您提供将其存储在注册表中的选项。 为此,请在Closed事件上调用SaveToRegistry方法,并在加载时调用RestoreFromRegistry。

 

void Args_Closed(object sender, XtraMessageBoxClosedArgs e) {
e.SaveToRegistry();
}

void Args_Load(object sender, XtraMessageBoxLoadArgs e) {
e.RestoreFromRegistry();
}

 

此代码将DialogResult和Visible键保存在Computer \ HKEY_CURRENT_USER \ Software \ X \ Y路径下,其中:

  • XtraMessageBox.RegistryPath属性的X - value;或未设置该属性,则为Path.Combine(Application.CompanyName,Application.ProductName,“ Messages”);
  • XtraMessageBoxArgs.RegistryKey属性的Y - value,或自动生成的ID。
DevExpress Winforms使用技巧教程

最后即使用户选择隐藏消息,也可以强制显示消息。 为此请在Load事件处理程序中调用e.ShowMessage方法,布尔方法参数指定是否应选中"Do not show again" 复选框。


DevExpress Dashboard控件实操公开课4月即将开启,

专家名师在线直播,免费听课名额先到先得~

DevExpress技术交流群:540330292      欢迎一起进群讨论

扫描关注DevExpress中文网微信公众号,及时获取最新动态及最新资讯

DevExpress中文网微信

posted on 2020-04-15 10:54  AABBbaby  阅读(316)  评论(0编辑  收藏  举报