Skip to main content

Microsoft Silverlight

Answered Question Bug in Rectangle Drawing (SL2 B1)RSS Feed

(0)

NilK29
NilK29

Member

Member

22 points

8 Posts

Bug in Rectangle Drawing (SL2 B1)

All,

Please check out the following simple code. If you hold down mouse button and drag mouse thru canvas, the rectangle is expected to draw/refresh itself. It does draw rectangle "except the Stroke" used for border. If you notice there is a small "Aaua" dot at left/top corner when you start dragging indicating the presence of stroke. Can somebody provide a solution to this issue?

Thanks

Nilk 

<UserControl x:Class="Tryouts.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Canvas Width="400" Height="300"
                Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="1"
                Background="DarkBlue"
                MouseLeftButtonDown="MyMouseLeftButtonDown"
                MouseLeftButtonUp="MyMouseLeftButtonUp"
                MouseMove="MyMouseMove">

        <Button x:Name="BtnTest"  Click="BtnTest_Click" Width="200" Height="30" Canvas.Left="150" Canvas.Top="5" Content="Move Mouse in Blue Area"></Button>
        <Rectangle  x:Name="RecSelection" 
                    Height="100" Width="100"
                    Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="10"
                    Fill="#7F92B5F4" Stroke="Aqua"
                    StrokeThickness="1"
                    Visibility="Collapsed"/>
    </Canvas>
</UserControl>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Tryouts
{
    public partial class Page : UserControl
    {
        Point MouseBeginPosition;

        public Page()
        {
            InitializeComponent();
        }

        private void BtnTest_Click(object sender, RoutedEventArgs e)
        {
            this.RecSelection.Width = 30;
            this.RecSelection.Height = 200;
            this.RecSelection.Visibility = Visibility.Visible;
        }

        private void MyMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MouseBeginPosition = e.GetPosition(this);
            if (RecSelection.Visibility == Visibility.Collapsed)
            {
                ResetRectangle(MouseBeginPosition.Y, MouseBeginPosition.X);
                RecSelection.Visibility = Visibility.Visible;
            }
        }

        private void MyMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (RecSelection.Visibility == Visibility.Visible)
            {
                RecSelection.Visibility = Visibility.Collapsed;
                ResetRectangle(-1, -1);
            }

            MouseBeginPosition = e.GetPosition(this);

        }

        private void MyMouseMove(object sender, MouseEventArgs e)
        {
            Point CurrentLocation = e.GetPosition(this);

            if (RecSelection.Visibility == Visibility.Visible)
                DrawSelectionRectangle(MouseBeginPosition, CurrentLocation);

        }

        private void DrawSelectionRectangle(Point From, Point To)
        {
            RecSelection.Visibility = Visibility.Visible;
           
            RecSelection.Width = Math.Abs(From.X - To.X);
            RecSelection.Height = Math.Abs(From.Y - To.Y);
            if (From.X > To.X)
                RecSelection.SetValue(Canvas.LeftProperty, To.X);
            else
                RecSelection.SetValue(Canvas.LeftProperty, From.X);

            if (From.Y > To.Y)
                RecSelection.SetValue(Canvas.TopProperty, To.Y);
            else
                RecSelection.SetValue(Canvas.TopProperty, From.Y);

            RecSelection.Visibility = Visibility.Visible;
        }

        private void ResetRectangle(double Top, double Left)
        {
            if (Top > -1)
            {
                RecSelection.SetValue(Canvas.TopProperty, Top);
            }
            if (Left > -1)
            {
                RecSelection.SetValue(Canvas.LeftProperty, Left);
            }
            RecSelection.Width = 0;
            RecSelection.Height = 0;
            RecSelection.Visibility = Visibility.Visible;
        }
    }
}

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Bug in Rectangle Drawing (SL2 B1)

Hello, thanks for reporting this issue. It's a known problem which our developers are investigating. This only happens when the Rectangle's Width and Height are both 0. A simple workaround is to set either Width or Height to a little larger than 0, such as 0.01, in the ResetRectangle method.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

NilK29
NilK29

Member

Member

22 points

8 Posts

Re: Bug in Rectangle Drawing (SL2 B1)

Yi-Lun Luo,

Thanks for the update. The workaround is prefect. I worked like champ.

Regards

Nilk

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities