views:

30

answers:

2

I would like to draw an opaque line on a control in c#.

I tried this with visual basic powerpacks shapecontrol.. but I couldn't find any properties that set opacity.

How do I draw an opaque line ?

Thanks,

A: 

This code will draw a cross of two half-transparent white lines.

WPF

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Background="LightGray">
    <Grid>
        <Line X1="0" Y1="0" X2="100" Y2="100" Stroke="White" Opacity=".5" StrokeThickness="10" />
        <Line X1="0" Y1="100" X2="100" Y2="0" Stroke="White" Opacity=".5" StrokeThickness="10" />
    </Grid>
</Window>

Windows.Forms

var pen = new Pen(Color.FromArgb(128, 255, 255, 255), 10);
using (var g = CreateGraphics())
{
    g.DrawLine(pen, 0, 0, 100, 100);
    g.DrawLine(pen, 0, 100, 100, 0);
}
Athari
A: 

Thanks for your help, I found another way without using WPF.

C# wnidows form has Opacity property, so

  1. Create a screen layer that covers the windows form.
  2. make the screen layer to be transparent by setting transparencykey to be the same color as the layer back color.
  3. when the windows form is being loaded, change the size of layer to be the same as the windows form.
  4. make sure to move screen layer along with the windows form.
  5. draw any shapes on the layer by using ShapeControl (visual basic power pack) or Graphic like the answer above.
  6. set Opacity = 0.5
Jin