المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Custom border color for Textbox is not working when using WS_EX_COMPOSITED style to the windows forms



C# Programming
06-30-2009, 12:50 PM
I am trying to give the custom boder color for the text box in C#. I am using XP operating system and visual studio 2008. I have subclassed the Textbox and override the WndProc method to handle the WM_PAINT message to give the custom border color to the text box.


// Text box code
public class SampleTextBox : TextBox
{
public SampleTextBox()
{
BorderStyle = BorderStyle.FixedSingle;
}

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x000F: // WM_PAINT
base.WndProc(ref m);

Control control = Control.FromHandle(Handle);
Graphics g = Graphics.FromHwnd(Handle);

ControlPaint.DrawBorder(g, control.ClientRectangle, Color.YellowGreen, ButtonBorderStyle.Solid);

g.Dispose();
break;
default:
base.WndProc(ref m);
break;
}
}
}

// Form code

public class SampleForm : Form
{
public SampleForm()
{
BackColor = Color.SteelBlue;
}

protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; /*WS_EX_COMPOSITED*/
return cp;
}
}
}


public class Form2 : SampleForm
{
private SampleTextBox textbox1;

public Form2()
{

textbox1 = new SampleTextBox();
textbox1.******** = new Point(50, 50);

this.Controls.Add(textbox1);

}
}


I am using WS_EX_COMPOSITED extened window style to my form to avoid the filckering because i am doing custom painting in my form. If i use WS_EX_COMPOSITED style with form , the border color of Text box comes with default color, that is black color while opening the form . But if i click and move the mouse in the non client area like border or titlebar of the form, the border color changes to color (Color.YellowGreen) which i specified in the Paint message of Textbox.


If I comment the "cp.ExStyle |= 0x02000000; /*WS_EX_COMPOSITED*/ " line, then the border always comes with the color what i specified in the Paint message of Textbox.

Can anyone please tell why this is happening and how can I achieve the custom border for the text box when WS_EX_COMPOSITED style applied to its parent control(form)?

Thanks in advance.

Mutpan.