问题描述
以下是子窗口的实现代码,为了模拟安卓的Toas提示t效果//CSToast.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Text;usingSystem.Windows.Forms;namespaceCSToastTip{publicpartialclassCSToast:Form{privateTimertm=newTimer();privateFormfrmParent;privateLabellabel1;privatestringstrMsg;privateconstintdefHeight=22;privateconstintroundSize=10;publicColorclrBack;publicColorclrText;privatevoidInitializeComponent(Formfrm){frmParent=frm;this.label1=newSystem.Windows.Forms.Label();this.SuspendLayout();this.label1.AutoSize=true;this.label1.Location=newSystem.Drawing.Point(130,109);this.label1.Name="label1";this.label1.Size=newSystem.Drawing.Size(41,12);this.label1.TabIndex=0;this.label1.Text="label1";this.ClientSize=newSystem.Drawing.Size(284,262);this.Controls.Add(this.label1);this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;this.Name="CSToast";this.Load+=CSToast_Load;this.ResumeLayout(false);this.PerformLayout();if(null!=frmParent){this.TopMost=true;this.TopLevel=false;frmParent.Controls.Add(this);this.BringToFront();}}publicCSToast(Formfrm){InitializeComponent(frm);DefColor();strMsg="thisisatoastmessage";tm.Interval=1000;tm.Start();tm.Tick+=tm_Tick;}publicCSToast(stringmsg,Formfrm){InitializeComponent(frm);DefColor();strMsg=msg;tm.Interval=1000;tm.Start();tm.Tick+=tm_Tick;}publicCSToast(intinterval,stringmsg,Formfrm){InitializeComponent(frm);DefColor();strMsg=msg;tm.Interval=interval;tm.Start();tm.Tick+=tm_Tick;}publicvoidDefColor(){this.clrText=Color.White;this.clrBack=Color.Black;}voidtm_Tick(objectsender,EventArgse){this.Close();}privatevoidSetWindowRegion(){System.Drawing.Drawing2D.GraphicsPathFormPath;FormPath=newSystem.Drawing.Drawing2D.GraphicsPath();Rectanglerect=newRectangle(0,defHeight,this.Width,this.Height-defHeight);FormPath=GetRoundedRectPath(rect,roundSize);this.Region=newRegion(FormPath);}privateGraphicsPathGetRoundedRectPath(Rectanglerect,intradius){intdiameter=radius;RectanglearcRect=newRectangle(rect.Location,newSize(diameter,diameter));GraphicsPathpath=newGraphicsPath();//左上角path.AddArc(arcRect,180,90);//右上角arcRect.X=rect.Right-diameter;path.AddArc(arcRect,270,90);//右下角arcRect.Y=rect.Bottom-diameter;path.AddArc(arcRect,0,90);//左下角arcRect.X=rect.Left;path.AddArc(arcRect,90,90);path.CloseFigure();returnpath;}protectedoverridevoidOnResize(System.EventArgse){this.Region=null;SetWindowRegion();}privatevoidCSToast_Load(objectsender,EventArgse){using(Graphicsgraphics=CreateGraphics()){Fontf=newFont("楷体",14F,System.Drawing.FontStyle.Regular);Sizesif=TextRenderer.MeasureText(graphics,strMsg,f,newSize(0,0),TextFormatFlags.NoPadding);//MessageBox.Show(sif.Width.ToString());this.Size=newSize(sif.Width+defHeight*2,sif.Height+defHeight*2);label1.Text=strMsg;label1.Font=f;label1.Location=newPoint(defHeight,defHeight*3/2);if(null!=clrBack){this.BackColor=clrBack;}if(null!=clrText){label1.ForeColor=clrText;}}if(null!=frmParent){this.Location=newPoint(frmParent.Location.X+frmParent.Width/2-this.Width/2,frmParent.Location.Y+frmParent.Height/2-this.Height/2);}else{this.CenterToScreen();}BringToFront();}}}
然后在另一个窗口中调用,但是窗口不显示privatevoidbutton1_Click(objectsender,EventArgse){CSToastts=newCSToast(1000,"testmessage",this);ts.Show();}
解决方案
解决方案二:
自己发现了问题。。。。。if(null!=frmParent){this.Location=newPoint(frmParent.Location.X+frmParent.Width/2-this.Width/2,frmParent.Location.Y+frmParent.Height/2-this.Height/2);}
这一行,Location的定位是相对容器的,所以应该改成if(null!=frmParent){this.Location=newPoint(frmParent.Width/2-this.Width/2,frmParent.Height/2-this.Height/2);}