代码是一个重绘控件的类,我的问题是关于最后一个方法:
protected override void WndProc(ref Message m)这个方法覆盖了原有的 WndProc 是吧。方法里面定义了对消息 WM_PAINT = 0x000F 的处理方法,但是也只有对这一条消息的处理,那对其他消息是怎么处理的?public class SubWindow : NativeWindow
{
[DllImport("user32.dll")]
extern static IntPtr GetWindowDC(IntPtr hWnd); [DllImport("user32.dll")]
extern static int ReleaseDC(IntPtr hWnd , IntPtr hDC); private Control control;
private string text = "null";
private Color foreColor = SystemColors.ControlText;
private Font font = SystemFonts.MenuFont; public Font Font
{
set
{
font = value;
if (control != null)
control.Invalidate();
}
} public Color ForeColor
{
set
{
foreColor = value;
if (control != null)
control.Invalidate();
}
} public string Text
{
set
{
text = value;
if (control != null)
control.Invalidate();
}
} public Control Control
{
set
{
control = value;
if (control != null)
{
AssignHandle(control.Handle);
control.Invalidate();
}
}
} protected override void WndProc(ref Message m)
{
const int WM_PAINT = 0x000F;
base.WndProc(ref m);
if (control == null) return;
switch (m.Msg)
{
case WM_PAINT:
IntPtr vDC = GetWindowDC(m.HWnd);
Graphics vGraphics = Graphics.FromHdc(vDC);
StringFormat vStringFormat = new StringFormat();
vStringFormat.Alignment = StringAlignment.Center;
vStringFormat.LineAlignment = StringAlignment.Center;
vGraphics.DrawString(text , font , new SolidBrush(foreColor) , new Rectangle(0 , 0 , control.Width , control.Height) , vStringFormat);
ReleaseDC(m.HWnd , vDC);
break;
}
}
}

解决方案 »

  1.   

    base.WndProc(ref m);
    这不处理过了么。
      

  2.   

    base.WndProc(ref m);表示默认执行了原先的消息处理。若不需要默认处理,需要将该句去掉
      

  3.   

    噢,再问下,调用该类的窗口是 Form_Main我在 Form_Main( ) 中执行 progressBar1.Invalidate( );就不会产生重绘效果。
    要在 Form_Main_Load( ) 中执行 progressBar1.Invalidate( ) 才产生重绘效果why?
      

  4.   

    Form_Main
    构造函数,此时窗体尚未创建,没有重绘一说。
    progressBar1.Invalidate是发消息的。
      

  5.   

    说错
    progressBar1.Invalidate
    是调用api使区域无效,然后产生WM_PAINT消息的。