Rendering a Focus Rectangle

C#, Component Development, Graphics and Images No Comments »

Although you can use the DrawRectangle method to render a focus rectangle, you’ll have to set up the pen to render in the stadard dotted format. However, in the controlpaint static class there is already a DrawFocusRectangle method which does the job for you and makes the appropriate system call.

[csharp]

ControlPaint.DrawFocusRectangle(g, FocusRect);

[/csharp]

ControlPaint is found in System.Windows.Forms.

Writing and Reading AVI files in C#

C#, External C# Articles, Graphics and Images 1 Comment »

Here is a very extensive article, with demos and code written by Corinna John on the CodeProject.

http://www.codeproject.com/cs/media/avifilewrapper.asp

Creating a transparent UserControl

C#, Component Development, Graphics and Images 1 Comment »

If you want to create a user control that is not rectangular and shows the parent control surface then you have to make the control transparent.

This can be achieved by adding the following to the UserControl’s constructor.

[csharp]

SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);

[/csharp]

Rendering standard visual style control elements

C#, Component Development, Graphics and Images No Comments »

.Net provides a single class that can render most standard control elements such as Buttons, 3d rectangles checkboxes, etc using the current theme services.

This class is the static ControlPaint class. Some of the methods include:

DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style);
DrawBorder3D(Graphics graphics, Rectangle rectangle);
DrawButton(Graphics graphics, int x, int y, int width, int height, ButtonState state);
DrawCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state);
DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state);
DrawSizeGrip(Graphics graphics, Color backColor, Rectangle bounds);

    See the documentation for more methods.

    In addition there are specific static Renderer classes availablehat provide measuring routines and rendering routines as well, such as:

    ButtonRenderer
    CheckBoxRenderer
    TabRenderer
    TextBoxRender
    TextRenderer
    ScrollBarRenderer
    ToolStripRenderer

      For a full list type renderer into the object browser in Visual studio.

How to create a curved tab and fill with a gradiant

C#, Component Development, Graphics and Images 2 Comments »

ExampleIf you want to create a curved tab style control similar to the control on the right, then you can use the path functionallity of the graphics class to produce the rectangle with rounded corners at the top.

A linear gradiant brush finishes off the nice rendering effect.

[csharp]

Rectangle rc = new Rectangle(0, 0, this.Width, _CaptionHeight+1);
LinearGradientBrush b = new LinearGradientBrush(rc,
_CaptionLeftColor, _CaptionRightColor,
LinearGradientMode.Vertical);

// Now draw the caption areas with the rounded corners at the top
GraphicsPath path = new GraphicsPath();
path.AddLine(_CurveRadius, 0, this.Width -
(_CurveRadius * 2), 0);
path.AddArc(this.Width - (_CurveRadius * 2) - 1, 0,
(_CurveRadius * 2), (_CurveRadius * 2), 270, 90);
path.AddLine(this.Width-1, _CurveRadius,this.Width-1, _CaptionHeight);
path.AddLine(this.Width, _CaptionHeight+1, 0, _CaptionHeight+1);
path.AddLine(0, _CaptionHeight, 0, _CurveRadius);
path.AddArc(0, 0, (_CurveRadius * 2),
(_CurveRadius * 2), 180, 90);

// Remove jaggies
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

// Smooooth fill
e.Graphics.FillPath(b, path);
e.Graphics.DrawPath(new Pen(_PanelOutlineColor), path);

[/csharp]

Drawing trimmed text with ellipsis …

C#, Graphics and Images 3 Comments »

I have been writing a mulit-columnal control that rendered it’s text using the graphics.DrawString method and formatted to add the ellipsis characters if the text was too long for the output rect. However, as the column was resized, the text inter-character spacing changed, giving a very disconcerting wobble to the text.
To correctly output trimmed text I had to use the TextRenderer.DrawText method.

[csharp]

TextFormatFlags flags = new TextFormatFlags();
flags = TextFormatFlags.EndEllipsis | TextFormatFlags.Left | TextFormatFlags.VerticalCenter ;
TextRenderer.DrawText(g, text, font, rect, SystemColors.ControlText,flags);
[/csharp>

Please Note : microsoft recommend using TextRedner for drawing text…

System.Drawing.Graphics class presents some limitations—it is based on GDI+ and currently has limited support for complex scripts. For more information see MSDN article http://msdn.microsoft.com/msdnmag/issues/06/03/TextRendering/default.aspx

How to access resources, eg use a resource image

C#, Graphics and Images, Win Forms No Comments »

In Visual Studio 2005, (im not sure about other versions), simply choose properties on the Assembly and add the Resources you need under the resource tab.
In the class file you want to access the resources in, add a using statement
[csharp]using MyProject.Properties;[/csharp]
You can now access the resources in code by using the static Resource class.

[csharp]MyGraphics.DrawImage(Resources.MyImageResourceName, new Point(0,0));[/csharp]

WP Theme &Design by minus19.com & Icons
Entries RSS Comments RSS Log in