短消息提示Toast:
用于呈现一条短消息,提示完成后即可自动消失;
特点:
① 弹出式窗口;
② 短文本,可以折行显示;
③ 根据文本长度,自动调整大小;
④ 约1.5后,自动消失;
在Winform中,所有的窗口都用Form实现,比如:
① 常规窗口Window;
② 对话框窗口Dialog;
③ 悬浮窗口FloatWindow;
④ 工具提示Tooltip;
⑤ 弹出式窗口Popup(如菜单窗口、下拉列表窗口);
自定义一个Form类,即可定义一个窗口
public class myToast:Form
{
}
本窗口不需要边框,位置和大小都是自己控制的。
创建并显示窗口
myToast toast=new myToast();
toast.ShowMessage("This is a totas!");
子窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 弹窗提示
{
public partial class Toast : Form
{
private string message;
public Toast()
{
//无边框
this.FormBorderStyle = FormBorderStyle.None;
//背景白色
this.BackColor = Color.White;
}
public void ShowMessage(string message)
{
this.message = message;
//手动指定位置
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(0, 0);
this.Size = new Size(300, 100);
//显示窗口
this.Show();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
int w = this.Width, h = this.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
rect.Inflate(-4, -4);
//平滑绘制,反锯齿
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
if(message!=null)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(Color.Black);
g.DrawString(message, this.Font, brush, rect, sf);
brush.Dispose();
}
}
}
}
父窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 弹窗提示
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Toast toast = new Toast();
toast.ShowMessage("This is a toast!");
}
}
}
toast窗口显示时,相当于主窗口位置居中。
① 找到主窗口,得到主窗口的位置;
② 计算toast窗口的位置;
toast窗口显示时,不剥夺主窗口的焦点,需要重写ShowWithoutActivation
protected override bool ShowWithoutActivation
{
get
{
return true;
}
}
① 已知一个控件Control,可以得到它所在的窗口;
Form form=ctrl.FindForm();
子窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 窗口位置
{
public partial class Toast : Form
{
private string message;
public Toast()
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.White;
this.ShowInTaskbar = false;
}
//owner 可以是子窗口也可以是控件
public void ShowMessage(Control owner,string message)
{
this.message = message;
this.StartPosition = FormStartPosition.Manual;
this.Size = new Size(300, 100);
//找到owner所在的顶级窗口
Form form = owner.FindForm();
this.Owner = form;
//使toast窗口相对主窗口居中
Rectangle fr = new Rectangle(form.Location, form.Size);
int x = fr.X + (fr.Width - this.Width) / 2;
int y = fr.Y + (fr.Width - this.Height) / 2;
this.Location = new Point(x, y);
this.Show();
}
//焦点控制:本窗口不剥夺主窗口的焦点
//否则,当toast窗口激活时,主窗口焦点被剥夺
protected override bool ShowWithoutActivation
{
get { return true; }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
int w = this.Width, h = this.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
rect.Inflate(-4, -4);
//平滑绘制,反锯齿
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
if(message!=null)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(Color.Black);
g.DrawString(message, this.Font, brush, rect, sf);
brush.Dispose();
}
}
}
}
父窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 窗口位置
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Toast toast = new Toast();
toast.ShowMessage(button1, "中国人民解放军!");
}
}
}
Toast的窗口的大小,应该根据消息长度来调整,比如:
toast.ShowMessage(button1,"成功");
toast.ShowMessage(button1,"思密达");
toast.ShowMessage(button1,"编程是一种艺术");
使用Graphics.MeasureString()可以测算文本的尺寸
Graphics g=this.CreateGraphics();
SizeF size=g.MeasureString(str,this.Font,300);
g.Dispose();
注意,临时创建的Graphics用完之后需要手工销毁。
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
我正在使用Ruby,我正在与一个网络端点通信,该端点在发送消息本身之前需要格式化“header”。header中的第一个字段必须是消息长度,它被定义为网络字节顺序中的2二进制字节消息长度。比如我的消息长度是1024。如何将1024表示为二进制双字节? 最佳答案 Ruby(以及Perl和Python等)中字节整理的标准工具是pack和unpack。ruby的packisinArray.您的长度应该是两个字节长,并且按网络字节顺序排列,这听起来像是n格式说明符的工作:n|Integer|16-bitunsigned,network(bi
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]
我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束
RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)
我想用这两种语言中的任何一种(最好是ruby)制作一个窗口管理器。老实说,除了我需要加载某种X模块外,我不知道从哪里开始。因此,如果有人有线索,如果您能指出正确的方向,那就太好了。谢谢 最佳答案 XCB,X的下一代API使用XML格式定义X协议(protocol),并使用脚本生成特定语言绑定(bind)。它在概念上与SWIG类似,只是它描述的不是CAPI,而是X协议(protocol)。目前,C和Python存在绑定(bind)。理论上,Ruby端口只是编写一个从XML协议(protocol)定义语言到Ruby的翻译器的问题。生