2013-04-30 190 views
3

我正在制作一个基本上是位图的音频谱图。要显示坐标轴(图形)我正在使用ZedGraph,基本上只是为了显示坐标轴,如上所述。一种帮手控制。然后我在上面显示位图。 当窗口默认大小时,此功能很好 - 但是,当我最大化它时,我会松动比例(请参见下面的图片)。这很难(不可能)使用锚定来完成。带边距的BackgroundImage

现在我想知道其他可以保持正确定位的选项。是否有可能为控件设置BackgroundImage(在我的情况下是ZedGraphControl)并设置边距?如果不是,我最好的选择是什么?

默认窗口大小(一切OK): enter image description here

最大化窗口(位图不填充ZedGraphControl): enter image description here

回答

1

在我看来有两个解决方案。第一个是自己将波形位图绘制到控件上(因此使用完整的可用空间,但稍微拉伸位图)。第二个是围绕您的控件构建一些面板并相应调整它们的大小(使图形总是以正确的高宽比出现,但浪费屏幕空间并且更复杂)。

我不知道zedgraph是如何工作的,但我介绍了以下内容。 从你写的是用户控件。我会做的是听取它的onPaint方法。 给你一个图形对象,你可以随意使用它来在控件上绘制任何东西(包括位图)。参考控件的大小,您可以轻松地绘制您的位图和相应的宽高比。

创建容器以容纳图形控件并添加四个面板,一个用于顶部,底部,左侧和右侧。就像这个形象:

现在,您可以将这些根据你所需要的长宽比调整。要做到这一点,你必须听两个事件,ResizeEnd事件(当用户完成控件的大小调整时调用)和一个事件来监听表单是否被最大化(example)。 需要被执行的代码如下:

 private void AdjustPanels(object sender, EventArgs e) 
     { 
     double desiredAspectRatio = 1; 

     // I am using the form itself as a reference to the size and aspect ration of the application. 
     // you can, of course, use any other control instead (e.g. a panel where you stuff all the other panels 
     int width = this.Width; 
     int height = this.Height; 
     double formAspectRatio = (double)width/(double)height; 

     int marginLeft=0, marginRight=0, marginTop=0, marginBottom=0; 

     if (desiredAspectRatio > formAspectRatio) 
     { 
      // high aspect ratios mean a wider picture -> the picture we want is wider than what it currently is 
      // so we will need a margin on top and bottom 
      marginLeft = 0; marginRight = 0; 
      marginTop = (int)((height - desiredAspectRatio * width)/2); 
      marginBottom = (int)((height - desiredAspectRatio * width)/2); 
     } 
     else 
     { 
      marginTop = 0; marginBottom = 0; 
      marginLeft = (int)((width - desiredAspectRatio*height)/2); 
      marginRight = (int)((width - desiredAspectRatio * height)/2); 
     } 

     pnlTop.Height = marginTop; 
     pnlBottom.Height = marginBottom; 
     pnlLeft.Width = marginLeft; 
     pnlRight.Width = marginRight; 
    } 

当然,你将与你的波形图像的宽高比,以取代“desiredAspectRation”的价值。 如果您需要进一步的帮助,只需发送一封私信给我您的电子邮件地址,我将向您发送完整的Visual Studio解决方案。

+0

首先,感谢您的帮助!现在我的工作量很大,我找不到解决我写的问题的时间。我会尽快审查你的解决方案,然后接受答案。 – 2013-05-14 09:27:33