2013-04-30 187 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