- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些问题。
我在表单中有一个面板和一个图片框。
我想打开一个 Windows 应用程序(例如记事本)并将其作为面板的父级。
然后我想在 PictureBox 中显示面板内容的图像:
我的代码:
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
IntPtr appWin1;
private void Notepad_Button_Click(object sender, EventArgs e)
{
ProcessStartInfo ps1 = new ProcessStartInfo(@"notepad.exe");
ps1.WindowStyle = ProcessWindowStyle.Minimized;
Process p1 = Process.Start(ps1);
System.Threading.Thread.Sleep(5000); // Allow the process to open it's window
appWin1 = p1.MainWindowHandle;
// Put it into this form
SetParent(appWin1, this.panel1.Handle);
}
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(panel1.Width, panel1.Height);
Graphics g = Graphics.FromImage(bm);
g.CopyFromScreen(0, 0, 0, 0, bm.Size);
pictureBox1.Image = bm;
}
private void Form5_Load(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void timer2_Tick(object sender, EventArgs e)
{
MoveWindow(appWin1, 0, 0, this.panel1.Width, this.panel1.Height, true);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
运行这段代码:
问题是这样的:我只想获取面板内部的内容,但是在我的面板前面移动的任何其他窗口都包含在 PictureBox 中显示的位图中:
[ 3
如图所示,当一个外部页面放在panel1
上时,它也会出现在pictureBox1
中(图中红框)。
我只希望记事本出现在 pictureBox1
中,无论什么窗口悬停在 panel1
上。
最佳答案
由于您想将 Panel 的内容渲染为 Bitmap - 它托管着一个外部应用程序 - 您不能使用 Control.DrawToBitmap() : 托管应用程序的内容,父级调用 SetParent()
,不会被打印。
由于您还想从渲染中排除可能悬停在面板上的任何其他窗口,因此您可以使用 PrintWindow函数,传递面板父窗体的句柄。
表单将收到 WM_PRINT
或 WM_PRINTCLIENT
消息,并将自身打印到指定的设备上下文:在本例中,从位图生成。
这不是屏幕截图:Window 将自身及其内容绘制到 DC,因此其他 Windows 是否悬停/部分或完全重叠并不重要,结果是相同的。
我正在使用 DwmGetWindowAttribute ,设置 DWMWA_EXTENDED_FRAME_BOUNDS
,以获取要打印的窗口的边界。它可能看起来冗余,但是,如果您的应用不是 DpiAware 并且您有一个高 Dpi 屏幕,那么在这种情况下它会显得不那么冗余。最好有它,IMO。
此函数比 GetWindowRect
或 GetClientRect
更可靠,它将在可变 DPI 上下文中返回正确的测量值。
不能将子Window的句柄传给这个函数,所以我是用Parent Form的句柄生成Bitmap,然后选择Panel所在的区段。这取决于:
[ParentForm].RectangleToClient([Child].RectangleToScreen([Child].ClientRectangle))
启动进程和父记事本窗口到面板控件:
private void Notepad_Button_Click(object sender, EventArgs e)
{
var psi = new ProcessStartInfo(@"notepad.exe") { WindowStyle = ProcessWindowStyle.Minimized };
using (var p = Process.Start(psi)) {
p.WaitForInputIdle();
var handle = p.MainWindowHandle;
if (handle != IntPtr.Zero) {
SetParent(handle, panel1.Handle);
MoveWindow(handle, 0, 0, panel1.Width, panel1.Height, true);
}
}
}
现在,无论何时,只要您想将 Panel 所在的 Form 部分渲染为 Bitmap,调用 RenderWindow 并获取您感兴趣的部分:Panel 的 ClientRectangle。
using (var image = RenderWindow(this.Handle, true, false)) {
if (image is null) return;
if (WindowState == FormWindowState.Minimized) return;
var panelRect = RectangleToClient(panel1.RectangleToScreen(panel1.ClientRectangle));
pictureBox1.Image?.Dispose();
pictureBox1.Image = image.Clone(panelRect, PixelFormat.Format32bppArgb);
}
// [...]
public static Bitmap RenderWindow(IntPtr hWnd, bool clientAreaOnly, bool tryGetFullContent = false)
{
var printOption = clientAreaOnly ? PrintWindowOptions.PW_CLIENTONLY : PrintWindowOptions.PW_DEFAULT;
printOption = tryGetFullContent ? PrintWindowOptions.PW_RENDERFULLCONTENT : printOption;
var hResult = DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out Rectangle dwmRect, Marshal.SizeOf<Rectangle>());
if (hResult < 0) {
Marshal.ThrowExceptionForHR(hResult);
return null;
}
if (dwmRect.Width <= 0 || dwmRect.Height <= 0) return null;
var bmp = new Bitmap(dwmRect.Width, dwmRect.Height);
using (var g = Graphics.FromImage(bmp)) {
var hDC = g.GetHdc();
try {
var success = PrintWindow(hWnd, hDC, printOption);
if (!success) {
// Failed, see what happened
var win32Error = Marshal.GetLastWin32Error();
return null;
}
return bmp;
}
finally {
g.ReleaseHdc(hDC);
}
}
}
Win32 声明:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
public enum PrintWindowOptions : uint
{
PW_DEFAULT = 0,
PW_CLIENTONLY = 1,
PW_RENDERFULLCONTENT = 2 // Undocumented. Use, e.g., with a WebBrowser
}
[DllImport("user32.dll", SetLastError = true)]
static extern internal bool PrintWindow(IntPtr hwnd, IntPtr hDC, PrintWindowOptions nFlags);
public enum DWMWINDOWATTRIBUTE : uint
{
DWMWA_NCRENDERING_ENABLED = 1, // [get] Is non-client rendering enabled/disabled
DWMWA_NCRENDERING_POLICY, // [set] DWMNCRENDERINGPOLICY - Non-client rendering policy - Enable or disable non-client rendering
DWMWA_TRANSITIONS_FORCEDISABLED, // [set] Potentially enable/forcibly disable transitions
DWMWA_ALLOW_NCPAINT, // [set] Allow contents rendered In the non-client area To be visible On the DWM-drawn frame.
DWMWA_CAPTION_BUTTON_BOUNDS, // [get] Bounds Of the caption button area In window-relative space.
DWMWA_NONCLIENT_RTL_LAYOUT, // [set] Is non-client content RTL mirrored
DWMWA_FORCE_ICONIC_REPRESENTATION, // [set] Force this window To display iconic thumbnails.
DWMWA_FLIP3D_POLICY, // [set] Designates how Flip3D will treat the window.
DWMWA_EXTENDED_FRAME_BOUNDS, // [get] Gets the extended frame bounds rectangle In screen space
DWMWA_HAS_ICONIC_BITMAP, // [set] Indicates an available bitmap When there Is no better thumbnail representation.
DWMWA_DISALLOW_PEEK, // [set] Don't invoke Peek on the window.
DWMWA_EXCLUDED_FROM_PEEK, // [set] LivePreview exclusion information
DWMWA_CLOAK, // [set] Cloak Or uncloak the window
DWMWA_CLOAKED, // [get] Gets the cloaked state Of the window. Returns a DWMCLOACKEDREASON object
DWMWA_FREEZE_REPRESENTATION, // [set] BOOL, Force this window To freeze the thumbnail without live update
PlaceHolder1,
PlaceHolder2,
PlaceHolder3,
DWMWA_ACCENTPOLICY = 19
}
[DllImport("dwmapi.dll", SetLastError = true)]
static extern internal int DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, out Rectangle pvAttribute, int cbAttribute);
关于c# - 打印面板内容的图像,不包括任何外部重叠窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68890833/
127.0.0.1:8000/api/仅包含来自第二个应用程序的 url,但我将两个 url 模块链接到相同的模式。甚至有可能做到这一点吗? 第一个应用程序: from django.urls imp
我目前正在学习 ColdFusion。我有 PHP 背景,对此我有点困惑。 我有一个选择菜单,我希望将选项保存在不同的文件中。 (例如 options.cfm)当我调用文件时,我想在选择菜单中包含选项
字符串: "75,000", "is", "95%", "or", "95/100" "of", "monthly", "income" o/p: "is","%, "or", "/", "of",
我有 4 个 javascript 文件(每个文件对应一个 HTML 文件),所有 4 个文件中的 3 个函数都是相同的。我想找到一个顺利的解决方案,我可以以某种方式分别包含这 3 个函数...是否可
我在 PHP 中有这种情况,其中 include在一台服务器上被遗漏,但在另一台服务器上没有(我没有设置服务器,所以我不能告诉你更多;我不是真正的 devops 人,所以这就是我在这里问的原因)。两台
这是一个模式文件,midi.xsd定义类型,note ,用于存储 MIDI 音符值: 这是另一个模式文件,octaves.xsd使用
我想备份以下文件夹 /home /etc /usr/local /root /var /boot 并排除 /var/tmp /var/run /var/lock /home/*/.thumbnails
如何重新编码具有许多值(包括缺失值)的数值变量,以获得数字 0:n-1哪里n是唯一值的数量,包括 NA ,整齐? 例子: df 1 1000 0 2 1000 0 3 N
选择元素的 html(包括在内)的最佳方法是什么?例如: This is just a test. 而$('#testDiv').html()返回"This is just a test."
我正在尝试设置Varnish来处理本地环境中的ESI包含。 我在虚拟机中运行 Varnish ,内容在主机上运行。 我有两个文件“index.html”和“test.html”。它们都存储在apach
我有以下内容,并且想要检索“ FromEmail”不为空的数据 Simple email@gma
欧海,我正在编写一个小型 PHP 应用程序,使用一个单独的 config.php 文件和一个functions.php,其中包含我将在应用程序中使用的所有自定义函数。现在,我真的必须在每个函数中包含
我知道可以将 JavaScript 放在一个特定的 .js 文件中,然后通过执行以下操作将其包含在任何页面中...... 我注意到,对于包含的这些 .js 文件: 它们实际上不必以 .js 结尾 其
我使用 gwt UIBinder 添加了一些项目到我的 ComboBox。 --select one-- Dispute Referral Form Dispute Settlement Clause
我可以将一个 first.c 文件包含到另一个 second.c 中吗? (我正在做一些套接字编程,以将服务器收到的消息存储在链接列表中,因此在第一个程序中,我尝试保留链接列表和第二个程序套接字编程文
我有一个简单的 Spring MVC 数据项目设置,我试图选择 Admin 中尚不存在的用户列表。 table 。这是我的存储库方法 SELECT u FROM User u WHERE u.id N
在 bash 脚本中,使用什么实用程序以及如何删除两个字符串之间的文本,包括字符串。 原文: (ABC blah1)blah 2(def blah 5)blah 7)(DEF blah 8)blah
我有这个 BST 问题,我试图用 Java 解决,但我不知道为什么它不起作用。问题是: 二叉搜索树 (BST) 是一种二叉树,其中每个值节点大于或等于该节点的所有节点中的值左子树并且小于该树中所有节点
我有一个字符串,其中包含“Dollars”和“Cents”符号。我想删除它们。我试过了 string.replaceAll("[\"\\u00A2\" $]", "") 但它不起作用。正确的做法是什么
我在 stories 和 tags 之间有一个多对多的关系,为保存关系而创建的表是 taxonomies。我想搜索所有具有所有给定标签的故事。 到目前为止我使用的查询是这个,当然它对我不起作用,它返回
我是一名优秀的程序员,十分优秀!