custom border for textbox on a tranaparent form problem


S

Sk

Hello,
I have a text box with custom border,

I am attaching the code for my control below.
Basically my derived control draws custom border in WM_NCPAINT message.

This control works fine when placed on a .net form. But when i change the
opacity of the form to anything other than 100%. I see black border on the
right and bottom side of the control.
Upon debugging I found that the GetWindowDC() call in wm_ncpaint do not
return the full control area, the portion which appears black is already
clipped.

interestingly native textbox control works fine.

Any solutions to the problem?
How can one change the size of the drawable area of the control.

Thanking you
SK

The code looks like something like this
namespace MyNameSpace

{

class MyTextBox1 : System.Windows.Forms.TextBox

{

[DllImport("user32.dll")]

static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]

static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

const int WM_NCCALCSIZE = 0x0083;

const int WM_NCHITTEST = 0x0084;

const int WM_NCPAINT = 0x0085;

struct RECT

{

public Int32 left;

public Int32 top;

public Int32 right;

public Int32 bottom;

};

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint (e);

//Debug.WriteLine("WM_NCPAINT: hWnd = "+Handle.ToString()+"WParam =
"+m.WParam.ToString());

IntPtr hDC = GetWindowDC(Handle);

if (hDC.ToInt32() == 0)

{

Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());

return;

}

Graphics g = Graphics.FromHdc(hDC);

Brush b = Brushes.Red;

Pen p = new Pen(b,5);

Rectangle r = new

Rectangle(0,0,this.Size.Width,this.Size.Height);

Debug.WriteLine("WM_NCPAINT: "+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);

g.DrawRectangle(p,r);


ReleaseDC(Handle,hDC);

}

protected override void WndProc(ref Message m)

{

switch(m.Msg)

{

case WM_NCPAINT:

{

base.WndProc(ref m);

Debug.WriteLine("WM_NCPAINT: hWnd = "+m.HWnd.ToString()+"WParam =
"+m.WParam.ToString());

//Region rgn = Region.FromHrgn(m.WParam);


IntPtr hDC = GetWindowDC(m.HWnd);

if (hDC.ToInt32() == 0)

{

Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());

break;

}

Graphics g = Graphics.FromHdc(hDC);

Brush b = Brushes.Red;

Pen p = new Pen(b,5);

Rectangle r = new

Rectangle(0,0,this.Size.Width,this.Size.Height);

Debug.WriteLine("WM_NCPAINT: "+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);

g.DrawRectangle(p,r);

m.Result = IntPtr.Zero;

ReleaseDC(m.HWnd,hDC);

return;

}

}

base.WndProc(ref m);

}

}

}
 
Ad

Advertisements

S

Sk

In contiuation with the message I find that
g.VisibleClipBounds rectangle returned when the transparency is set is
different than when the form is opaque. the reactangle returned in this case
is equal to the size of the client area.

But i found this little wierd.

Is there any way to increase the visibleClipBounds. graphics methods like
resetClip is not helping.

Sk said:
Hello,
I have a text box with custom border,

I am attaching the code for my control below.
Basically my derived control draws custom border in WM_NCPAINT message.

This control works fine when placed on a .net form. But when i change the
opacity of the form to anything other than 100%. I see black border on the
right and bottom side of the control.
Upon debugging I found that the GetWindowDC() call in wm_ncpaint do not
return the full control area, the portion which appears black is already
clipped.

interestingly native textbox control works fine.

Any solutions to the problem?
How can one change the size of the drawable area of the control.

Thanking you
SK

The code looks like something like this
namespace MyNameSpace

{

class MyTextBox1 : System.Windows.Forms.TextBox

{

[DllImport("user32.dll")]

static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]

static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

const int WM_NCCALCSIZE = 0x0083;

const int WM_NCHITTEST = 0x0084;

const int WM_NCPAINT = 0x0085;

struct RECT

{

public Int32 left;

public Int32 top;

public Int32 right;

public Int32 bottom;

};

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint (e);

//Debug.WriteLine("WM_NCPAINT: hWnd = "+Handle.ToString()+"WParam =
"+m.WParam.ToString());

IntPtr hDC = GetWindowDC(Handle);

if (hDC.ToInt32() == 0)

{

Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());

return;

}

Graphics g = Graphics.FromHdc(hDC);

Brush b = Brushes.Red;

Pen p = new Pen(b,5);

Rectangle r = new

Rectangle(0,0,this.Size.Width,this.Size.Height);

Debug.WriteLine("WM_NCPAINT:
"+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);
g.DrawRectangle(p,r);


ReleaseDC(Handle,hDC);

}

protected override void WndProc(ref Message m)

{

switch(m.Msg)

{

case WM_NCPAINT:

{

base.WndProc(ref m);

Debug.WriteLine("WM_NCPAINT: hWnd = "+m.HWnd.ToString()+"WParam =
"+m.WParam.ToString());

//Region rgn = Region.FromHrgn(m.WParam);


IntPtr hDC = GetWindowDC(m.HWnd);

if (hDC.ToInt32() == 0)

{

Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());

break;

}

Graphics g = Graphics.FromHdc(hDC);

Brush b = Brushes.Red;

Pen p = new Pen(b,5);

Rectangle r = new

Rectangle(0,0,this.Size.Width,this.Size.Height);

Debug.WriteLine("WM_NCPAINT:
"+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top