How to disable a User Control to draw it's background (or Region) <p>My question : <strong>How to disable a User Control to draw it's background (or Region)</strong></p> <p>Note : I already tried to override and empty OnPaintBackground or set background color to transparent.</p> <p>I'm trying to bypass winform paint for my custom user controls in a custom container. For that I thought to give a try to this : <a href="http://www.codeproject.com/Articles/409988/Beginners-Starting-a-2D-Game-with-GDIplus" rel="nofollow">Beginners-Starting-a-2D-Game-with-GDIplus</a></p> <p>My setup is :</p> <ul> <li>A Form containing: <ul> <li>A User control (DrawingBoard) </li> <li>A Container with elements I can drag and drop to this DrawingBoard (it's a listbox).</li> </ul></li> </ul> <p>My render loop is inside the DrawingBoard with all elements specified in the previous link.</p> <pre><code>public DrawingBoard() { InitializeComponent(); //Resize event are ignored SetStyle(ControlStyles.FixedHeight, true); SetStyle(ControlStyles.FixedWidth, true); SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);// True is better SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true); // True is better // Disable the on built PAINT event. We dont need it with a renderloop. // The form will no longer refresh itself // we will raise the paint event ourselves from our renderloop. SetStyle(System.Windows.Forms.ControlStyles.UserPaint, false); // False is better } #region GDI+ RENDERING public Timer t = new Timer(); //This is your BackBuffer, a Bitmap: Bitmap B_BUFFER = null; //This is the surface that allows you to draw on your backbuffer bitmap. Graphics G_BUFFER = null; //This is the surface you will use to draw your backbuffer to your display. Graphics G_TARGET = null; Size DisplaySize = new Size(1120, 630); bool Antialiasing = false; const int MS_REDRAW = 32; public void GDIInit() { B_BUFFER = new Bitmap(DisplaySize.Width, DisplaySize.Height); G_BUFFER = Graphics.FromImage(B_BUFFER); //drawing surface G_TARGET = CreateGraphics(); // Configure the display (target) graphics for the fastest rendering. G_TARGET.CompositingMode = CompositingMode.SourceCopy; G_TARGET.CompositingQuality = CompositingQuality.AssumeLinear; G_TARGET.SmoothingMode = SmoothingMode.None; G_TARGET.InterpolationMode = InterpolationMode.NearestNeighbor; G_TARGET.TextRenderingHint = TextRenderingHint.SystemDefault; G_TARGET.PixelOffsetMode = PixelOffsetMode.HighSpeed; // Configure the backbuffer's drawing surface for optimal rendering with optional // antialiasing for Text and Polygon Shapes //Antialiasing is a boolean that tells us weather to enable antialiasing. //It is declared somewhere else if (Antialiasing) { G_BUFFER.SmoothingMode = SmoothingMode.AntiAlias; G_BUFFER.TextRenderingHint = TextRenderingHint.AntiAlias; } else { // No Text or Polygon smoothing is applied by default G_BUFFER.CompositingMode = CompositingMode.SourceOver; G_BUFFER.CompositingQuality = CompositingQuality.HighSpeed; G_BUFFER.InterpolationMode = InterpolationMode.Low; G_BUFFER.PixelOffsetMode = PixelOffsetMode.Half; } t.Tick += RenderingLoop; t.Interval = MS_REDRAW; t.Start(); } void RenderingLoop(object sender, EventArgs e) { try { G_BUFFER.Clear(Color.DarkSlateGray); UIPaint(G_BUFFER); G_TARGET.DrawImageUnscaled(B_BUFFER, 0, 0); } catch (Exception ex) { Console.WriteLine(ex); } } #endregion </code></pre> <p>Then my elements get the event fired and try to draw what I would like:</p> <pre><code>public override void UIPaint(Graphics g) { Pen p = new Pen(Color.Blue,4); p.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset; g.DrawLines(p, new Point[] { new Point(Location.X, Location.Y), new Point(Location.X + Width, Location.Y), new Point(Location.X + Width, Location.Y + Height), new Point(Location.X, Location.Y + Height), new Point(Location.X, Location.Y - 2) }); g.DrawImageUnscaled(GDATA.GetWindowImage(), Location); } </code></pre> <p>here is what happening on my DrawingBoard :</p> <p>As I can't post image ... here is the link : <a href="http://s8.postimage.org/iqpxtaoht/Winform.jpg" rel="nofollow">http://s8.postimage.org/iqpxtaoht/Winform.jpg</a></p> <ul> <li>The background is DarkSlateGray because my G_BUFFER state to clear each tick with that, Ok</li> <li>The blue rectangle is what I paint, but it get cropped, KO</li> <li>The Texture is cropped, KO</li> <li>The region that crop my drawing is the control size.</li> </ul> <p>So from there I've tried everything I could to disable WinForm to make some magic drawing in background. Tried to override and empty everything that got paint/update/refresh/invalidate/validate on Form/DrawingBoard/Elements but nothing allowed me to get my texture or drawing to not get cropped by the control background : (</p> <p>I also tried to set the background of the Element as transparent and also to set Form.TransparencyKey = blabla with each element BackColor = blabla. But failed each time.</p> <p>I'm certainly missing something : / But I don't know what.</p>