2015-05-28 153 views
0

更新:由於最初提出這個問題,我稍微改變了我的方法。而不是使用System.Drawing.Graphics進行繪圖,而是使用InkCanvas託管WPF用戶控件。這就是我需要它做的一切。問題仍然是我無法獲得ElementHost的背景透明。我看到了我之前看到的同一個黑色方塊。使用C繪製3D渲染場景#

原文出處:我有一個C#WinForms應用程序,它使用Ogre3D將3D場景呈現給使用該面板句柄的窗體中的面板。我正在嘗試使用C#的System.Drawing.Graphics添加在該場景上繪製的能力(想象邁登在電視屏幕上繪圖)。

我正在使用BufferedGraphics類來做到這一點。作爲一個測試,我試圖簡單地在3D場景上繪製一個矩形。以下是我用來設置所有內容的代碼片段。

namespace TestApp 
{ 
    public partial class TestForm 
    { 
     private BufferedGraphics graphicsBuffer; 
     private BufferedGraphicsContext bufferContext = BufferedGraphicsManager.Current; 

     public TestForm() 
     { 
      InitializeComponent(); 
      UpdateGraphicsBuffer(); 
     } 

     private void UpdateGraphicsBuffer() 
     { 
      bufferContext.MaximumBuffer = new Size(panelRender.Width + 1, panelRender.Height + 1); 
      graphicsBuffer = bufferContext.Allocate(Graphics.FromHwnd(panelRender.Handle), new Rectangle(49, 49, 100, 100)); 
      graphicsBuffer.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
     } 

     private void TestForm_Load(object sender, EventArgs e) 
     { 
      graphicsBuffer.Graphics.DrawRectangle(new Pen(Color.Red), 50, 50, 50, 50); 
     } 
    } 
} 

我留下了很多專有代碼(有在該專有代碼部分graphicsBuffer.Render();的調用),並更名爲一些東西,但我希望我所提供給你的要點。另外,3D場景也使用panelRender.Handle來繪製該面板,並且panelRender.BackColor是黑色的。

概括地說,我所看到的是我的3D場景的失蹤裏面繪製的50×50的紅色矩形塊(具體爲100×100塊),如右圖所示,在這裏:

drawing problem

很顯然,我不想失去我試圖在上面畫的場景。現在,我不知道爲什麼會發生這種情況。我試圖做的是不可能的?如果需要任何附加信息/代碼,我會很樂意提供,如果可能的話。

編輯: 爲了嘗試簡化問題,我創建了一個非常簡單的WinForms應用程序,它具有一個面板並使用上面的代碼重新創建問題。代碼隱藏在這裏:

using System.Drawing; 
using System.Windows.Forms; 

namespace DoubleBufferTest 
{ 
    public partial class Form1 : Form 
    { 
     private BufferedGraphics graphicsBuffer = null; 
     private BufferedGraphicsContext bufferContext = BufferedGraphicsManager.Current; 

     public Form1() 
     { 
      this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true); 
      this.UpdateStyles(); 
      InitializeComponent(); 
      UpdateGraphicsBuffer(); 
     } 

     private void UpdateGraphicsBuffer() 
     { 
      bufferContext.MaximumBuffer = new Size(panel1.Width + 1, panel1.Height + 1); 
      graphicsBuffer = bufferContext.Allocate(Graphics.FromHwnd(panel1.Handle), new Rectangle(10, 10, 50, 50)); 
     } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e); 
      graphicsBuffer.Graphics.DrawRectangle(new Pen(Color.Red, 3.0f), 20, 20, 10, 10); 
      graphicsBuffer.Render(); 
     } 
    } 
} 

面板的背景色設置爲透明。下面是結果:

Test app

那黑色方塊對應,這是獲得上下文分配的圖形緩衝區。爲什麼它總是顯示爲黑色基本上是什麼使我困惑...

回答

0

謝謝@Ron Beyer回答這個對我來說different question我張貼。

所以我在ElementHost控制的正確軌道上,但由於透明度問題,羅恩建議我嘗試使用一個新的WPF窗口,我可以覆蓋應用程序頂部(link)。 WPF窗口的背景設置爲透明,因此在InkCanvas上繪圖會產生所需的效果。

0

我認爲你應該做的是使用SetStyle方法將背景設置爲透明。更多的信息在https://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle%28v=vs.110%29.aspx

+0

我認爲你應該在完成所有這些後將控件的背景顏色設置爲透明。 this.BackColor = Color.Transparent; – BVintila

+0

使用'SetStyle()'不適合我。我下面的代碼添加到我的窗體構造函數: 'this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint,真);'' this.UpdateStyles();' 也許我用錯了? – WERUreo

+0

對不起,我試圖重新格式化我的評論,並最終刪除它並重新開始。我也嘗試添加你建議的將BackColor設置爲透明的那一行,但這也不起作用,我發現這很奇怪,因爲在Designer中,背景顏色現在確實是透明的,沒有任何黑色的跡象。但是當我運行時,我仍然看到一個黑色方塊內的紅色矩形.. – WERUreo