 |
|
|
 |
|
 |
Thank you very much! Worked awesome in VB.NET in VS2010!
|
|
|
|
 |
|
 |
Hi, How i can set transparent BackColor for Labal as "Color.FromArgb(40, 0, 0, 255)" ?
Thanks
|
|
|
|
 |
|
 |
Theres a small bug in the DrawLine method. Theres no checking for the window state ( it should not attempt a redraw when the application is minimised ).
The problem being the LinearGradientBrush constructor will throw an exception if the given rect has a width or height of zero. In this instance on the Resize event..
I just did a simple fix in DrawLine() that returned if the height or width of the parent was zero.
Heres a stack trace of the app i was using it in:
at System.Drawing.Drawing2D.LinearGradientBrush..ctor(Rectangle rect, Color color1, Color color2, Single angle, Boolean isAngleScaleable)
at simpleline.assemblies.simpleLine.DrawLine()
at simpleline.assemblies.simpleLine.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.UserControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, Int32 x, Int32 y, Int32 cx, Int32 cy, Int32 flags)
at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)
at System.Windows.Forms.Layout.DefaultLayout.ApplyCachedBounds(IArrangedElement container)
at System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)
at System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)
at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Panel.OnResize(EventArgs eventargs)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, Int32 x, Int32 y, Int32 cx, Int32 cy, Int32 flags)
at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)
at System.Windows.Forms.Layout.DefaultLayout.xLayoutDockedControl(IArrangedElement element, Rectangle newElementBounds, Boolean measureOnly, Size& preferredSize, Rectangle& remainingBounds)
at System.Windows.Forms.Layout.DefaultLayout.LayoutDockedControls(IArrangedElement container, Boolean measureOnly)
at System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)
at System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)
at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.Form.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Form.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmSysCommand(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmNcButtonDown(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at AEC.AecEcfModifier.Forms.MainForm.Main() in U:\projects\ECU5\PCTools\AecEcfModifier\AecEcfModifier\Forms\MainForm.cs:line 330
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
|
|
|
|
 |
|
 |
Thanks for pointing that out.
|
|
|
|
 |
|
 |
When I change the option "Use Gradient" for simple line within the Designer to True, an exception occurs. I am using Microsoft Dev Environment 2003. Any idea's?
|
|
|
|
 |
|
 |
Sorry, I don't know what could be causing that. Are you running Windows XP?
|
|
|
|
 |
|
 |
Yes I am using XP. I have just created a new project and tried again. It works. Must be a problem with my Original project. Sorry to bother you. Great control thow.
|
|
|
|
 |
|
 |
Thanks for assembling a simple, straightforward control to provide this functionality... saved me tons of time.
Here's one enhancement. I must change the border color on the fly, but control was not repainting. Add the following method to cause all run-time changes to re-paint:
protected override void OnInvalidated(InvalidateEventArgs e)
{
if(_backBuffer!=null)
{
_backBuffer.Dispose();
_backBuffer=null;
}
base.OnInvalidated (e);
}
Finally, transparent background doesn't seem to work for me... I see a copy of whatever's behind my window! Anyone have this working?
Mark Arend
Microsoft Consulting Services
|
|
|
|
 |
|
 |
Paul,
Thanks for posting these - quite useful. I'm using the simpleLine and TransparentLabel.
I have added the ability to control the alpha-channel of the text in TransparentLabel, so the text can be see-through, too. I implemented it as "TextTransparency" which you may set from 0 to 100 (100 being fully opaque). Technically, it should be called "TextOpacity", but I thought that was a little obscure.
I don't see an easy way to upload my code here, so I'll upload it to my ftp server when I'm at work tomorrow and send the URL.
Donald
|
|
|
|
 |
|
 |
Great! I got the code. I'll definately integrate it into this project as soon as I have some time.
For anyone that wants to do it themselves, here it is:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
namespace simpleline.assemblies
{
/*
* Thanks due to Kenneth Hadden for creating this class, and to
* Paul Brower for posting it at codeproject.com. See
* http://www.codeproject.com/cs/miscctrl/SimpleLine.asp for
* Paul's original article.
*
* Added: TextTransparency property, Donald Kane June 2005.
*/
///
/// Summary description for TransparentLabel.
///
public class TransparentLabel : System.Windows.Forms.Control
{
System.Drawing.ContentAlignment _textAlign = ContentAlignment.TopLeft ;
StringFormat _drawFormat = new StringFormat( ) ;
///
/// From 0 to 255, specifies transparency of the text itself.
///
int _alphaChannel = 255 ;
Rectangle _textRect = new Rectangle( 0, 0, 0, 0 ) ;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public TransparentLabel()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
SetStyle( ControlStyles.Opaque, false ) ;
SetStyle( ControlStyles.ResizeRedraw, true ) ;
}
protected virtual void InvalidateEx( )
{
if ( null == this.Parent ) return ;
System.Drawing.Rectangle dirtyRect = new Rectangle( this.Location, this.Size ) ;
this.Parent.Invalidate( dirtyRect, true ) ;
}
private void ResetAlign( )
{
switch ( _textAlign )
{
case ContentAlignment.BottomLeft:
case ContentAlignment.MiddleLeft:
case ContentAlignment.TopLeft:
_drawFormat.Alignment = StringAlignment.Near ;
break ;
case ContentAlignment.BottomCenter:
case ContentAlignment.MiddleCenter:
case ContentAlignment.TopCenter:
_drawFormat.Alignment = StringAlignment.Center ;
break ;
case ContentAlignment.BottomRight:
case ContentAlignment.MiddleRight:
case ContentAlignment.TopRight:
_drawFormat.Alignment = StringAlignment.Far ;
break ;
}
}
private void ResetRect( )
{
Graphics g = this.CreateGraphics( ) ;
SizeF textSize = g.MeasureString( base.Text, this.Font ) ;
switch ( _textAlign )
{
case ContentAlignment.BottomLeft:
case ContentAlignment.BottomCenter:
case ContentAlignment.BottomRight:
_textRect = new Rectangle( 0, this.ClientRectangle.Height - (int)textSize.Height, this.ClientRectangle.Width, (int)textSize.Height ) ;
break ;
case ContentAlignment.MiddleLeft:
case ContentAlignment.MiddleCenter:
case ContentAlignment.MiddleRight:
_textRect = new Rectangle( 0, ((this.ClientRectangle.Height - (int)textSize.Height) / 2), this.ClientRectangle.Width, (int)textSize.Height ) ;
break ;
case ContentAlignment.TopLeft:
case ContentAlignment.TopCenter:
case ContentAlignment.TopRight:
_textRect = new Rectangle( 0, 0, this.ClientRectangle.Width, (int)textSize.Height ) ;
break ;
}
g.Dispose( ) ;
}
#region O V E R R I D E S
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams ;
cp.ExStyle |= 0x00000020 ; //WS_EX_TRANSPARENT
return( cp ) ;
}
}
protected override void InitLayout()
{
base.InitLayout( ) ;
ResetAlign( ) ;
ResetRect( ) ;
// This get's removed immediatly upon the first invocation.
this.Parent.Paint += new PaintEventHandler(Parent_Paint);
}
//Must be stubbed out. If you implement this you will loose your transparent background.
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//do not allow the background to be painted
//Debug.WriteLine( "TransparentLabel(" + this.Name + ")::OnPaintBackground" ) ;
}
protected override void OnResize(EventArgs e)
{
base.OnResize (e);
ResetRect( ) ;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics ;
Color fg = Color.FromArgb( _alphaChannel, ForeColor ) ;
if ( ! Enabled ) fg = System.Drawing.SystemColors.GrayText ;
SolidBrush drawBrush = new SolidBrush( fg ) ;
g.DrawString( base.Text, this.Font, drawBrush, _textRect, _drawFormat ) ;
drawBrush.Dispose( ) ;
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged (e);
this.InvalidateEx( ) ;
this.Invalidate( ) ;
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged (e);
ResetRect( ) ;
this.InvalidateEx( ) ;
this.Invalidate( ) ;
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged (e);
this.InvalidateEx( ) ;
this.Invalidate( ) ;
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged (e);
this.InvalidateEx( ) ;
this.Invalidate( ) ;
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);
ResetRect( ) ;
this.InvalidateEx( ) ;
this.Invalidate( ) ;
}
protected override void OnLocationChanged(EventArgs e)
{
base.OnLocationChanged (e);
this.InvalidateEx( ) ;
this.Invalidate( ) ;
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
_drawFormat.Dispose( ) ;
}
base.Dispose( disposing );
}
#endregion
#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
//
// TransparentLabel
//
this.BackColor = System.Drawing.SystemColors.Control;
this.Name = "TransparentLabel";
this.Size = new System.Drawing.Size(192, 16);
}
#endregion
#region P R O P E R T I E S
[Browsable( true ),
DesignerSerializationVisibility( DesignerSerializationVisibility.Visible )]
public override string Text
{
get { return base.Text ; }
set { base.Text = value; this.InvalidateEx( ) ; }
}
[Browsable( true ),
DesignerSerializationVisibility( DesignerSerializationVisibility.Visible ),
DefaultValue(ContentAlignment.TopLeft)]
public System.Drawing.ContentAlignment TextAlign
{
get { return( _textAlign ) ; }
set
{
_textAlign = value ;
this.InvalidateEx( ) ;
ResetAlign( ) ;
ResetRect( ) ;
}
}
[Browsable( true ),
DesignerSerializationVisibility( DesignerSerializationVisibility.Visible ),
DefaultValue( 100 ),
Description( "The percent opacity of the text (0=invisible, 100=fully opaque). " ),
Category( "Appearance" )]
public int TextTransparency
{
get
{
return Convert.ToInt32( _alphaChannel * 100D / 255 );
}
set
{
int newAlpha = value > 100 ? 100 : value < 0 ? 0 : value;
newAlpha = Convert.ToInt32( newAlpha * 255D / 100 );
if( newAlpha != _alphaChannel )
{
_alphaChannel = newAlpha;
InvalidateEx();
}
}
}
#endregion
#region E V E N T H A N D L E R S
private void Parent_Paint(object sender, PaintEventArgs e)
{
this.Invalidate( ) ;
// This get's removed immediatly upon the first invocation,
// becuase it is just a cludge to stop the text from disapearing when
// you drop a TranparentLable onto a container.
// If you don't remove it you get way-to-many paints.
this.Parent.Paint -= new PaintEventHandler(Parent_Paint);
}
#endregion
}
}
|
|
|
|
 |
|
 |
To get the control to show changes to the gradient in the designer, change the first few lines of OnPaint() to:
if ( _backBuffer == null || this.DesignMode)
{
if(_backBuffer == null)
_backBuffer = new Bitmap(
this.ClientSize.Width,
this.ClientSize.Height ) ;
This gets it to always draw the control in design mode but only allocates the buffer if not already done.
Eric
|
|
|
|
 |
|
 |
Thanks Eric. I'll update that as soon as I can.
Off topic question: Did you ever live in Sequim Washington?
|
|
|
|
 |
|
 |
No, I've never lived in Sequim.
|
|
|
|
 |
|
 |
Hi Paul!
"This is the first article I have submitted, so please bear with me on the format."
Well, first of all, congratulations on your first article. Second, the following lines are just my opinion. I enjoy seeing new people contributing so I'm really trying to keep my feedback objective and constructive.
Having said that, I think the biggest problem with your article is that it contains zero code snippets (and of course explanations what the code does). Personally, I think that this is one of the most interesting aspects of many CP articles. Especially since you marked yours as Beginner. The comment on the submission wizard also states that the "level should reflect the level of competency needed to understand and use this article. 'Beginner' should only be chosen for tutorials or step-by-step walk-throughs." I also can recommend A Guide To Writing Articles For Code Project[^] by Marc Clifton for some ideas on how to improve articles.
Anyway, thanks for sharing and keep it up!
Best regards
Dennis
|
|
|
|
 |
|
 |
Thanks for the feedback. I'll try and take some time in the next day or two and "standardize" the article a bit.
Please keep in mind, however, that this is just a shape control, and all of its properties can be set at design time with the properties box.
At the very least, I will provide some instructions on how to set those properties.
Paul
|
|
|
|
 |
|
 |
paulbrower wrote:
Please keep in mind, however, that this is just a shape control, and all of its properties can be set at design time with the properties box.
At the very least, I will provide some instructions on how to set those properties.
That's not what I meant. Of course readers want to know how to use your controls and as you said yourself this might be kind of self-explanatory in this case. But even more (especially beginners) most likely want to read about how it works and why you implemented it the way you did. So, for example, you could write a little bit about how to implement user-drawn controls in general and use your own control source code as an example. Again, just a suggestion. I'm looking forward to your article update.
Best regards
Dennis
|
|
|
|
 |
|
 |
Thanks for the clarification. I guess I just needed a little knock on the head!
I'll definately update the article to cover some of the how. I suppose I'll change this to "intermediate" as well, as it does require more than a beginner understanding of .Net.
Paul
|
|
|
|
 |
|