Tag: winforms

使用按钮大小制作背景图像比例

我正在尝试将一些背景图像添加到我的Win Forms应用程序中的几个按钮。 这三个图像的大小不同(即像素尺寸不匹配,一个是128×128,另一个是256×256)。 我需要按钮的大小相同(否则GUI非常不对称)。 在不更改实际图像文件的情况下,如何使用按钮大小来缩放图像? 我已经尝试创建自己的类,并为按钮resize事件添加事件处理程序,但这似乎不起作用。 我的代码: class CustomButton : Button { internal void CustomButton_Resize( object sender, EventArgs e ) { if ( this.BackgroundImage == null ) { return; } var pic = new Bitmap( this.BackgroundImage, this.Width, this.Height ); this.BackgroundImage = pic; } } 并以forms: this.buttonOne.Resize += new System.EventHandler(this.buttonOne.CustomButton_Resize); 忘记提及,上面的代码根本没有调整图像的大小。 按钮仍需要具有不同的尺寸才能完全显示图像。

如何在显示表单时运行代码?

我认为Load事件可能有所帮助,但以下代码只是立即显示“Done”。 public Form1() { InitializeComponent(); Load += new EventHandler(Form1_Load); } void Form1_Load(object sender, EventArgs e) { System.Threading.Thread.Sleep(3000); Text = “Done”; } 表格显示后如何让它睡眠? 谢谢。

如何将数据从表单传递到另一个先前从任何其他表单实例化的表单?

我有三个表单(form1,form2,form3),我的主要表单是form1,从我打开form2获取一些数据,我在form2上有一个更新按钮,它将带我到form3,现在我想要无论用户在form3上写什么更新到form2,我怎样才能使用c#.net? (我使用showdialog()方法打开form2,form3) //reference to form2 Form2 SecondaryForm = new Form2(mainForm); SecondaryForm.ShowDialog(); //in the constructor of Form2 save the reference of Form1 Form1 form1 = null Form2(Form1 mainForm) { form1 = mainForm; } //then instead of creating a new MainForm again just use reference of Form1 form1.updateText(data); this.Close() 我使用了上面的代码但是我在form1.updateText(data)上得到了nullreferenceexception;

如何设置进度条的值

我用C#Winforms编写了一个用户控件。 在用户控件中,我有三个文本框: txtStartNumber – 输入的类型为:int。 txtEndNumber – 输入的类型为:int。 txtQuantity – iput的类型为:int。 (value = txtEndNumber – txtStartNumber) 进度条表示编号。 添加到数据库的记录及其总范围设置为等于txtQuantity。 当一个或多个记录重复时,进度条将停止。 我的问题是: 如何设置进度条的初始值? 如何管理进度条显示的进度? 我如何将其保存到数据库: for (long i = from; i < to; i++) { for (int j = 0; j < (to – from); j++) { arrCardNum[j] = from + j; string r = arrCardNum[j].ToString(); try […]

c#如何使用图形路径制作平滑的弧形区域

我正在尝试用圆边制作标签控件。 这是我在inheritance自Label的myclass中的OnPaint()重写方法中的代码。 protected override void OnPaint(PaintEventArgs e) { e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, GradientColor1, GradientColor2, 90); e.Graphics.FillRectangle(brush, new Rectangle(new Point(0, 0), new Size(this.Width, this.Height))); using (GraphicsPath gp = new GraphicsPath()) { gp.AddArc(new Rectangle(new Point(0, 0), new Size(this.Height, this.Height)), 90, 180); gp.AddLine(new Point(this.Height / […]

LinearGradientBrush无法正确呈现

请考虑标准System.Windows.Forms.Form的以下代码 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle test = new Rectangle(50, 50, 100, 100); using (LinearGradientBrush brush = new LinearGradientBrush(test, Color.Red, Color.Blue, 0f)) { e.Graphics.DrawRectangle(new Pen(brush, 8), test); } } 它产生了这个结果: 为什么红线和蓝线显示的顺序不正确,如何修复?

在resize时将图像绘制到Panel控件上会产生伪影

目前我正在尝试做我认为简单的任务: 在Windows窗体中的Panel控件的整个区域上绘制图像 。 (请忽略我可以使用BackgroundImage属性的那一刻) 要绘制的图像如下所示: 即一个黄色的盒子,周围有一个1像素的蓝色框架。 要绘制,我正在使用Panel控件的Paint事件: private void panel1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImage(Resources.MyImage, panel1.ClientRectangle); } 这在最初显示表单时看起来很好: 调整窗体大小(以及停靠的面板)时,它会在缩小时缩小边缘… …或者当它变得更大时它会绘制人工制品: 我很确定有一些相当简单和直接的东西,但我真的无法理解其中的原因。 因为我忽略了ClipRectangle并总是绘制所有内容,所以我认为图像会一直缩放。 我的问题是: 这些文物的原因是什么? (我喜欢理解这一点!) 为了摆脱人工制品,我该怎么做? (在每次resize时调用Invalidate旁边) 更新,解决方案: 感谢Ryan的回答 ,我找到了一个可接受的解决方案。 基本上我从Panel派生了一个类,重写了OnPaintBackground并且没有调用基本方法。 最后,我将以下代码添加到派生面板的构造函数中: base.DoubleBuffered = true; SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); UpdateStyles();

面板上的画线没有显示出来

我有一个名为panel1的Panel,我正在尝试使用以下代码在我的panel1上画一条线: Graphics g = panel1.CreateGraphics(); var p = new Pen(Color.Black, 3); var point1 = new Point(234,118); var point2 = new Point(293,228); g.DrawLine(p, point1, point2); 但没有任何东西出现。 有任何想法吗? 这是一个Windows窗体。

使用WebClient()计算要下载的总字节数

变量’totalBytes’始终为-1,因此我无法正确计算/更新进度条,为什么会发生这种情况? private void button1_Click(object sender, EventArgs e) { WebClient client = new WebClient(); client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); client.DownloadFileAsync(new Uri(“http://example.com/test.mp3″), @”E:\Test.mp3”); } private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { double bytesIn = double.Parse(e.BytesReceived.ToString()); label1.Text = Convert.ToString(bytesIn); double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); //stays at -1 label2.Text = Convert.ToString(totalBytes); double percentage = bytesIn / totalBytes […]

如何在两个不同的选项卡上显示相同的控件?

我正在使用VB.NET 我需要在2个不同的选项卡上显示相同的控件(ListBox)。 是否必须创建2个不同的ListBox实例?