 |
|
|
 |
|
 |
Imported the stuff and compiled well on VS2010.
However, borders are still have native color.
In fact, the code never enters the following if statement, as hdc is alway equal to IntPtr.Zero (don't ask me why):
if (hdc != IntPtr.Zero)
{
Graphics graphics = Graphics.FromHdc(hdc);
Rectangle rectangle = new Rectangle(0, 0, width, height);
ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid);
message.Result = (IntPtr)1;
ReleaseDC(message.HWnd, hdc);
}
so I replaced this part of the code by this one :
try
{
Graphics graphics = Graphics.FromHdc(m.WParam);
Rectangle rectangle = new Rectangle(0, 0, width, height);
ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid);
m.Result = (IntPtr)1;
graphics.Dispose();
}
catch (Exception e) { }
That did the Trick.
BTW : IT IS crazy that MS did not put this natively in .Net Framework !!!!!!!!!
What a piss, dude !
modified on Wednesday, November 24, 2010 5:49 AM
|
|
|
|
 |
|
|
 |
|
 |
Hi,
I have a implemented my border panel on similar lines.But,when i do a resize,the borders are not changing properly.2 sides of the panel shows proper color and few sides show default color,i.e Color.empty.Here is my code
please help.
Amitha
#region Properties
/// <summary>
/// Gets or sets the color for the border
/// </summary>
[
Category("Appearance"),
Description("Specifies the color for the border. Leaving this property to default value, renders with themed colors"),
DefaultValue(typeof(Color), "127, 157, 185")
]
public Color BorderColor
{
get { return this.stroke; }
set
{
this.stroke = value;
Invalidate();
}
}
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of <see cref="BorderedPanel"/> control
/// </summary>
public BorderedPanel()
{
base.ResizeRedraw = true;
base.BorderStyle = BorderStyle.FixedSingle;
}
#endregion
#region Painting related
/// <override/>
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0085:
base.WndProc(ref m);
DrawBorder(ref m);
m.Result = IntPtr.Zero;
return;
//case 0x000F:
// base.WndProc(ref m);
// DrawBorder(ref m);
// m.Result = IntPtr.Zero;
// return;
} //end switch
base.WndProc(ref m);
}
/// <summary>
/// Draws the border
/// </summary>
protected virtual void DrawBorder(ref Message m)
{
// Render the border
Color colorToUse = (this.stroke == Color.Empty) ? BorderedPanel.ThemedBorderColor : this.stroke;
IntPtr hdc = BorderedPanel.GetWindowDC(m.HWnd);
using (Graphics g = Graphics.FromHdc(hdc))
{
using (Pen pen = new Pen(colorToUse))
g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
}
BorderedPanel.ReleaseDC(m.HWnd, hdc);
}
protected override void OnResize(EventArgs eventargs)
{
this.Refresh();
}
/// <summary>
/// Gets the themed border color
/// </summary>
/// <remarks>
/// I am not caching the themed border color, and use SystemEvents to maintain cache coherency.
/// If this is reported as a perf-hit, we ll worry about that.
/// </remarks>
protected internal static Color ThemedBorderColor
{
get
{
Color clr = Color.Empty;
if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser)
{
clr = VisualStyleInformation.TextControlBorder;
// I dont know why the text-border is different from ListBox/TreeView/Combobox borders
// for Metallic alone. Inspite of the anamoly Jim has suggested we stick to it.
if (VisualStyleInformation.ColorScheme == "Metallic")
clr = Color.FromArgb(127, 157, 185);
}
else
{
clr = Infragistics.Win.VisualStudio2005ColorTable.Colors.TabBorderColor;
}
return clr;
}
}
#endregion
|
|
|
|
 |
|
 |
I had this same problem with the top and left borders rendering the border color and the bottom and right borders rendering with the default color (black). It turned out that I had WS_EX_COMPOSITED style set on the parent container control. After I removed that extended style, my borders rendered the correct color for the entire control. I use VS 2008.
|
|
|
|
 |
|
 |
I should use WS_EX_COMPOSITED style to my form to avoid flickering. Is there any other ways to achieve the custom border?
|
|
|
|
 |
|
 |
Very pretty code. I think it must be usefull for many coders.
But here is one moment: for some controls calling Invalidate(); wont refresh control border just after color changed or if you change in your bordered control some bool hasBorder; this way:protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if ( hasBorder )
{
borderDrawer.DrawBorder(ref m, this.Width, this.Height);
}
}Instead of using Invalidate() in these controls you can use code like this: this.Parent.Invalidate(new Rectangle(this.Location.X - 1, this.Location.Y - 1, this.Width + 2, this.Height + 2), true);
Thanks !
"Bartender what is wrong with me Why am I so out of breath"
|
|
|
|
 |
|
 |
I was playing with this solution, after trying to do it myself and found that this solution does not support other font setting..
it only takes the default font "MS sans serif".
I know that this kind of solution interfere with the controls painting event and therefore does not allow changing the font.
can you test and let me know if you've got an idea?
Ron
Ron Maman
.NET Consultant
|
|
|
|
 |
|
 |
Great, thanks
|
|
|
|
 |
|
 |
When i change the Color of the BorderColor at Runtime to Color.Empty then the Border wont redraw with it =/
|
|
|
|
 |
|
 |
This does not work on Windows Vista. On line 24, if (hdc != IntPtr.Zero), hdc is actually Zero; GetDCEx does not return a HDC.
|
|
|
|
 |
|
 |
I confirm, the hdc value is always 0.
Can you help me to fix this?
Regards /// Angel
|
|
|
|
 |
|
 |
Yeah, a fix for Vista would be great...
|
|
|
|
 |
|
 |
I'm also looking for a fix for Windows Vista....
If somebody has solved the problem, please post the solution.
Cheers,
Johnny J.
|
|
|
|
 |
|
|
 |
|
 |
Thanks, I've been looking for something like this for a long time.
The code, however, works on WIndows XP but not on WIndows Server 2003.
Do you know how I can make it work?
|
|
|
|
 |
|
 |
Hi!
I didn't know about Windows Server 2003 problem, because I've tested only on XP.
And I have to say that it is not the best variant of painting borders, because it has some bugs. I am still searching another, better method.
Only I can help is: try to set breakpoint after this string:
if (message.Msg == WM_NCPAINT || message.Msg == WM_ERASEBKGND || message.Msg == WM_PAINT)
Maybe Windows Server 2003 has no such windows messages.
|
|
|
|
 |
|
 |
Hello dear Mr. Voldemar,
I guess it's not a windows2003 specific problem. I have the same problem, that it's not possible to paint custom border colors for textboxes.
First I solved it same way like you, BUT: With Win2003 Server there are ugly 3D-Borders and around them my custom border.
Problem is here same like you are using WinXP: The windows classic theme style!
With that theme style your (and mine too) solution looks ugly, because a inner 3D-border will appear. The solution to set BorderStyle property to "FixedSingle" does not work, because Windows will overpaint the custom border with it's default border.
I don't have any solution seen yet, but perhaps you have any idea about this?
Greetings,
NTR
|
|
|
|
 |
|
 |
If I use this text box on a Form that has set Opacity to a value smaller 1, e.g. 0.5 the border is not drawn correctly. Anyone else has that experience too? Any solutions? I couldn't find one.
Greetings
Maik
|
|
|
|
 |
|
 |
Very Elegant
Thanks.
KHadden
|
|
|
|
 |
|
|
 |