Wil*_*eer 1 .net c# wpf pinvoke
我创建了一个小样本应用程序来说明我在使用时遇到的问题GetWindowRect.当我单击Foo按钮时,它显示Left返回的值GetWindowRect不同于Window.Left.
看来返回的值Window.Left是相对的,但我不确定是什么.同样奇怪的是,我不能在每台机器上重现这一点,在我的家用笔记本电脑上有或没有额外的显示器我可以重现这个问题,在我的工作电脑上问题不会发生.两个系统都运行Windows 7
为什么这些值不同,我该如何解决这个问题呢?
MainWindow.xaml.cs
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace PositionTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IntPtr thisHandle;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
thisHandle = new WindowInteropHelper(this).Handle;
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private void ReportLocation(object sender, RoutedEventArgs e)
{
var rct = new RECT();
GetWindowRect(thisHandle, ref rct);
MessageBox.Show(Left.ToString() + " " + rct.Left);
}
}
}
Run Code Online (Sandbox Code Playgroud)
MainWindow.xaml
<Window x:Class="PositionTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Loaded="Window_Loaded" WindowStartupLocation="Manual" Height="150" Width="150" WindowStyle="SingleBorderWindow" ResizeMode="NoResize" BorderThickness="0,1,0,0" >
<Grid>
<Button Content="Foo" HorizontalAlignment="Left" Margin="35,50,0,0" VerticalAlignment="Top" Width="75" Click="ReportLocation"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1434 次 |
| 最近记录: |