WinForm,消息框怎样显示在窗体正中?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WinForm,消息框怎样显示在窗体正中?相关的知识,希望对你有一定的参考价值。

你可以自定义一个消息框,代码我这里给你。你可以自己在这基础上扩展其他的功能,如果改造得好可以当广告栏为使用或是像QQ一样的信息弹出窗口。具体就不写了太详细反正你要的我做好了。其他的你自己改吧。呵呵。

1、先创建一个消息窗体免得还要写代码
public class MsgBox : Form

private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btClose;
private System.Windows.Forms.Label label1;
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)

if (disposing && (components != null))

components.Dispose();

base.Dispose(disposing);

public MsgBox(string text)

this.panel1 = new System.Windows.Forms.Panel();
this.btClose = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.Control;
this.panel1.Controls.Add(this.btClose);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 70);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(287, 46);
this.panel1.TabIndex = 1;
//
// btClose
//
this.btClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btClose.Location = new System.Drawing.Point(206, 7);
this.btClose.Name = "btClose";
this.btClose.Size = new System.Drawing.Size(75, 28);
this.btClose.TabIndex = 0;
this.btClose.Text = "确定";
this.btClose.UseVisualStyleBackColor = true;
this.btClose.Click += new System.EventHandler(this.btClose_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(123, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(43, 17);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
this.label1.SizeChanged += new System.EventHandler(this.label1_SizeChanged);
//
// MsgBox
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(287, 116);
this.Controls.Add(this.label1);
this.Controls.Add(this.panel1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(303, 154);
this.Name = "MsgBox";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
this.label1.Text = text;


private void label1_SizeChanged(object sender, EventArgs e)

this.ClientSize = new Size (this.label1.Width +50,this.label1.Height + 100);
this.label1.Location = new Point((this.ClientSize.Width - this.label1.Width) / 2,
(this.ClientSize.Height - this.label1.Height - 46) / 2);


private void btClose_Click(object sender, EventArgs e)

this.Close();


2、封装成静态类
public static class MessageBoxX

private static MsgBox _boxFrm;
public static void Show(string text)

_boxFrm = new MsgBox(text);
_boxFrm.ShowDialog();

public static void Show(string text, Form owner, FormStartPosition ShowLocation)

_boxFrm = new MsgBox(text);
_boxFrm.StartPosition = ShowLocation;
_boxFrm.ShowDialog();

参考技术A 100 窗体可以通过整个屏的宽、高显示在整个屏的正中,同样也可以显示在其它窗体的正中。消息框怎么办呢?当然我不希望拿一个窗体模式显示当作消息框。谢谢! 补充: 我要代码,MessageBox我只会Show出来,不知道怎么设定Location。 补充: 小弟,我也是从这周一开始搞WinForm,我原来也是搞Web开发的,举步维艰呀。我现在需要的是解决问题的方法,阿震那个我倒是看明白了,但是我的代码出了太大的问题,现在跑不起来,没法儿测试,得等一小会儿。我就烦没完没了的改需求,程序没完没了的重构。 补充: 阿震,依照你的说法,我没有搞定,但是我好像明白了。本回答被提问者采纳

以上是关于WinForm,消息框怎样显示在窗体正中?的主要内容,如果未能解决你的问题,请参考以下文章

Winform消息框中的可点击URL?

Winform消息框中的可点击URL?

winform中如何设置提示框在winform窗体中居中显示?

等待对话框实现

WinForm | C# 弹出简易的消息提示框 (仿Android Toast消息提示)

如何在窗口/消息框中显示500字的输出?