|
|
От: |
Indifferent
|
|
| Дата: | 20.04.06 06:55 | ||
| Оценка: | 44 (2) | ||
механизм Subclassing'a, при котором подменяется оконная процедура и следовательно все сообщения сначала приходят в новую процедуру, а затем (по усмотрению) уже в оригинальную. В .NET такая функциональность обеспечивается при помощи наследования от класса System.Windows.Forms.NativeWindow
using System;
using System.Windows.Forms;
using System.Drawing;
class Demo
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
public class MainForm : Form
{
private DateTimePicker dtp1;
public MainForm()
{
this.Name = "MainForm";
this.Text = "Demo";
this.Size = new Size(400, 350);
dtp1 = new DateTimePicker();
dtp1.Location = new Point(12,12);
dtp1.BackColor = Color.Aqua;
this.Controls.Add(this.dtp1);
dtpNativeWindow myNativeWindow = new dtpNativeWindow(dtp1);
}
}
class dtpNativeWindow : NativeWindow
{
private Control _control = null;
public dtpNativeWindow(Control control)
{
_control = control;
control.HandleCreated += new EventHandler(OnHandleCreated);
control.HandleDestroyed += new EventHandler(OnHandleDestroyed);
}
void OnHandleCreated(object sender, EventArgs e)
{
AssignHandle(((Control)sender).Handle);
}
void OnHandleDestroyed(object sender, EventArgs e)
{
ReleaseHandle();
}
const int WM_ERASEBKGND = 0x14;
protected override void WndProc(ref Message m)
{
if(m.Msg == WM_ERASEBKGND && _control.Enabled)
{
using(Graphics g = Graphics.FromHdc(m.WParam))
{
g.FillRectangle(new SolidBrush(_control.BackColor), _control.ClientRectangle);
}
return;
}
base.WndProc(ref m);
}
}
}