2012-02-22 79 views
2

我想的WinForms与SharpDX项目整合,才能在我的3D软件使用的WinForms(并最终通过HostElement WPF)。如何覆盖Winforms控件的paint方法以使其绘制为纹理?

我需要创建或配置控件或窗体,这样我可以:

一个。它渲染到纹理(我可以显示为子画面*)
湾在控件未处于活动状态时,过滤其输入以删除鼠标/键盘事件。

我已经尝试过控制和窗体的子类,来重写OnPaint和OnPaintBackground,但这些对子控件没有任何影响 - 或者就此而言,窗体边框(即使他们自己做得不够,因为我我仍然留下一个白色的广场,我认为'父'已经画出来了)。

我怎样才能阻止控件或窗体画到屏幕上而不是只画到一个位图?(例如,在绘制树之前有没有办法覆盖图形?)

*由于Winforms不支持,所以需要这样做(而不是让控件渲染到屏幕上)真正的透明度,所以我需要在我的像素着色器中剪裁彩色编码的像素。

(为了证实,我的意思并不是一个DirectX质地特别 - 我很高兴(其实宁愿)一个简单的System.Drawing中位图)

回答

3

这里是开始绕了一个办法:

  • 创建一个派生控件类,使我们可以公开哪些保护
  • 调用我们自定义的方法来获得控制的图像InvokePaint
  • 测试表单需要一个图片框和myButton的
  • 的实例


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

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() { InitializeComponent(); } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      // create image to which we will draw 
      var img = new Bitmap(100, 100); 

      // get a Graphics object via which we will draw to the image 
      var g = Graphics.FromImage(img); 

      // create event args with the graphics object 
      var pea = new PaintEventArgs(g, new Rectangle(new Point(0,0), new Size(100,100))); 

      // call DoPaint method of our inherited object 
      btnTarget.DoPaint(pea); 

      // modify the image with algorithms of your choice... 

      // display the result in a picture box for testing and proof 
      pictureBox.BackgroundImage = img; 
     } 
    } 

    public class MyButton : Button 
    { 
     // wrapping InvokePaint via a public method 
     public void DoPaint(PaintEventArgs pea) 
     { 
      InvokePaint(this, pea); 
     } 
    } 
} 
+0

显式调用InvokePaint的伟大工程,它现在呈现给我的质地,谢谢! – sebf 2012-02-23 19:32:23

+0

您也可以使用“DrawToBitmap”。我认为这将避免需要从控制中派生出来。 btnTarget.DrawToBitmap(IMG,新的Rectangle(新点(0,0),新尺寸(100100)); – chrispepper1989 2014-10-22 12:35:41