我想弄清楚如何从绘画屏幕上停止表单。我的意思是,当我启动表单时,它不会最终绘制表单,以便不显示界面。
我知道如何使用控件执行此操作,但我无法弄清楚如何处理表单。我正在考虑发送一条消息来阻止它绘画将是最好的选择,虽然我不确定哪条消息会创建最初的绘画工作。
以下是如何暂停控件的绘制。
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();
}
}
答案 0 :(得分:2)
一些标准控件处理WM_SETREDRAW。他们不会停止绘画,当他们添加新项目或更改文本时,他们会停止刷新窗口。
这不是其他规定的行为,每个控件都以其认为合适的方式解释该消息。表单和控件类不内置了任何改变它们绘制方式的逻辑。你必须自己实现它。您不会使用消息处理程序(WndProc)执行此操作,只需添加类型为 bool 的公共属性。而且,当它设置为false时,不会在OnPaint方法中绘制任何内容。等等。阻止父重绘自身是不行的,不清楚为什么要考虑这个。
答案 1 :(得分: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));
}
}
}