I'm trying to set my control over the Outlook Mailgrid with SetParent and resize (MoveWindow) and invalidate it if the Mailgrid changed.
This works, but on resize the control starts the flicker.
My Control is a replacement of Outlook's Mailgrid. To change the mailpreview i just change the selection of the original mailgrid via my control.
The Parent of my Control is the Outlook main-window. I have already tried to close the Mailgrid window, but that didn't help.
The flicker stops if I set the the Mailgrid as Parent, but in this case it flickers if i change the selection, and its not possible to draw my control over the mailsearch-window.
Does anybody know how to stop the extrem flicker?
Class to Handle changes of Outlook's Mailgrid Messages
sealed class SubWindow : NativeWindow
{
public event EventHandler Changed;
protected override void WndProc(ref Message m)
{
if (m.Msg == (int)(NativEnums.WindowMessage.WM_SIZE) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_WINDOWPOSCHANGED) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_WINDOWPOSCHANGING) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_ERASEBKGND) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_NCHITTEST) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_NCCALCSIZE) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_PAINT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_NCPAINT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_PRINT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_PRINTCLIENT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_SETREDRAW) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_MOVE)
)
{
OnChanged();
}
base.WndProc(ref m);
//I have already tried to ignore wm_paint, but it still painted
//if (m.Msg != (int)NativEnums.WindowMessage.WM_PAINT)
//{
// base.WndProc(ref m);
//}
}
private void OnChanged()
{
if (Changed != null)
Changed(this, null);
}
}
Creating the Event-Listener and Control and set it's parent
//Is the Class above
SubWindow lw = new SubWindow();
lw.AssignHandle(ListHandle);
lw.Changed += new EventHandler(lw_Changed);
//Gets the IntPtr of the Mailgrid
//MainWindow is the Outlook main-window window
IntPtr ListHandle = GetSizes.GetMailFolderIntPtr(MainWindow);
//Gets the Rectangle of the Mailgrid
System.Drawing.Rectangle listsize = GetSizes.GetMailfolderSize(MainWindow, ListHandle);
//mc is the Custom Con开发者_如何转开发trol
MoveWindow(mc.Handle, listsize.Left, listsize.Top, listsize.Width, listsize.Height, false);
SetParent(mc.Handle, MainWindow);
SetWindowLong(mc.Handle, (int)NativEnums.GetWindowLongConst.GWL_STYLE, (uint)(NativEnums.WindowStyles.WS_CHILD | NativEnums.WindowStyles.WS_VISIBLE));
On Change-Event
//Gets the Rectangle of the Mailgrid
System.Drawing.Rectangle listsize = GetSizes.GetMailfolderSize(MainWindow, ListHandle);
//Move and size the CustomControl to the Mailgrid Rectangle
MoveWindow(mc.Handle, listsize.Left, listsize.Top, listsize.Width, listsize.Height, false);
//Invalidate my Control
mc.Invalidate();
Painting of my Control
protected override void OnPaint(PaintEventArgs e)
{
DoPaint(e.Graphics);
//base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//base.OnPaintBackground(pevent);
}
public void DoPaint(Graphics g)
{
g.Clear(BackColor);
//Here comes the painting of the GridRows (only the visible rows)
}
/// EDIT
After adding a Thread.Sleep(1000)
to the the DoPaint methode i was able to see the paint order.
After resizing, my control shows up for an instant, afterwards the Outlook Mailgrid overpaints my Control.
I've tried to set base.WndProc(ref m);
befor OnChange();
but nothing changed.
I hope this helps to resolve the problem.
/// EDIT
After some testing I've tried to listen to all outlook windows and Graphics.Clear
them orange.
I don't know why, but even this doesn't work. While resizing nearly everything is drawn by outlook.
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
/////////////////////////////////////////////////////
// Here i clear the complete window (all windows) //
/////////////////////////////////////////////////////
using (Graphics g = Graphics.FromHwnd(m.HWnd))
g.Clear(Color.Orange);
if (m.Msg == (int)(NativEnums.WindowMessage.WM_SIZE) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_WINDOWPOSCHANGED) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_WINDOWPOSCHANGING) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_WINDOWPOSCHANGING) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_ERASEBKGND) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_NCHITTEST) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_NCCALCSIZE) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_PAINT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_NCPAINT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_PRINT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_PRINTCLIENT) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_SETREDRAW) ||
m.Msg == (int)(NativEnums.WindowMessage.WM_MOVE)
)
{
OnChanged();
}
}
I have solved the problem by adding the listener to the Outlook "Frame Splitter". For some reason Outlook sets the Mailgrid invisible and paints direct on the main window. (the main window doesn't get any windowmessage)
I now also paint on WM_WINDOWPOSCHANGING
WM_WINDOWPOSCHANGED
WM_SETREDRAW
my control over the main window.
精彩评论