当前位置:好百科>百科知识>WinForm,消息框怎样显示在窗体正中?

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

2024-10-19 13:13:35 编辑:zane 浏览量:526

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

的有关信息介绍如下:

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(); } }

版权声明:文章由 好百科 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.haobaik.com/article/166857.html
热门文章