0

我想弄清楚如何阻止表格在屏幕上绘画。我的意思是当我启动表单时,它最终不会绘制表单,因此不会显示界面。

我知道如何使用控件来做到这一点,但我无法弄清楚如何使用表单。我想发送一条消息阻止它绘画将是最好的选择,尽管我不确定哪个消息会创建初始绘画工作。

这是暂停绘制控件的方法。

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();
    }
}
4

3 回答 3

2

几个标准控件处理 WM_SETREDRAW。他们不会停止绘画,当你添加新项目或更改文本时,他们会停止刷新窗口。

这不是其他规定的行为,每个控件都以它认为合适的方式解释该消息。Form 和 Control 类没有任何内置的逻辑来改变它们的绘制方式。您必须自己实施。您不会使用消息处理程序 (WndProc) 这样做,您只需添加一个bool类型的公共属性。并且,例如,当 OnPaint 方法设置为 false 时,不绘制任何内容。等等。阻止父母重绘自己是不行的,不清楚你为什么要考虑这个。

于 2013-09-09T18:56:37.207 回答
0

我想出了我的问题的答案。它就像发送一条消息以阻止绘画发生并将其添加到 InitializeComponent() 和 OnPaint() 一样简单。

将它添加到 InitializeComponent() 将绘制表单,但会立即暂停它。仅将其添加到 onPaint 似乎无济于事,因此两者兼而有之。

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));
        }

    }
}
于 2013-09-10T17:15:26.040 回答