I am working on a Kinect application for WPF (Kinect 2). I am using the KinectRegion to trigger some buttons, but i want to trigger the buttons on hover and not on click. what is the best way to achieve this? I have tried with MouseEnter and MouseLeave with no luck. My goal is that when the user hovers on a button an animation is played and then after 2 seconds the buttons is clicked. I would appreciate any help!
<k:KinectRegion x:Name="kinectRegion" >
<Grid Background="White" Name="gridTest">
<k:KinectUserViewer Height="400" Width="300" HorizontalAlignment="Center" VerticalAlignment="Top" DefaultUserColor="#FFF93636" EngagedUserColor="#FF395913" />
<Button Name="buttonStartSpel" Width="400" Height="200" Margin="0,800,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" Content="Start het spel" Style="{StaticResource KinectCustomButton}" MouseEnter="buttonStartSpel_MouseEnter" MouseLeave="buttonStartSpel_MouseLeave"></Button>
</Grid>
</k:KinectRegion>
UPDATE -
Looking for multiple ways to solve this i came up with this solution:
We executed the code on window load.
void KinectPointerPointSample_Loaded(object sender, RoutedEventArgs e)
{
// Listen to Kinect pointer events
KinectCoreWindow kinectCoreWindow = KinectCoreWindow.GetForCurrentThread();
kinectCoreWindow.PointerMoved += kinectCoreWindow_PointerMoved;
}
private void kinectCoreWindow_PointerMoved(object sender, KinectPointerEventArgs args)
{
KinectPointerPoint kinectPointerPoint = args.CurrentPoint;
bool isEngaged = kinectPointerPoint.Properties.IsEngaged;
if (isEngaged)
{
System.Drawing.Point mousePt = new System.Drawing.Point((int)(kinectPointerPoint.Position.X * kinectRegion.ActualWidth - 80), (int)(kinectPointerPoint.Position.Y * kinectRegion.ActualHeight));
System.Windows.Forms.Cursor.Position = mousePt;
}
}
Noticed that the mouse pointer does not have the exact same position of the handpointer, i have tested that with negative results. Thats why i placed the mouse pointer slightly left to the handpointer (80 pixs to be exact). You can play around depending on your project. Finally we hide the mouse pointer with the following code:
this.Cursor = Cursors.None;
Now we can use the OnMouseEnter and OnMouseLeave events...I hope this helps anyone having this problem.
Related
I am creating an application which currently lets users scroll to zoom into an image. Currently, if I try to use a scrollview to allow users to scroll down the page, it breaks this feature. Is there any way for me to implement a scrollbar that does not access the scroll wheel? What I am looking for is a way to only allow users to access the scrollbar by clicking it, not by using the scrollwheel.
Set the MouseWheel event on your image and set e.Handled = true at the end.
Contrived example -
XAML:
<Grid>
<ScrollViewer>
<Canvas Width="1000" Height="1000" Background="PeachPuff" MouseWheel="Canvas_MouseWheel" />
</ScrollViewer>
</Grid>
Code:
private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
(sender as Canvas).Background = Brushes.Blue;
e.Handled = true;
}
I am developing a UWP application in which I have several XAML controls(buttons, textblocks, checkboxes, etc). Ideally, The final user of this app should be able to navigate between these controls ONLY USING MOUSE WHEEL(this is a medical device UI in which only a mouse wheel will be available on top of the monitor). Now my question is that how to force this application use mouse wheel as the primary source of navigation between controls?
Some more feedbacks:
1.Right now, when I run my application in visual studio, I just see mouse pointer and of course buttons are sensitive to mouse clicks but in order to initiate an event, I have to hover to that element and click. MOUSE WHEEL IS NOT WORKING by default to navigate and select controls.
2.when I sideload this UWP application on a raspberry pi device and run the application there, the only way to navigate between controls is using an attached keyboard(possible to navigate and select controls using it). AGAIN ATTACHED MOUSE WHEEL IS NOT WORKING HERE.
an example of controls I use in my code is this:
xaml code:
<Button x:Name="button1" Grid.Column="0" Grid.Row="0" Content="Button1" Click="button1_click" />
<Button x:Name="button2" Grid.Column="1" Grid.Row="0" Content="Button2" Click="button2_click" />
<Button x:Name="button3" Grid.Column="2" Grid.Row="0" Content="Button3" Click="button3_click" />
c# code:
private void button1_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}
private void button2_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}
private void button3_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}
in above code, it is not possible to navigate between three buttons using mouse wheel(both in visual studio and raspberry pi).
AGAIN ATTACHED MOUSE WHEEL IS NOT WORKING HERE.
How did you register the 'MOUSE WHEEL' event in your code? It worked well on my side.
Please see the following code sample:
<StackPanel x:Name="root" >
<Button x:Name="button1" Grid.Column="0" Grid.Row="0" Content="Button1" Click="button1_click" />
<Button x:Name="button2" Grid.Column="1" Grid.Row="0" Content="Button2" Click="button2_click" />
<Button x:Name="button3" Grid.Column="2" Grid.Row="0" Content="Button3" Click="button3_click" />
</StackPanel>
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.PointerWheelChanged += CoreWindow_PointerWheelChanged;
}
private async void CoreWindow_PointerWheelChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.PointerEventArgs args)
{
Debug.WriteLine(args.CurrentPoint.Properties.MouseWheelDelta);
UIElement element;
if (args.CurrentPoint.Properties.MouseWheelDelta > 0)
{
element = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Up);
if (element == null)
{
element = FocusManager.FindLastFocusableElement(root) as UIElement;
}
var result = await FocusManager.TryFocusAsync(element, FocusState.Keyboard);
Debug.WriteLine((element as Button).Content.ToString() + " focused: " + result.Succeeded);
}
else
{
element = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Down);
if (element == null)
{
element = FocusManager.FindFirstFocusableElement(root) as UIElement;
}
var result = await FocusManager.TryFocusAsync(element, FocusState.Keyboard);
Debug.WriteLine((element as Button).Content.ToString() + " focused: " + result.Succeeded);
}
}
just to give you an idea.
First you should handle tabIndex properties of all of those Items on form and set their order. also whats being triggered with you will move with the tab in your case with mouse wheel gonna be "Focused" or "GotFocus" method. so an event like "GotFocus" will be needed. also you need to handle mouse wheel movements(up or down). you may google about how to override TabIndex property from tab Key to Mouse wheel as you want.
I'm developing an app and I'd like when the user is in Tablet Mode and switches from one app to other to show a black screen when you press "alt + tab" and the opened apps are being shown. I'd like instead of showing the "myTrip" screenshot of the app to show a black screen.
I know for WPF we had ShowInTaskbar = false but nothing like that in Windows 10 Universal App Platform.
I tried so far:
Window.Current.CoreWindow.VisibilityChanged += CoreWindow_VisibilityChanged;
private void Current_VisibilityChanged(object sender, VisibilityChangedEventArgs e)
{
var parentGrid = RootPanel();
parentGrid.Visibility = e.Visible ? Visibility.Visible : Visibility.Collapsed;
}
But the snapshot image of the app is taken before those events are being called. Any idea on how to do it?
Regards.
I don't get why exactly you want to do this, but here it goes.
You'll need to handle the Activated event of the current thread, and than place a control over your content. See the example bellow.
First, the XAML:
<Canvas Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<Canvas x:Name="contetProtector" Canvas.ZIndex="10" Background="Black" Width="1014" Height="758" Visibility="Collapsed"/>
<TextBlock Text="My precious content" FontSize="50" Canvas.Top="50" Canvas.Left="50"/>
<TextBlock Text="Nobody should see it" FontSize="50" Canvas.Top="100" Canvas.Left="50"/>
</Canvas>
Then, the codebehind of the page:
public MainPage()
{
this.InitializeComponent();
CoreWindow.GetForCurrentThread().Activated += CoreWindowOnActivated;
}
private void CoreWindowOnActivated(CoreWindow sender, WindowActivatedEventArgs args)
{
if(args.WindowActivationState == CoreWindowActivationState.Deactivated)
this.contetProtector.Visibility = Visibility.Visible;
else
this.contetProtector.Visibility = Visibility.Collapsed;
}
Here you can see the unprotected/active screen, and here the protected one.
Hope it helps.
In my app there is a canvas with a small image (not taking up entire space of canvas).
when user clicks on image and drags inside the canvas, it traces the path with a black line. Till here everything works. Now, I want the path to be deleted as soon as the user releases the hold on the mouse. I am using MouseLeftButtonUp on the canvas to detect the mouse release. Now problem is this is working randomly. sometimes it fires and sometimes it does not. Kindly help.
XAML:
<Canvas Height="400" HorizontalAlignment="Center" Margin="10,10,0,0" Name="canvas1" VerticalAlignment="Center" Background="Aqua" Width="400">
<Image Canvas.ZIndex="30" Canvas.Left="10" Canvas.Top="10" Height="20" Name="dot1" Stretch="Uniform" Width="20" Source="/BoxIt;component/Images/dot.png" MouseLeftButtonDown="dot_MouseLeftButtonDown" />
</Canvas>
C#:
this.canvas1.MouseMove += new MouseEventHandler(canvas1_MouseMove);
this.canvas1.MouseLeftButtonUp += new MouseButtonEventHandler(canvas1_MouseLeftButtonUp);
Eventhandler in C# :
void canvas1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// code to delete the path
}
In Windows Phone we are advised to use the Tap, Hold and Manipulation* methods instead of the Mouse related events.
Please read this for more information.
I have a WPF application that I am working on where there is a button that is obscured by a partially opaque rectangle overlay. The button is still visible, but it can not receive any events because they are all caught by the rectangle which is on top of it.
Is there any way to set a pass-through so that the event is received by the next visual item underneath? If not is there some other workaround that could be used in this situation?
Set IsHitTestVisible="false" on the opaque overlay.
You need to set IsHitTestVisible="False" for the control over your button.
This example shows that a button is covered by a border, but the border doesn't get any event since because of the IsHitTestVisible="False" condition of border:
<Grid Background="Yellow">
<Button Click="Button_Click" Width="100" Height="25"/>
<Border Background="Cyan" Opacity="0.4" Width="200" Height="200" IsHitTestVisible="False" />
</Grid>
C# code,
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("iiiiiii");
}
You could add the your own event to the rectangle event (or in the click event of the rectangle it self) and check there if it's with in buttons area
Rectangle.Click += your_click_event;
private void your_click_event(object sender, RoutedEventArgs e)
{
//check if it's coordinates are within the underlining button.
//fire button click event
}
But it would be more convient to set
IsHitTestVisible="False"
Like mentioned in other posts. UIElement.IsHitTestVisible Property
I would recommend to look in to routed events in WPF. Routed events get routed based primarily on the visual tree. Routed events support a RoutingStrategy of Bubble, Tunnel, or Direct.
Understanding Routed Events and Commands In WPF