Pieter Vanhees

Hi,

I'm creating an application that will be used for brainstorming.
The users can add words or sentences, these words will be showed in usercontrols that are randomly placed on a grid.
I'd like to give the illusion that the grid they're typing on has an unlimmited size. Therefore I want to create an enviroment where the user can pan around the surface. I tried with the following code but the problem is, although the for each is properly done, the usercontrols don't move. I can't see what I'm doing wrong.

void Window_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (mbMouseDown)
{
Point bNew = e.GetPosition(Window);
foreach (UIElement oObject in LayoutRoot.Children)
{
Canvas.SetTop(oObject, bNew.X - mpBegin.X);
Canvas.SetLeft(oObject, bNew.Y - mpBegin.Y);
}
}
mbMouseDown = false;
}

void LayoutRoot_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (mbMouseDown)
{
Point bNew = e.GetPosition(Window);
foreach (UIElement oObject in LayoutRoot.Children)
{
Canvas.SetTop(oObject, bNew.X - mpBegin.X);
Canvas.SetLeft(oObject, bNew.Y - mpBegin.Y);
}
}
}

void LayoutRoot_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
mbMouseDown = true;
mpBegin = e.GetPosition(Window);
}


I was wondering if, besides the problem I describe here, this is a proper way to do it.
Won't this operation stress the CPU too much and will it work smoothly

Anyone with experience in this area

Thanks in advance!
Pieter






Re: Windows Presentation Foundation (WPF) Drag every control on mousemove event

Tomas

Hello,

I guess the problem is that Canvas attached properties are simply ignored when controls are contained within a grid.

After the mouse is pressed you would have to move these controls into overlaying Canvas instead of Grid and move them along.

After the mouse is released, a new Grid layout has to be created (refering to recent situation) and controls put back inside.

This comment is rather to the code you're presenting. I have to admin that I really haven't caught the idea about the application.

As far as I have experienced, this code should be fast (however, I'm using rather fast machines).

Best, Tomas





Re: Windows Presentation Foundation (WPF) Drag every control on mousemove event

Tovdb

Hi,

I'm also working on the application, I'll try to explain what we're trying to do here.

You have to imagine a white empty 2D paper which is virtually unlimited in size. An admin can place a question on this paper, users can then reply on the active question. The admin can also zoom in and zoom out and drag the paper around and post new questions. So we have to work with an environment that contains a fair amount of questions linked to answers. Every question can be differently scaled, so this means that you have to zoom in more on some questions than others.

Basic functionality would have to be panning the paper, by that I mean hold mouse down and dragging it to another position, thus moving all objects on the paper in that direction.

Which we have succeeded in doing, however that the performance(with only 2 questions posted) is not really optimal.

Also we need to be able to zoom, I¡¯ve been working with the scaling function of the grid, like this.

Code Snippet

private void Window_MouseWheel(object sender, MouseWheelEventArgs e)

{

if (e.Delta > 0)

{

mpScaleSize = new Point((mpScaleSize.X - 0.05), (mpScaleSize.Y - 0.05));

grdCanvas.LayoutTransform = new ScaleTransform(mpScaleSize.X, mpScaleSize.Y);

zoomX.Content = mpScaleSize.X.ToString();

zoomY.Content = mpScaleSize.Y.ToString();

}

else

{

mpScaleSize = new Point((mpScaleSize.X + 0.05), (mpScaleSize.Y + 0.05));

grdCanvas.LayoutTransform = new ScaleTransform(mpScaleSize.X, mpScaleSize.Y);

zoomX.Content = mpScaleSize.X.ToString();

zoomY.Content = mpScaleSize.Y.ToString();

}

}

This also isn¡¯t very fast, performance wise.

Any help you can give us would be very much appreciated, we¡¯re just a couple of students, trying to make our internship work. J





Re: Windows Presentation Foundation (WPF) Drag every control on mousemove event

Tomas

I am not very experienced in this area. But as far as I've read WPF docs, most of controls is pretty optimized itself for performance.

By that I mean, why not to use an "unlimited" Canvas containing your Q/A in combination with VisualBrushes and let WPF to do all the stuff for you

You would need to give it a try, but if you use VisualBrush as some kind of view, you can move the brush along the Canvas instead of moving a couple of Controls.

The same way zooming can be provided.

This is just a quick idea,

Tomas





Re: Windows Presentation Foundation (WPF) Drag every control on mousemove event

Tovdb

We've been trying to do it with the VisualBrush like you said, but can't really understand how the VisualBrush model works.

I've tried the following, but I'm unable to show anything on the Canvas within the VisualBrush.Visual.

<Window

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="Working_with_visuals.frmTestingVisuals"

x:Name="Window"

Title="Discussion"

Width="799" Height="635" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" Opacity="1">

<Window.Background>

<LinearGradientBrush EndPoint="0.501,0.57" StartPoint="0.496,1.102">

<GradientStop Color="#FFE0E0E0" Offset="0"/>

<GradientStop Color="#FFFFFFFF" Offset="1"/>

</LinearGradientBrush>

</Window.Background>

<VisualBrush ViewportUnits="Absolute" Stretch="Fill" >

<VisualBrush.Visual>

<Canvas x:Name="cnsBlank">

</Canvas>

</VisualBrush.Visual>

</VisualBrush>

</Window>

Even in the Design window, dropping items into the canvas doesn¡¯t seem to be working.

If I add anything in code, I do it like this:

private void placeUC(string sTest, Point pnt)

{

try

{

//placing UC

ucMessage ucMsg = new ucMessage();

ucMsg.Width = 200;

ucMsg.Height = 70;

//ucMsg.clsMessage = oMessage;

ucMsg.txtMessage.Text = sTest;

Canvas.SetTop(ucMsg, pnt.Y);

Canvas.SetLeft(ucMsg, pnt.X);

cnsBlank.Children.Add(ucMsg);

}

catch (StackOverflowException ex)

{

//placeUC(oMessage);

}

}

Is it smart, performance wise, to each time add a usercontrol as a child, or would it be better to add them all to one Child

And thinking ahead to what you were saying about Panning and zooming, do you mean working with the ViewBox attributes of the VisualBrush





Re: Windows Presentation Foundation (WPF) Drag every control on mousemove event

Tomas

For understanding of VisualBrush, try to find some examples. There are tones on the web (e.g. http://msdn2.microsoft.com/en-us/library/ms771563.aspx ). Microsoft SDK Samples contain rich amount as well.

> Is it smart, performance wise, to each time add a usercontrol as a child, or would it be better to add them all to one Child

Well, I'd like to know, too. Personally, I think there should not be the any difference.

> do you mean working with the ViewBox attributes of the VisualBrush

I meant just to use VisualBrush as a simple view which is put to the output window. Actually, this is just an idea. But it is based on the fact, that WPF original controls are optimized for a performance - I hope that WPF on its own will be able to decide what part of Canvas and Brush has to be rendered and what part can be omitted. (Very same case as in TreeView containing 1M items while only 20 items on the screen get really rendered!)

My experience shows that WPF gets back to the good old habit - less user code, better performance.





Re: Windows Presentation Foundation (WPF) Drag every control on mousemove event

Tovdb

I've been playing around with the VisualBrushes for a while now.

I've managed to get controls from code onto the VisualBrush(which is on a rectangle in the center of my app), however working with VisualBrush as some kind of view, like you said, isn't working for me.

Maybe you could elaborate on what you mean by using it as a view Hate being new at this...

What I "discovered"is that working with the viewbox property of the visualbrush might be the solution, but I can't seem to get it to work.

To my understanding the viewbox is a rectangle with it's x & y as panning values, and it's with & height as crop/stretch values (zoom).

I understand that you haven't got any practical experience, but I was hoping that someone else does

All help is appreciated,

Tobias