-
- using System;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
- namespace 取得CAD绘图区域屏幕坐标
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- [DllImport("user32.DLL", EntryPoint = "GetTopWindow", SetLastError = true, CharSet = CharSet.Unicode,
- ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern IntPtr GetTopWindow(IntPtr hWnd);
- [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- public static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- 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 button1_Click(object sender, EventArgs e)
- {
- Rect rect = new Rect();
- IntPtr intPtr1 = GetTopWindow(Application.DocumentManager.MdiActiveDocument.Window.Handle);
- textBox1.Text = intPtr1.ToString();
- IntPtr intPtr2 = GetWindow(intPtr1, 2);
- GetWindowRect(intPtr2, ref rect);
- textBox2.Text = rect.Left.ToString();
- textBox3.Text = rect.Top.ToString();
- textBox4.Text = (rect.Right - rect.Left).ToString();
- textBox5.Text = (rect.Bottom - rect.Top).ToString();
- }
- }
- }
gfukalfwv1v.PNG
|