0

I am trying to figure out how to stop a form from paintingon screen. What I mean by this is when I launch the form it doesn't end up painting the form so that the interface is not displayed.

I know how to do this with controls but I cannot figure out how to do with a Form. I am thinking sending a message to stop it from painting would be the best option although I am unsure of which message would create the initial paint job.

Here is how to suspend a control from being painted.

using System.Runtime.InteropServices;

class DrawingControl
{
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, 
                                         bool wParam, Int32 lParam);

    private const int WM_SETREDRAW = 11;

    public static void SuspendDrawing(Control parent)
    {
        SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
    }

    public static void ResumeDrawing(Control parent)
    {
        SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
        parent.Refresh();
    }
}
user1632018
  • 2,485
  • 10
  • 52
  • 87

3 Answers3

2

Several of the standard controls handle WM_SETREDRAW. They don't stop painting, they stop refreshing their window when, say, you add a new item or alter the text.

This is not otherwise prescribed behavior, every control interprets that message the way it sees fit. The Form and Control classes do not have any logic built in that alters the way they draw. You will have to implement that yourself. You would not do so with a message handler (WndProc), you'd just add a public property of type bool. And, say, not paint anything in the OnPaint method when it is set to false. Etcetera. Preventing the parent from redrawing itself is not okay, it isn't clear why you are contemplating this.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for the insight Hans. The reason I am trying to do this is so that I can paint my form into a buffer instead of onscreen. I know that when you set the form to double buffer it paints to the buffer first and then copies the image from the buffer onto the screen. I was hoping that I could find a way to have it paint to the buffer and then stop it from painting onto the screen. So I can access the buffer without having it paint to the screen surface. – user1632018 Sep 09 '13 at 19:04
  • That's already built-in, just set the DoubleBuffered property to true in the constructor. Getting the entire form plus all of its controls double-buffered requires a bit of [extra magic](http://stackoverflow.com/a/3718648/17034). Rather a mile removed from your question, I have to say. – Hans Passant Sep 09 '13 at 19:10
  • Thats what I was saying, I know that .net has the double buffer feature built in and thats what I wanted to take advantage of, but when you set it to double buffer in the constructer it will paint the window onscreen after it paints into the buffer. (like it should, it is double buffering afterall.) I wanted to stop it from painting the form onscreen after it paints to the buffer. – user1632018 Sep 09 '13 at 19:21
  • This makes little sense, double buffering is meant to buffer a paint to the screen. If you want to paint to a bitmap then you'll have to use Control.DrawToBitmap(). – Hans Passant Sep 09 '13 at 19:30
  • I understand double buffering is meant for that, but framebuffers are common for other uses. I figure I could take advantage of the built in double buffering feature and just stop it from painting on the screen. So basically it is only acting as a framebuffer. The reason why I wanted to take advantage of the built in Dbuffering is because it paints to the virtual/back buffer first then paints on screen. So this means it paints the form offscreen first. I don;t know any other way to just paint the form offscreen into a buffer, so I can access the form from a client type application. – user1632018 Sep 09 '13 at 19:37
  • The reasoning behind this is that I am trying to create a headless version of my application that can be acesses through a client on the network, but does not take up space on the server box. Kind of like virtual box does it. VirtualBpx headless paints it's viewport into a framebuffer that can be accessed by a remote client. – user1632018 Sep 09 '13 at 19:39
  • I'm going to stick with the actual question that was asked instead of trying to peel the layers off this quagmire. Good luck with it. – Hans Passant Sep 09 '13 at 19:42
  • Okay thank you. I realize the question is a little broad and I would need another approach to only write to the fb. – user1632018 Sep 09 '13 at 19:57
0

I figured out the answer to my question. It was as simple as sending a message to stop the painting from happening and adding it to both the the InitializeComponent() and OnPaint().

Adding it to just InitializeComponent() will paint the form, but immediately suspend it. Adding it to just onPaint seems to do nothing, so the winner was both.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SuspendDrawing(this);
        }

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, Int32 wMsg,
                                             bool wParam, Int32 lParam);

        private const int WM_SETREDRAW = 11;
        private const int WM_PAINT = 0xf;
        private const int WM_CREATE = 0x1;

        public static void SuspendDrawing(Form parent)
        {
            SendMessage(parent.Handle, WM_PAINT, false, 0);
        }

        public static void ResumeDrawing(Form parent)
        {
            SendMessage(parent.Handle, WM_PAINT, true, 0);
         //   parent.Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            SuspendDrawing((this));
        }

    }
}
user1632018
  • 2,485
  • 10
  • 52
  • 87