我正在尝试附加 WPF 窗口作为外部应用程序(如记事本)的子窗口以提供覆盖。在研究了我可以在 SO 和 MSDN 上找到的所有答案之后,我已经在我的 WPF 应用程序运行时在记事本的一角创建了一个坚实的覆盖层。然而,
这是演示该问题的最小示例:
Overlay.xaml
<Window x:Class="WindowControlTest.Overlay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
Opacity="1"
Background="Azure"
MouseMove="Window_MouseMove"
GotFocus="Window_GotFocus"
Loaded="Window_Loaded"
Title="Overlay"
WindowStyle="None"
>
</Window>
Overlay.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Interop;
namespace WindowControlTest
{
public partial class Overlay : Window
{
IntPtr m_ParentHwnd;
HwndSource m_HwndSource;
public Overlay(IntPtr parentHwnd)
{
InitializeComponent();
m_ParentHwnd = parentHwnd;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine("Overlay.Window_MouseMove: " + e.GetPosition(this));
}
private void Window_GotFocus(object sender, RoutedEventArgs e)
{
Console.WriteLine("Overlay.Window_GotFocus");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HwndSourceParameters parameters = new HwndSourceParameters();
parameters.WindowStyle = (int) (WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD);
parameters.SetPosition(0, 0);
parameters.UsesPerPixelOpacity = true;
parameters.SetSize((int)Width, (int)Height);
parameters.ParentWindow = m_ParentHwnd;
m_HwndSource = new HwndSource(parameters);
m_HwndSource.CompositionTarget.BackgroundColor = Colors.Aqua;
m_HwndSource.RootVisual = (Visual)Content;
}
}
}
主窗口.xaml
<Window x:Class="WindowControlTest.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"
Loaded="OnLoaded"
>
<StackPanel>
<Label x:Name="stateLabel">Label</Label>
</StackPanel>
</Window>
MainWindow.xaml.cs - 查找并处理记事本并创建一个Overlay
using System;
using System.Text;
using System.Windows;
namespace WindowControlTest
{
public partial class MainWindow : Window
{
private IntPtr m_TargetHwnd;
private Overlay m_Overlay;
public MainWindow()
{
InitializeComponent();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
processWindows();
if (m_TargetHwnd != IntPtr.Zero)
{
m_Overlay = new Overlay(m_TargetHwnd);
m_Overlay.Show();
}
}
private void processWindows()
{
Win32.EnumWindows(delegate(IntPtr wnd, IntPtr param)
{
String text = GetWindowText(wnd);
Console.WriteLine("Window: " + text);
if (text.Contains("Notepad"))
{
m_TargetHwnd = wnd;
}
return true;
}, IntPtr.Zero);
}
public static string GetWindowText(IntPtr hWnd)
{
int size = Win32.GetWindowTextLength(hWnd);
if (size++ > 0)
{
var builder = new StringBuilder(size);
Win32.GetWindowText(hWnd, builder, builder.Capacity);
return builder.ToString();
}
return String.Empty;
}
}
}
(注意:许多 SO 问题解决了一个相似但不同的问题,例如 How to set Win32 window as owner of WPF window? 假设我控制着 Win32 窗口的源代码,我可以在 MSDN 上找到的 examples 也是如此。)
最佳答案
我认为你走错了路,而不是让你的覆盖层成为记事本窗口的子窗口,你应该尝试通过 HwndHost 使记事本成为主窗口的子窗口,然后将覆盖层保持在主机之上。
这是我的博客 post介绍一个library我写的是在 HwndHost 托管的任何 hwnd 上分层 WPF 装饰器。一个简单的网络浏览器演示如下所示:
关于c# - 如何在外部 Win32 应用程序窗口上添加 WPF 覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30872931/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r