2017-07-27 78 views
1

我有一個tablelayout與10x10(列x行),我可以下降對象到這些單元格。
我想添加一個圖像(作爲背景)到該桌面佈局,可以是可滾動並覆蓋所有表格單元格,用戶可以上傳任何圖像大小。winform中tablelayout的可滾動backgroundimage

拖放正在工作,我的問題是:我無法滾動tablebackgound圖像eventho我設置AutoScroll爲true。 任何想法如何解決這個問題

更新: 現在我可以滾動,但圖像顯示在錯誤的方式

private void addImage() 
     { 
      Image img = Image.FromFile(@"C:\Users\c1\Desktop\img_Clean.png"); 
      if (tableLayoutDropZone.BackgroundImage != null) tableLayoutDropZone.BackgroundImage.Dispose(); 
      tableLayoutDropZone.BackgroundImage = img; 
      tableLayoutDropZone.AutoScrollPosition = Point.Empty; 
      tableLayoutDropZone.AutoScrollMinSize = new Size(img.Width, img.Height); 

     } 

回答

1

你需要做的件事:

  • 套裝yourTLP.AutoScroll = true當然還有BackgroundImageLayout=None

  • 設置合適的AutoScrollMinSize;它應該有大小和你一樣BackgroundImage使用的圖像相同,即當圖像變化,你需要重新設置AutoScrollMinSize

  • 你需要編寫Paint事件包括此行:e.Graphics.DrawImage(yourTLP.BackgroundImage, yourTLP.AutoScrollPosition);

enter image description here

這裏是我的代碼加載一個新的形象:

Image img = Image.FromFile(someimagepath); 
if (yourTLP.BackgroundImage != null) yourTLP.BackgroundImage.Dispose(); 
yourTLP.BackgroundImage = img; 
yourTLP.AutoScrollPosition = Point.Empty; 
yourTLP.AutoScrollMinSize = new Size(img.Width, img.Height); 

注意,作爲TLP 滾動控件它擁有滾動以及!

如果你希望他們留下來固定,你可以這樣做:

  • 刪除上述所有設置和代碼
  • 創建具有非常相同設置和代碼
  • 一個PanelTLPBackColor設置爲Transparent
  • 巢它在Panel

您可能需要打開DoubleBuffering流暢的滾動..

+0

感謝您的答覆,但是當我滾動圖像內容雜亂請看看[鏈接](HTTP: //imgur.com/3vfDDu3)事件我超越了OnPaint方法,如你所說 –

+0

嗯,不在這裏。請更新問題以顯示完整的畫圖代碼! (順便說一句,我沒有看到圖像中的錯誤) – TaW

+0

請看看這個:[鏈接](http://imgur.com/a/T7Sj5) –