WPF 精修篇 调用Win32Api

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 精修篇 调用Win32Api相关的知识,希望对你有一定的参考价值。

原文:WPF 精修篇 调用Win32Api

技术图片

栗子是 调用WIn32API 让窗口最前 

后台代码

  1. [DllImport("user32.dll")]
  2. private static extern bool SetForegroundWindow(IntPtr hWnd);
  3. //
  4. Process process = null;
  5. private void OPenButton_Click(object sender, RoutedEventArgs e)
  6. {
  7. process = new Process();
  8. process.StartInfo.FileName = "iexplore.exe";
  9. process.StartInfo.Arguments = Text_Path.Text;
  10. process.Start();
  11. }
  12. private void TOPButton_Click(object sender, RoutedEventArgs e)
  13. {
  14. if (process != null)
  15. {
  16. var ptr = process.MainWindowHandle;
  17. SetForegroundWindow(ptr);
  18. }
  19. }
  20. private void FlushButton_Click(object sender, RoutedEventArgs e)
  21. {
  22. if (process != null)
  23. {
  24. var ptr = process.MainWindowHandle;
  25. SetForegroundWindow(ptr);
  26. SendKeys.SendWait("{F5}");
  27. }
  28. }

前台

  1. <Grid>
  2. <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" />
  3. <Button x:Name="OPenButton" Content="打开" HorizontalAlignment="Left" Margin="10,86,0,0" VerticalAlignment="Top" Width="75" Click="OPenButton_Click"/>
  4. <Button x:Name="TOPButton" Content="前台" HorizontalAlignment="Left" Margin="10,123,0,0" VerticalAlignment="Top" Width="75" Click="TOPButton_Click"/>
  5. <Button x:Name="FlushButton" Content="刷新" HorizontalAlignment="Left" Margin="10,160,0,0" VerticalAlignment="Top" Width="75" Click="FlushButton_Click"/>
  6. </Grid>

  SendKeys.SendWait("{F5}"); 需要引用System.Windows.Forms.dll

以上是关于WPF 精修篇 调用Win32Api的主要内容,如果未能解决你的问题,请参考以下文章

WPF 精修篇 全局为处理异常处理

WPF 精修篇 属性触发器

WPF 精修篇 Winform 嵌入WPF控件

WPF 精修篇 用户控件

WPF 精修篇 WPF嵌入Winfrom控件

WPF 精修篇 BackgroundWorker