S
Sampathnarayanan
Guest
Hi Team,
I am trying to draw my custom caption and border to my form, So I have drawn by inheriting the Standard form, I have calculated the non client area and draw my border, In some cases I am receiving the unwanted border at the end of my form,
Actually I am running my sample in Windows 10 OS, but the form border is Windows 7 OS. I have calculated the nonclient area and drawn the border in Non client paint, please refer below code example. Also see that the status strip is clipped by that new border.
The border is drawn from the base, please guide us how to solve this issue, Share us the guidelines to remove the unwanted border in my custom form.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case NativeMethods.WM_NCCALCSIZE:
if (OnWmNcCalcSize(ref m)) return;
break;
case NativeMethods.WM_NCPAINT:
if (OnWmNcPaint(ref m)) return;
break;
case NativeMethods.WM_NCACTIVATE:
if (OnWmNcActivate(ref m)) return;
break;
}
base.WndProc(ref m);
}
private bool OnWmNcCalcSize(ref Message m)
{
NativeMethods.RECT rc = (NativeMethods.RECT)m.GetLParam(typeof(NativeMethods.RECT));
int iBorderWidth = 1;
rc.top += 30;
{
rc.left += iBorderWidth;
rc.right -= iBorderWidth;
rc.bottom -= iBorderWidth;
}
Marshal.StructureToPtr(rc, m.LParam, true);
m.Result = IntPtr.Zero;
return true;
}
private bool OnWmNcPaint(ref Message m)
{
IntPtr hdc = NativeMethods.GetWindowDC(this.Handle);
if (hdc != IntPtr.Zero)
{
NativeMethods.RECT rc = new NativeMethods.RECT();
NativeMethods.GetWindowRect((int)this.Handle, ref rc);
NativeMethods.GetActiveWindow();
IntPtr bufferDC = NativeMethods.CreateCompatibleDC(hdc);
if (bufferDC != IntPtr.Zero)
{
IntPtr hBmp = NativeMethods.CreateCompatibleBitmap(hdc, rc.Width, rc.Height);
if (hBmp != IntPtr.Zero)
{
IntPtr oldBmp = NativeMethods.SelectObject(bufferDC, hBmp);
using (Graphics bufferedGraphics = Graphics.FromHdc(bufferDC))
{
int iBorderWidth = 1;
DrawFrame(bufferedGraphics, new Rectangle(0, 0, rc.Width, rc.Height));
int right = rc.Width - iBorderWidth;
if (right - iBorderWidth > 0)
{
int captionHeight = 30;
int bottom = rc.Height - iBorderWidth;
if (bottom - captionHeight > 0)
{
NativeMethods.ExcludeClipRect(hdc, iBorderWidth, captionHeight, right, bottom);
}
}
NativeMethods.BitBlt(hdc, 0, 0, rc.Width, rc.Height, bufferDC, 0, 0, SRCCOPY);
}
NativeMethods.DeleteObject(hBmp);
}
NativeMethods.DeleteDC(bufferDC);
}
NativeMethods.ReleaseDC(this.Handle, hdc);
}
m.Result = IntPtr.Zero;
return true;
}
const int SRCCOPY = 0x00CC0020;
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="rc"></param>
private void DrawFrame(Graphics g, Rectangle rc)
{
SmoothingMode smoothingMode = g.SmoothingMode;
g.SmoothingMode = SmoothingMode.AntiAlias;
if (this.WindowState != FormWindowState.Maximized)
{
Pen pen = new Pen(Color.Red, 1);
Rectangle rect = new Rectangle(rc.X, rc.Y, rc.Width - 1, rc.Height - 1);
g.DrawRectangle(pen, rect);
pen.Dispose();
}
g.SmoothingMode = smoothingMode;
}
private bool OnWmNcActivate(ref Message m)
{
bool bResult = false;
m_bActive = (m.WParam != IntPtr.Zero);
if (!m_bActive)
{
m_highlightedButton = SB__MAX;
}
if (this.IsVisible)
{
if (!this.IsMdiContainer)
NativeMethods.LockWindowUpdate(this.Handle);
base.WndProc(ref m);
NativeMethods.LockWindowUpdate(IntPtr.Zero);
Message msg = new Message();
msg.Msg = NativeMethods.WM_NCPAINT;
msg.HWnd = m.HWnd;
msg.WParam = (IntPtr)1;
msg.LParam = (IntPtr)0;
OnWmNcPaint(ref msg);
bResult = true;
}
return bResult;
}
Continue reading...
I am trying to draw my custom caption and border to my form, So I have drawn by inheriting the Standard form, I have calculated the non client area and draw my border, In some cases I am receiving the unwanted border at the end of my form,
Actually I am running my sample in Windows 10 OS, but the form border is Windows 7 OS. I have calculated the nonclient area and drawn the border in Non client paint, please refer below code example. Also see that the status strip is clipped by that new border.
The border is drawn from the base, please guide us how to solve this issue, Share us the guidelines to remove the unwanted border in my custom form.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case NativeMethods.WM_NCCALCSIZE:
if (OnWmNcCalcSize(ref m)) return;
break;
case NativeMethods.WM_NCPAINT:
if (OnWmNcPaint(ref m)) return;
break;
case NativeMethods.WM_NCACTIVATE:
if (OnWmNcActivate(ref m)) return;
break;
}
base.WndProc(ref m);
}
private bool OnWmNcCalcSize(ref Message m)
{
NativeMethods.RECT rc = (NativeMethods.RECT)m.GetLParam(typeof(NativeMethods.RECT));
int iBorderWidth = 1;
rc.top += 30;
{
rc.left += iBorderWidth;
rc.right -= iBorderWidth;
rc.bottom -= iBorderWidth;
}
Marshal.StructureToPtr(rc, m.LParam, true);
m.Result = IntPtr.Zero;
return true;
}
private bool OnWmNcPaint(ref Message m)
{
IntPtr hdc = NativeMethods.GetWindowDC(this.Handle);
if (hdc != IntPtr.Zero)
{
NativeMethods.RECT rc = new NativeMethods.RECT();
NativeMethods.GetWindowRect((int)this.Handle, ref rc);
NativeMethods.GetActiveWindow();
IntPtr bufferDC = NativeMethods.CreateCompatibleDC(hdc);
if (bufferDC != IntPtr.Zero)
{
IntPtr hBmp = NativeMethods.CreateCompatibleBitmap(hdc, rc.Width, rc.Height);
if (hBmp != IntPtr.Zero)
{
IntPtr oldBmp = NativeMethods.SelectObject(bufferDC, hBmp);
using (Graphics bufferedGraphics = Graphics.FromHdc(bufferDC))
{
int iBorderWidth = 1;
DrawFrame(bufferedGraphics, new Rectangle(0, 0, rc.Width, rc.Height));
int right = rc.Width - iBorderWidth;
if (right - iBorderWidth > 0)
{
int captionHeight = 30;
int bottom = rc.Height - iBorderWidth;
if (bottom - captionHeight > 0)
{
NativeMethods.ExcludeClipRect(hdc, iBorderWidth, captionHeight, right, bottom);
}
}
NativeMethods.BitBlt(hdc, 0, 0, rc.Width, rc.Height, bufferDC, 0, 0, SRCCOPY);
}
NativeMethods.DeleteObject(hBmp);
}
NativeMethods.DeleteDC(bufferDC);
}
NativeMethods.ReleaseDC(this.Handle, hdc);
}
m.Result = IntPtr.Zero;
return true;
}
const int SRCCOPY = 0x00CC0020;
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="rc"></param>
private void DrawFrame(Graphics g, Rectangle rc)
{
SmoothingMode smoothingMode = g.SmoothingMode;
g.SmoothingMode = SmoothingMode.AntiAlias;
if (this.WindowState != FormWindowState.Maximized)
{
Pen pen = new Pen(Color.Red, 1);
Rectangle rect = new Rectangle(rc.X, rc.Y, rc.Width - 1, rc.Height - 1);
g.DrawRectangle(pen, rect);
pen.Dispose();
}
g.SmoothingMode = smoothingMode;
}
private bool OnWmNcActivate(ref Message m)
{
bool bResult = false;
m_bActive = (m.WParam != IntPtr.Zero);
if (!m_bActive)
{
m_highlightedButton = SB__MAX;
}
if (this.IsVisible)
{
if (!this.IsMdiContainer)
NativeMethods.LockWindowUpdate(this.Handle);
base.WndProc(ref m);
NativeMethods.LockWindowUpdate(IntPtr.Zero);
Message msg = new Message();
msg.Msg = NativeMethods.WM_NCPAINT;
msg.HWnd = m.HWnd;
msg.WParam = (IntPtr)1;
msg.LParam = (IntPtr)0;
OnWmNcPaint(ref msg);
bResult = true;
}
return bResult;
}
Continue reading...