具有动态宽度的 WinForms Graphics PictureBox
我正在制作一个 winform .NET 应用程序。它必须以条形格式显示图形。我使用图片框是因为这是我知道如何做到这一点的唯一方法(如果有人知道更好的方法,请告诉我)。
我使用以下代码动态添加线条(图形的条形):
int currentX = this.lineAmmount * (lineWidth + lineMargin);
pictureBox.CreateGraphics().DrawLine(new Pen(color, lineWidth), //Pen
currentX, pictureBox.Height, //Starting (x, y)
currentX, pictureBox.Height - Convert.ToInt32(value * graphicsScale)); //Ending (x, y)
this.lineAmmount++;
效果非常完美。
我现在想要的是图片框有一个水平滚动条。所以我将 pictureBox 放入带有 autoscroll = true 的面板中。现在我需要它来动态增加图片框的宽度。所以我在添加每一行后添加了这段代码:
pictureBox.Width = Math.Max(this.lineAmmount * (lineWidth + lineMargin), 205);
(205是我想要的最小宽度)。
该代码也很好用。宽度增加。在第一行中,Math.Max 始终返回 205,在几行之后,它开始返回其他值。从那一刻起,所有的线条都消失了!
请帮忙!!
预先感谢并抱歉我的英语不好,
迭戈
I'm making a winform .NET app. It must show a graphic in bars format. I'm using a picturebox because it's the only way I know how to do it (if someone knows a better way, please tell me).
I'm adding dynamically the lines (the bars of the graphic) with this code:
int currentX = this.lineAmmount * (lineWidth + lineMargin);
pictureBox.CreateGraphics().DrawLine(new Pen(color, lineWidth), //Pen
currentX, pictureBox.Height, //Starting (x, y)
currentX, pictureBox.Height - Convert.ToInt32(value * graphicsScale)); //Ending (x, y)
this.lineAmmount++;
That works just perfect.
What I want now is the pictureBox to have an horizontal scroll bar. So what I put the pictureBox into a panel with autoscroll = true. Now what I needed its to dynamically increase the pictureBox width. So I added this code after I add each line:
pictureBox.Width = Math.Max(this.lineAmmount * (lineWidth + lineMargin), 205);
(205 is the minimum width I want).
That code also works greate. The width is increased. With the first lines Math.Max always returns 205, after a couple of lines it starts returning the orher value. From that moment on ALL THE LINES DISAPPEAR!!!
Please help!!
Thanks in advance and sorry for my bad english,
Diego
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了图表控件。它会自动完成这一切。
I found out the Chart control. It does all that automatically.
您在第一个框中发布的代码写在哪里?是在控件的更新方法中吗?
当然,图表放在这里更合适
Where is written that code, that you posted in first box? Is it in update method of the control?
Sure, chart will be more appropriate here