I am trying to use canvas inside windows form and zoom and pan that canvas for that first i put element host and then put canvas inside it and then put picture box in canvas and then trying to zoom canvas i have tried various ways but event of any control does not executed i have also written all mouse wheel events but no one is executed so please suggest me solution below is my code for adding controls and mouse wheel events
elementHost1.Height = picVarify.Height;
elementHost1.Width = picVarify.Width;
elementHost1.Location = picVarify.Location;
touchcanvas = new System.Windows.Controls.Canvas();
WindowsFormsHost hst = new WindowsFormsHost();
hst.Name = "Host";
hst.Child = picVarify;
hst.Height = picVarify.Height;
hst.Width = picVarify.Width;
touchcanvas.Height = picVarify.Height;
touchcanvas.Width = picVarify.Width;
touchcanvas.Children.Add(hst);
zm = new ZoomAndPan.ZoomAndPanControl();
zm.Name = "zm";
zm.Content = touchcanvas;
zm.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(zoomAndPanControl_MouseWheel);
elementHost1.Child = zm;
touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
hst.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(hst_MouseWheel);
picVarify.MouseWheel += new MouseEventHandler(picverify_MouseWheel);
here is the code that's working fine for me. I added canvas inside a canvas and set background property of both canvases.
XAML
<Window x:Class="WpfStack.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="rootGrid" Margin="10,0,0.4,3.8">
<Canvas Name="BaseCanvas" Background="AliceBlue" Margin="10,0,0,10">
</Canvas>
</Grid>
Code
public MainWindow()
{
InitializeComponent();
Canvas touchcanvas = new System.Windows.Controls.Canvas();
touchcanvas.Height =100;
touchcanvas.Width = 100;
touchcanvas.Background = Brushes.Transparent;
touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
BaseCanvas.Children.Add(touchcanvas);
BaseCanvas.Background = Brushes.Transparent;
}
public void touchcanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs args)
{
args.Handled = true;
}
Related
I have a basic wpf app that basically detects a touch input and moves a rectangle according to the input. My problem is that if that touch comes outside the screen(For example: I touch the bottom bezel of the screen and slide it in the touch area) it won't be detected. Is there a way to get over this problem? I want to detect edge swipes as well.
XAML:
<Window x:Class="touch_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:touch_test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Stylus.IsPressAndHoldEnabled="False"
>
<Canvas>
<Rectangle Width="50" Height="50" x:Name="rect" Fill="Red"/>
</Canvas>
</Window>
Cs:
protected TouchPoint TouchStart;
public MainWindow()
{
InitializeComponent();
this.PreviewTouchDown += new EventHandler<TouchEventArgs>(BasePage_TouchDown);
this.PreviewTouchMove += new EventHandler<TouchEventArgs>(BasePage_TouchMove);
WindowState = WindowState.Maximized;
WindowStyle = WindowStyle.None;
}
void BasePage_TouchDown(object sender, TouchEventArgs e)
{
TouchStart = e.GetTouchPoint(this);
}
void BasePage_TouchMove(object sender, TouchEventArgs e)
{
Rectangle rectToMove = rect;
rectToMove.RenderTransform = new MatrixTransform();
Matrix matr = ((MatrixTransform)rectToMove.RenderTransform).Matrix;
var currentTouchPoint = e.GetTouchPoint(this);
matr.Translate(currentTouchPoint.Position.X, currentTouchPoint.Position.Y );
rectToMove.RenderTransform = new MatrixTransform(matr);
}
I have a wpf application with two panes similar to powerpoint application:
left pane which shows list of all the panels in listbox
right pane which shows the selected panel
In the listbox I want to display panel as thumbnail and update the thumbnail as an when new controls are added to panel in right pane.
Just like powerpoint application thumbnail behaviour.
By using RenderTargetBitmap and PngBitmapEncoder we can capture a region of window.
and by using the PngBitmapEncoder frame Property assigned it to Image Source.
Lets start with Xaml
I divided the window by two half and left and right panel. Same in PowerPoint with less style. In order to demonstrate I have implemented to add TextBox on the right panel and the preview will be displayed on the left panel thumbnail.
<Grid Background="Aqua" x:Name="gridg">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox HorizontalAlignment="Left" Height="372" Margin="10,38,0,0" VerticalAlignment="Top" Width="306" Grid.Column="0" x:Name="Listtems" SelectionChanged="Listtems_SelectionChanged" />
<Button Content="+ TextBox" HorizontalAlignment="Left" Margin="142,10,0,0" VerticalAlignment="Top" Width="174" Click="Button_Click" Grid.Column="0"/>
<StackPanel x:Name="stackPanel" Background="Wheat" Grid.ColumnSpan="2" Margin="321,0,0,0" />
</Grid>
As soon as you click on the left panel item, the corresponding the control will be displayed on the right panel with the data.
In order to keep track of the items in the ListBox, I have used Dictionary with ItemIndex and to it's corresponding item's index used control.
Window's Code Behind
/// <summary>
/// Interaction logic for Window6.xaml
/// </summary>
public partial class Window6 : Window
{
Dictionary<int, Control> _dictionaryControls = new Dictionary<int, Control>();
DispatcherTimer dispatcherTimer = new DispatcherTimer();
public Window6()
{
InitializeComponent();
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Start();
}
private void BmpImage()
{
RenderTargetBitmap renderTargetBitmap =
new RenderTargetBitmap(800, 450, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(stackPanel);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
Image img = new Image();
img.Source = pngImage.Frames[0];
img.Height = 148;
img.Width = 222;
Listtems.Items.Add(img);
Listtems.SelectedIndex = Listtems.Items.Count - 1;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
stackPanel.Children.Clear();
int item = Listtems.Items.Count;
TextBox txtControl = new TextBox();
txtControl.FontSize = 100;
txtControl.Height = 122;
txtControl.TextWrapping = TextWrapping.Wrap;
_dictionaryControls.Add(item, txtControl);
stackPanel.Children.Add(txtControl);
stackPanel.UpdateLayout();
BmpImage();
}
private void Listtems_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateThumbNail();
}
private void UpdateThumbNail()
{
int indexbackup = -1;
Listtems.SelectionChanged -= Listtems_SelectionChanged;
Control control;
_dictionaryControls.TryGetValue(Listtems.SelectedIndex, out control);
if (control == null)
{
Listtems.SelectionChanged += Listtems_SelectionChanged;
return;
}
indexbackup = Listtems.SelectedIndex;
stackPanel.Children.Clear();
stackPanel.Children.Add(control);
stackPanel.UpdateLayout();
RenderTargetBitmap renderTargetBitmap =
new RenderTargetBitmap(800, 450, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(stackPanel);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
Image img = new Image();
img.Source = pngImage.Frames[0];
img.Height = 148;
img.Width = 222;
Listtems.Items.Insert(Listtems.SelectedIndex, img);
Listtems.Items.RemoveAt(Listtems.SelectedIndex);
Listtems.SelectedIndex = indexbackup;
Listtems.SelectionChanged += Listtems_SelectionChanged;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
UpdateThumbNail();
}
}
BmpImage() : - I used to capture or in other words the print screen of the StackPanel control.
Button_Click Event :- Is used to create a new Item in ListBox adding Image with the current print screen of the TextBox Control in StackPanel. It also adds control in _dictionaryControls variable.
Listtems_SelectionChanged Event:- Clears the StackPanel and then take the TextBox Control from _dictionaryControls based on the SelectedIndex of ListBox and place it in the StackPanel by taking current snapshot of the StackPanel.
For Demo Purpose, I have done it only for TextBox Control, but you can do it for any other control with a little tweaking.
UpdateThumbNail created a method responsible to update the image in Listbox based on the ListBoxItem.
dispatcherTimer_Tick : - Event is responsible to call the UpdateThumbNail() Method for every second.
I am trying to use VLC in WPF via Vlc.DotNet. I have been successful in getting Vlc.DotNet to work in Winforms, but thus far unsuccessful with WPF.
I get no errors, but I also get no video... just a blank white pane.
Here is my very simple XAML:
<Window x:Class="VLC.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closing="Window_Closing">
<Grid x:Name="Grid1">
</Grid>
</Window>
And here is the codebehind I use to insert and start the Vlc Wpf control.
public MainWindow()
{
VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_AMD64;
VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_AMD64;
VlcContext.StartupOptions.IgnoreConfig = true;
VlcContext.Initialize();
InitializeComponent();
var vlcPlayer = new VlcControl();
var media = new LocationMedia("rtsp://admin:12345#192.168.42.200:554/MediaInput/h264");
Grid1.Children.Add(vlcPlayer);
var vlcBinding = new Binding("VideoSource");
vlcBinding.Source = vlcPlayer;
var vImage = new Image();
vImage.SetBinding(Image.SourceProperty, vlcBinding);
var vBrush = new VisualBrush();
vBrush.TileMode = TileMode.None;
vBrush.Stretch = Stretch.Uniform;
vBrush.Visual = vImage;
Grid1.Background = vBrush;
vlcPlayer.Play();
}
Does anyone see anything wrong with this?
Using Vlc 2.1.5 win32
You haven't set vlcPlayer's Media property.
var vlcPlayer = new VlcControl();
var media = new LocationMedia("rtsp://admin:12345#192.168.42.200:554/MediaInput/h264");
vlcPlayer.Media = media; //add this
Btw, you don't need to add vlcPlayer to Grid1.
I want to set an image in a canvas control and have 20 canvases that I am creating by using a loop. The problem is that when I want to add all those canvas items in to a scroll viewer it does not work. Here is my code:
private void CreateAndShowCanvas()
{
List<Canvas> list = new List<Canvas>();
for (int i = 0; i < 20; i++)
{
Canvas myCanvas1 = new Canvas();
myCanvas1.Background = new SolidColorBrush(Colors.Transparent);
myCanvas1.Height = 235;
myCanvas1.Width = 626;
//Canvas.SetZIndex(myCanvas1, 4);
Image MainImage = new Image();
MainImage.Width = 275;
MainImage.Height = 235;
BitmapImage mi = new BitmapImage(new Uri("select_1.png", UriKind.Relative));
MainImage.Source = mi;
Canvas.SetTop(MainImage, 0);
Canvas.SetLeft(MainImage, 0);
myCanvas1.Children.Add(MainImage);
Image SeparatorImage = new Image();
BitmapImage si = new BitmapImage(new Uri("Sentre Seprator.png", UriKind.Relative));
SeparatorImage.Height = 270;
SeparatorImage.Source = si;
Canvas.SetTop(SeparatorImage, -5);
Canvas.SetLeft(SeparatorImage, 310);
myCanvas1.Children.Add(SeparatorImage);
Image SecondImage = new Image();
SecondImage.Width = 275;
SecondImage.Height = 235;
BitmapImage sci = new BitmapImage(new Uri("select_2.png", UriKind.Relative));
SecondImage.Source = sci;
Canvas.SetTop(SecondImage, 0);
Canvas.SetLeft(SecondImage, 350);
myCanvas1.Children.Add(SecondImage);
list.Add(myCanvas1);
}
scv.Content = list;
}
and in XML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ScrollViewer Name="scv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Margin="60,40,59,46" Opacity="99" Background="Transparent" />
</Grid>
</Window>
and when I run it, it only shows ("Collection"). Please help me out, thanks in advance...
Should you not just be using a ListBox with an ItemTemplate that has Canvases inside it instead? There must be an easier way to do whatever you are trying to achieve than creating 20 canvases manually.
Here's some reading to do with ListBoxes.
A ScrollViewer can hold just a single child, so you need another panel type like a Grid or StackPanel which holds your canvases.
<Grid>
<ScrollViewer Name="scv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Margin="60,40,59,46" Opacity="99" Background="Transparent">
<StackPanel Name="stp" />
</ScrollViewer>
</Grid>
Add canvases to stp instead.
I have some itemsControl that i dynamicly adding to it StackPanel as an item.
Each of the StackPanel contain 2 button - btn1, btn2.
I dynamicly connect each button to some button event ( all the button event are same ).
When i test it - i see that the event is not call on the button click - and i dont see any reason to this.
the Code:
private StackPanel CreatePanel()
{
StackPanel stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Horizontal;
for (int i = 0; i < 2; i++)
{
Button btn = new Button();
btn.Height = btn.Width = 100;
btn.Content = i % 2 == 0 ? "Btn1" : "Btn2";
// Event connection
btn.Click += new RoutedEventHandler( button1_Click );
stackPanel.Children.Add(btn);
}
return stackPanel;
}
From MSDN: For certain combinations of input events and WPF control classes, the element that raises the event is not the first element that has the opportunity to handle it.
This is the nature of the RoutedEventHandler:)
try below code , works for me Please note grd is a Grid on my XAML
Edited added code for ItemsControl
XAML
<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">
<ScrollViewer >
<Grid>
<ItemsControl Name="grd">
</ItemsControl>
</Grid>
</ScrollViewer>
Code
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
grd.Items.Add(CreatePanel()); // Now grd is a itemsControl
}
private StackPanel CreatePanel()
{
StackPanel stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Horizontal;
for (int i = 0; i < 2; i++)
{
Button btn = new Button();
btn.Height = btn.Width = 100;
btn.Content = i % 2 == 0 ? "Btn1" : "Btn2";
// Event connection
btn.Click += new RoutedEventHandler(doCall);
stackPanel.Children.Add(btn);
}
return stackPanel;
}
private void doCall(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hi");
}
}