How to show a preview of screen saver inside WPF window - c#

I want to be able to show a preview of a screen saver inside WPF window. (using a container or control or ...) I know that Windows itself passes "/p" argument to the screen saver to get a preview. But how can I show that preview inside my WPF application? Should I get a handle of it and change its parent to my container o control? How?

You need to use Windows.Forms interop, because screen savers expect windows handles (HWND) and in WPF, only top-level windows have them.
MainWindow.xaml
<Window x:Class="So18547663WpfScreenSaverPreview.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="Screen Saver Preview" Height="350" Width="525"
Loaded="MainWindow_OnLoaded" Closed="MainWindow_OnClosed"
SizeToContent="WidthAndHeight">
<StackPanel Orientation="Vertical" Margin="8">
<TextBlock Text="Preview"/>
<WindowsFormsHost x:Name="host" Width="320" Height="240">
<forms:Control Width="320" Height="240"/>
</WindowsFormsHost>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Diagnostics;
using System.Windows;
namespace So18547663WpfScreenSaverPreview
{
public partial class MainWindow
{
private Process saver;
public MainWindow ()
{
InitializeComponent();
}
private void MainWindow_OnLoaded (object sender, RoutedEventArgs e)
{
saver = Process.Start(new ProcessStartInfo {
FileName = "Bubbles.scr",
Arguments = "/p " + host.Child.Handle,
UseShellExecute = false,
});
}
private void MainWindow_OnClosed (object sender, EventArgs e)
{
// Optional. Screen savers should close themselves
// when the parent window is destroyed.
saver.Kill();
}
}
}
Assembly references
WindowsFormsIntegration
System.Windows.Forms
Related links
Walkthrough: Hosting a Windows Forms Control in WPF
Creating a Screen Saver with C# (describes command line arguments)

Related

WPF WebView doesn't show

I'm learning WPF WebView control, I have the below MainWindow.xaml file:
<Window x:Class="MyWPF.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:MyWPF"
xmlns:WPF="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls.WebView"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid x:Name="Grid1" Grid.Row="1" Grid.Column="1">
<WPF:WebView x:Name="webView1"
Grid.Row="0"
Grid.Column="0"
Loaded="WebView_Loaded" />
</Grid>
</Window>
and the below corresponding MainWindow.xaml.cs file:
namespace MyWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
webView1.Navigate(new Uri("https://www.google.com"));
}
private void WebView_Loaded(object sender, RoutedEventArgs e)
{
}
}
}
But I don't get any expecting popup window. If I remove
webView1.Navigate(new Uri("https://www.google.com"));
I can get a blank popup window. What is wrong with my code and how to solve it?
UPDATE1
I added Window_Loaded event handler and move the below two in it, now I can see a blank popup window but without any content as well as message box.
Uri uri = new Uri("https://www.google.com");
WebView1.Navigate(uri);
The whole updated code-behind:
namespace MyWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
Uri uri = new Uri("https://www.google.com");
WebView1.Navigate(uri);
}
}
}
UPDATE2
I added one HyperLink and one Button into xaml file, the below is the updated xaml file:
<Window x:Class="MyWPF.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:MyWPF"
xmlns:WPF="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls.WebView"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" AllowsTransparency="False" >
<Grid x:Name="Grid1" Visibility="Visible" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="71*"/>
</Grid.ColumnDefinitions>
<WPF:WebView x:Name="WebView1" Visibility="Visible" Grid.Column="1" Margin="50,50,50,50">
</WPF:WebView>
<Button Name="BTN1" Click="BTN1_Click" Margin="285,380,285,10" Grid.Column="1" >Click Me</Button>
<TextBlock Margin="0,0,747,390" Grid.ColumnSpan="2">
<Hyperlink
NavigateUri="http://www.google.com">
Google
</Hyperlink>
</TextBlock>
</Grid>
</Window>
and adding Button click event handler into the code-behind with webview initialization inside it:
namespace MyWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
//Uri uri = ;
WebView1.Navigate(new Uri("https://www.google.com"));
}
private void BTN1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
WebView1.Navigate(new Uri("https://www.google.com"));
}
}
}
Nothing happens after clicking the HyperLink. I see popup Messagebox after clicking button but still google doesn't show up.
UPDATE3
I followed the link:
https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/webview#high-dpi
I set Source property Source="http://www.google.com" inside the xaml file, see the below code:
<Window x:Class="MyWPF.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:MyWPF"
xmlns:WPF="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls.WebView"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" AllowsTransparency="False" >
<Grid x:Name="Grid1" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="71*"/>
</Grid.ColumnDefinitions>
<WPF:WebView x:Name="WebView1" Visibility="Visible" Grid.Column="1" Margin="50,50,50,50" Source="http://www.google.com" >
</WPF:WebView>
<Button Name="BTN1" Click="BTN1_Click" Margin="285,380,285,10" Grid.Column="1" >Click Me</Button>
<TextBlock Margin="0,0,747,390" Grid.ColumnSpan="2">
<Hyperlink
NavigateUri="http://www.google.com">
Google
</Hyperlink>
</TextBlock>
</Grid>
</Window>
But still not working.
UPDATE4
I added((ISupportInitialize)webView).BeginInit() and ((ISupportInitialize)webView).EndInit() into the button click event handler, see the below:
using Microsoft.Toolkit.Wpf.UI.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
}
private void BTN1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Loaded");
((ISupportInitialize)WebView1).BeginInit();
((ISupportInitialize)WebView1).EndInit();
WebView1.Navigate(new Uri("https://localhost:3000"));
Console.ReadLine();
}
}
}
But get the below error message at this line:
((ISupportInitialize)WebView1).BeginInit();
:
$exception {"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure
\"Microsoft.Toolkit.Win32.UI.Controls.DesignerUI.resources\" was
correctly embedded or linked into assembly
\"Microsoft.Toolkit.Wpf.UI.Controls.WebView\" at compile time, or that
all the satellite assemblies required are loadable and fully
signed."} System.Resources.MissingManifestResourceException
Then I went back and added more properties based on this link:
https://github.com/rjmurillo/webview-samples/blob/master/Toolkit/dotnet/Microsoft.Toolkit.Win32.Samples.WPF.WebView/MainWindow.xaml
<WPF:WebView x:Name="WebView1" Visibility="Visible" Grid.Column="1" Margin="50,50,50,50" Source="http://www.google.com" IsIndexedDBEnabled="True"
IsJavaScriptEnabled="True"
IsPrivateNetworkClientServerCapabilityEnabled="True"
IsScriptNotifyAllowed="True" >
</WPF:WebView>
But still WebView not working.
I was having the same issue upgrading from the first preview Toolkit, and it was all because the Navigate function causing to start window hidden.
Change from:
WebView1.Navigate(new Uri("https://localhost:3000"));
To:
WebView1.Source = new Uri("https://localhost:3000");
I was also having this issue and even using the myWebBrowser.Source = new Uri("https://www.google.com"); it was not showing the page.
I added the following to the code and now it loads fine.
myWebBrowser.BringIntoView();
I am using this with WPF and displaying it in a popup window, I'm not sure if that has anything to do with the issue I was having or not.
Did you run your program as an administrator (with elevated privileges), either directly or through Visual Studio?
According to this discussion this is not possible, the Win32 WebView does not support running under an elevated token.
If you check your Windows event log, you should also find an application error like the one mentioned in the discussion.
I also tried to use WebView WPF, but it can not show when I use webview.Source = url as accepted answer.
Finally, I changed to use CefSharp.Wpf library following this link https://www.technical-recipes.com/2016/using-the-cefsharp-chromium-web-browser-in-wpf-xaml/
And it worked perfectly at my site, i shared for whom concerned.
Create a new WPF project
Obtain the CefSharp packages using NuGet
Select Tool > NuGet Package manager > Package Manager Console.
Use the PM> install-package CefSharp.Wpf
Use in WPF as
<Window x:Class="CefSharpBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
Title="MainWindow"
Height="350" Width="525">
<Grid>
<wpf:ChromiumWebBrowser
x:Name="Browser"
Address="http://www.myaddress.com" />
</Grid>
</Window>

usercontrol open different windows c#

hello i am having some problem while using user control in wpf. i have single image in user control . i want to place this user control on my main window which contains some map.i am placing this user control(image) on my main window.they are 10 t0 15 .what i want is to open different windows by clicking on this usercontrol which is just an image . but when i do that it show me only same window every time . is there any way so i just have to create single user control and use it multiple times to open different windows .like (window1,window2,window3) etc..
kindly help me . i will be really thankfull.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Demo.WindowsPresentation.CustomMarkers.CustomMarkerDemo"
Height="40" Width="30" Opacity="10">
<Image MouseDown="clickit" Name="icon" Source="bigMarkerGreen.png" VerticalAlignment="Center" HorizontalAlignment="Center" />
In cs
this.MouseDown += new MouseButtonEventHandler(clickit);
void clickit(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("test");
main.show();
}
As far as I could understand by the given information, you want to open this UserControl in different windows. I would suggest you to create a window and add this UserControl onto it like
<Window x:Class="Demo.WindowsPresentation.CustomMarkers.win.winCustomMarker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:marker="clr-namespace:Demo.WindowsPresentation.CustomMarkers"
Title="Window" Height="345" Width="380">
<Grid>
<marker:CustomMarkerDemo></marker:CustomMarkerDemo>
</Grid>
</Window>
And then in your mouse click event:
void clickit(object sender, MouseButtonEventArgs e)
{
winCustomMarker customMarkerWindow = new winCustomMarker();
//bind any information you want that you want to pass to your inner UserControl
customMarkerWindow.show();
}
You may consider launching them as separate processes. Move user control to a new WPF project(.exe) and from main app create as many instances of user control you need.
var imgUserControlProcess = new ProcessStartInfo
{
FileName = "ImageUserControl.exe",
UseShellExecute = false,
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Normal
};
var processStart = new Process
{
StartInfo = mainViewProcess
};
processStart.Start();

WPF Ribbon ApplicationMenu open and close event

I'm using the WPF Ribbon Application Menu:
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11877
https://msdn.microsoft.com/de-de/library/ff799534(v=vs.110).aspx
How can I close the Application (File) Menu programmatically?
How can I detect if the user opens the Application Menu? I didn't found an appropriated event
You need IsDropDownOpen property and related event(s).
XAML (this is for .NET 4.5+, but for 4.0 it will be almost the same, the difference will be in namespace prefix):
<StackPanel>
<Ribbon>
<Ribbon.ApplicationMenu>
<RibbonApplicationMenu x:Name="Menu" DropDownOpened="RibbonApplicationMenu_DropDownOpened">
<RibbonApplicationMenuItem Header="Foo"/>
<RibbonApplicationMenuItem Header="Bar"/>
</RibbonApplicationMenu>
</Ribbon.ApplicationMenu>
</Ribbon>
</StackPanel>
Code-behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void RibbonApplicationMenu_DropDownOpened(object sender, EventArgs e)
{
// user has opened menu
Debug.WriteLine("Menu opened.");
// let's close it from code
Menu.IsDropDownOpen = false;
}
}
Also, you may want to disable entire menu. This can be done using IsEnabled property.

How to set transparent background for richtextbox using the WPF RichTextBoxToolBar Control?

I am using the WPF RichTextBoxToolBar control from this Code Project project, combined with the Exteneded WPF Toolkit's RichTextBox control.
When I enter text for the first time with no formatting it's all ok and the background is Transparent. But when I use the toolbar to set the background to Transparent it actually sets it to White. I've noticed the additional \highlight1 parameter in the RTF format of the text which does this thing.
Does anyone have experience with this toolbar? Is there a way to change this behavior? Can I disable the background color picker command?
Following is the code of a small test project I made to investigate this. The project only needs to install the Extended WPF Toolkit from NuGet and reference the dll from this zip.
XAML:
<Window x:Class="RtbTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:rtb="clr-namespace:RichTextBoxToolBar;assembly=RichTextBoxToolBar"
xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<rtb:RichTextBoxToolBar Margin="0,0,0,5" DockPanel.Dock="Top" Name="EditTextToolbar" />
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
<Button Content="Save RTF text" Click="ShowRtfText"/>
<Button Content="Reload RTF text" Click="ReloadRtfText"/>
</StackPanel>
<toolkit:RichTextBox Name="EditTextRtb">
<toolkit:RichTextBox.TextFormatter>
<toolkit:RtfFormatter />
</toolkit:RichTextBox.TextFormatter>
</toolkit:RichTextBox>
</DockPanel>
</Window>
Codebehind:
namespace RtbTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ShowRtfText(object sender, RoutedEventArgs e)
{
MessageBox.Show(EditTextRtb.Text);
}
private void ReloadRtfText(object sender, RoutedEventArgs e)
{
string rtfText;
rtfText = EditTextRtb.Text;
EditTextRtb.Text = rtfText;
}
}
}

How to access commandbindings in c# WPF application?

I'm using C# WPF application to display crystal reports and i want to call a function when crystal report viewer refresh button is clicked.
so in viewer properties i set command binding to refresh and application XAML looks like below
<Grid>
<cr:CrystalReportsViewer Name="reportViewer">
<cr:CrystalReportsViewer.CommandBindings>
<CommandBinding Command="NavigationCommands.Refresh"/>
</cr:CrystalReportsViewer.CommandBindings>
</cr:CrystalReportsViewer>
</Grid>
any idea how to capture this refresh event and call the command handler?
Regards
cr:CrystalReportViewer is not a WPF control; therefore it has no CommandBindings property.
In order to be notified when the Refresh button was clicked, you could proceed as follows:
XAML:
<Window x:Class="CRTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cr="clr-namespace:CrystalDecisions.Windows.Forms;assembly=CrystalDecisions.Windows.Forms"
Title="Window1" Height="300" Width="300">
<Grid>
<WindowsFormsHost>
<cr:CrystalReportViewer x:Name="reportViewer" ReportRefresh="OnReportRefresh"/>
</WindowsFormsHost>
</Grid>
</Window>
Code-behind:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
// Load a sample report:
ReportDocument doc = new ReportDocument();
doc.Load(#"C:\Temp\test.rpt");
reportViewer.ReportSource = doc;
}
private void OnReportRefresh(object source, CrystalDecisions.Windows.Forms.ViewerEventArgs e)
{
MessageBox.Show("Refresh clicked!");
}
}
Your code-behind must include
using CrystalDecisions.CrystalReports.Engine;
You need to add references to WindowsFormIntegration, CrystalDecisions.Windows.Forms, and CrystalDecisions.CrystalReports.Engine to your Visual Studio project.

Categories