我想解决的实际问题是,我想自动找出窗口周围边距的大小.如果你能找到更好的方法,请一定要回答而不是这个.
为此,我决定拍摄测试窗口的屏幕截图并测量边距.这很简单,因为我预计没有任何边缘会变成鲜艳的粉红色,但我承认这是一个黑客.我使用GetWindowRect(py)获取边界框,并使用PIL抓取屏幕截图并裁剪到边界框.问题是当裁剪操作正确时,边界框不准确.Windows 7"截图工具"获得正确的尺寸.我怎么能这样做?
我设计了一个Windows窗体对话框,应该可以在其他应用程序,WPF和Windows窗体中重用.当我在Windows窗体应用程序中使用它时,这可以正常工作,但在WPF应用程序中调用时会导致一些布局问题.从屏幕上的像素,WinForms API所说的以及Spy ++中测量的尺寸和大小是不一致的.窗口宽10像素,在没有调试器的情况下运行比Spy ++说的更高,并且比我说它应该是.这有什么问题?我找不到任何东西,只是说它是一个严重破坏的.NET Framework.
这是Form类代码:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DialogTestApp
{
internal class MyDialog : Form
{
public MyDialog()
{
Text = "Title";
Width = 500; // -> actually 510 (Spy++ says 500)
Height = 300; // -> actually 310 (Spy++ says 300)
Font = SystemFonts.MessageBoxFont;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
TableLayoutPanel mainLayout = new TableLayoutPanel();
mainLayout.BackColor = Color.FromArgb(255, 171, 255); // pink
mainLayout.Dock = DockStyle.Fill; …Run Code Online (Sandbox Code Playgroud) 我想将表单放置在屏幕的左上角。
我已经尝试过this.Location = new Point(0,0),但窗口位于 (7,0) - 窗口顶部位于屏幕顶部,但左侧距离屏幕边缘 7 个像素。我创建了新的 WinForms 应用程序进行测试,并仅添加了以下代码:
private void Form1_Load(object sender, EventArgs e)
{
Point p = new Point(0, 0);
WindowState = FormWindowState.Maximized;
Debug.WriteLine("\nMaximized");
Debug.WriteLine("Location: " + Location);
Debug.WriteLine("Size: " + Size);
Debug.WriteLine("PointToScreen(0,0): " + PointToScreen(p));
WindowState = FormWindowState.Normal;
Location = p;
Debug.WriteLine("\nNormal");
Debug.WriteLine("Location: " + Location);
Debug.WriteLine("Size: " + Size);
Debug.WriteLine("PointToScreen(0,0): " + PointToScreen(p));
Debug.Write("\nScreen.PrimaryScreen.WorkingArea: ");
Debug.WriteLine(Screen.PrimaryScreen.WorkingArea);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Maximized
Location: {X=-8,Y=-8}
Size: {Width=1936, Height=1056}
PointToScreen(0,0): {X=0,Y=23}
Normal
Location: {X=0,Y=0}
Size: {Width=300, …Run Code Online (Sandbox Code Playgroud)