473,659 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing border color?

Is there a simple way to change a UserControl border color?

I'm creating a UserControl that will have a border, using the
UserControl.Bor derStyle property. The standard border is black; I'd like to
change the color to something a little more appealing. I've tried overriding
OnPaint, like this:

public partial class MyControl : UserControl
{
public MyControl()
{
this.SetStyle(C ontrolStyles.Us erPaint, true);
InitializeCompo nent();
}

protected override void OnPaint(PaintEv entArgs e)
{
base.OnPaint(e) ;

if (this.BorderSty le == BorderStyle.Fix edSingle)
{
int borderWidth = 1;
Color borderColor = SystemColors.Co ntrolDark;
ControlPaint.Dr awBorder(e.Grap hics, e.ClipRectangle ,
borderColor, borderWidth, ButtonBorderSty le.Solid,
borderColor, borderWidth, ButtonBorderSty le.Solid,
borderColor, borderWidth, ButtonBorderSty le.Solid,
borderColor, borderWidth, ButtonBorderSty le.Solid);
}
}

}

That doesn't work, because it leaves the black border. Plus, UserPaint
setting apparently drops all control painting in my lap, when all I want to
do is change the border color.

Is there a P/Invoke or a Windows message I can use to change the border
color? Thanks.

--
David Veeneman
Foresight Systems
Aug 15 '06 #1
1 22496
I found my answer. The problem is that the border is not part of the client
area, which is what the OnPaint event args provide in e.Graphics. So, you
have to PInvoke GetDCEx() to get the window's device context. Then call
Graphics.FromHd c() to get a graphics object for the entire window, including
the non-client area on which the border is drawn. From there, it's drawing
as usual.

Here is some very simple code that draws a red border around a user control:

// PInvoke declaration
[DllImport("User 32.dll", EntryPoint = "GetDCEx")]
internal static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRgn, int flags);

// Event Override
protected override void OnPaint(PaintEv entArgs e)
{
base.OnPaint(e) ;

IntPtr hWnd = this.Handle;
IntPtr hRgn = IntPtr.Zero;
IntPtr hdc = this.GetDCEx(hW nd, hRgn, 1027);

using (Graphics grfx = Graphics.FromHd c(hdc))
{
Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height -
1);
Pen pen = new Pen(Color.Red, 1);
grfx.DrawRectan gle(pen, rect);
}
}
Aug 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9089
by: Mudcat | last post by:
Hello, I would like to change the color of the default border around frames or widgets. Note: not the highlight border, but the default border. There have been many previous requests about the highlight border, but I couldn't find anything on changing the default border color. I know how to change the border relief and width but have found no way to change the color of it. Is there a way to do that?
14
14490
by: Sheila King | last post by:
Hello, Am at my wits end, after spending quite a bit of time looking at online CSS docs, reading FAQs, searching this newsgroup and validating my CSS document. Was trying to copy, to some extent, the "stylish buttons" tutorial here: http://www.webreference.com/programming/css_stylish/ and have had mostly success with it, however one stupid thing isn't
8
128615
by: Chuck | last post by:
given that the img border >0, how is a border color specified? Chuck
2
19854
by: Ramesh | last post by:
Hi, Is there any solution available for changing the border color of the textbox control in VB.NET? Pls provide me with a sample code. Thanks in advance Ramesh
31
7201
by: jcrouse | last post by:
Is there a quick and easy way to change the color of a label controls border from the default black to white? Thank you, John
1
1631
by: Lee | last post by:
Hi, how can I change the border color (and status bar color) of a form at runtime? thanks in advance lee
8
3913
by: Bosconian | last post by:
I have two multiple select inputs. Initially the first contains a bunch of items and the second is empty. Using a common method, I move items back and forth by double clicking on them. This portion works perfectly, but I would also like to change the background color of the select element with the current focus. I have defined the following classes: ..selected {
1
2175
by: Arpan | last post by:
The output of a DataGrid is nothing but a HTML table. To color or change the width of the outer 4 borders of a DataGrid, one can use the properties "BorderColor" & "BorderWidth" respectively but how does one change the border color & width of the 4 borders surrounding each cell? There are 4 other properties apparently meant for changing the border color & width of the 4 borders surrounding each cell i.e. HeaderStyle-BorderColor &...
23
8734
by: cmcdermo | last post by:
I want to change the colour of a border around an image when a user clicks on the image. Not sure how to go about this, i have tried a few things but it doens't seem to work. here is my image. <img src="images/canvas_colours/images/canvas_taupe_small.gif" alt="Taupe" name="taupe_small" id="taupe_small" style="cursor:hand;border-Style:solid;border-Width:2px;border-Color:white"> This is what I tried.
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.