WPF 精修篇 调用Win32Api
Posted lonelyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 精修篇 调用Win32Api相关的知识,希望对你有一定的参考价值。
原文:WPF 精修篇 调用Win32Api
栗子是 调用WIn32API 让窗口最前
后台代码
- [DllImport("user32.dll")]
- private static extern bool SetForegroundWindow(IntPtr hWnd);
-
- //
- Process process = null;
- private void OPenButton_Click(object sender, RoutedEventArgs e)
- {
- process = new Process();
- process.StartInfo.FileName = "iexplore.exe";
- process.StartInfo.Arguments = Text_Path.Text;
- process.Start();
- }
-
- private void TOPButton_Click(object sender, RoutedEventArgs e)
- {
- if (process != null)
- {
- var ptr = process.MainWindowHandle;
- SetForegroundWindow(ptr);
- }
- }
-
- private void FlushButton_Click(object sender, RoutedEventArgs e)
- {
- if (process != null)
- {
- var ptr = process.MainWindowHandle;
- SetForegroundWindow(ptr);
- SendKeys.SendWait("{F5}");
- }
- }
前台
- <Grid>
- <TextBox x:Name="Text_Path" HorizontalAlignment="Left" Height="25" Margin="0,34,0,0" TextWrapping="Wrap" Text="https://www.baidu.com" VerticalAlignment="Top" Width="251" />
- <Button x:Name="OPenButton" Content="打开" HorizontalAlignment="Left" Margin="10,86,0,0" VerticalAlignment="Top" Width="75" Click="OPenButton_Click"/>
- <Button x:Name="TOPButton" Content="前台" HorizontalAlignment="Left" Margin="10,123,0,0" VerticalAlignment="Top" Width="75" Click="TOPButton_Click"/>
- <Button x:Name="FlushButton" Content="刷新" HorizontalAlignment="Left" Margin="10,160,0,0" VerticalAlignment="Top" Width="75" Click="FlushButton_Click"/>
-
- </Grid>
SendKeys.SendWait("{F5}"); 需要引用System.Windows.Forms.dll
以上是关于WPF 精修篇 调用Win32Api的主要内容,如果未能解决你的问题,请参考以下文章