获取ArcMap窗口句柄,通过WinAPI获取工作空间中点击要素的系统桌面坐标实现窗体跟随

这里用了个“桌面坐标”,是希望区分一下ArcGis的“屏幕坐标”(与之对应的还有一个“地理坐标”)。

什么是“屏幕坐标”呢?使用ITool接口的OnMouseDown方法获取的“x,y”值即是,其原点值在“工作空间”窗体左上角。

什么是我所称谓的“工作空间”呢?它是数据编辑区域,用Spy++查看一下其窗体的classname是“AfxFrameOrView90u”,这样看称作“工作框”或者“工作视图”什么的似乎更准确~

获取ArcMap窗口句柄,通过WinAPI获取工作空间中点击要素的系统桌面坐标实现窗体跟随获取ArcMap窗口句柄,通过WinAPI获取工作空间中点击要素的系统桌面坐标实现窗体跟随

前面已经提到可以通过OnMouseDown方法获取鼠标在AfxFrameOrView90u窗口相对于其左上角的坐标值(鼠标点击位置的“屏幕坐标”),而通过IActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint()可以将地理坐标转为屏幕坐标,那么获取AfxFrameOrView90u窗口左上角的系统桌面坐标加上“屏幕坐标值”就可以获取鼠标点击位置或者工作框中要素的桌面坐标。

知道桌面坐标就可以做一些窗体跟随,桌面绘图等的效果了。

使用GetClassName通过句柄获取当前ArcMap窗口的ClassName,然后查找子窗口获得AfxFrameOrView90u就可以下一步操作了,主要代码如下:
API部分:

 class WinAPI
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr Hwnd, ref RECT lpRect);//获取window的矩形坐标
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
} 窗体跟随鼠标,可以实现通过对窗体选项内容的点选等操作,将信息写入当前选中的要素,节省人工属性表输入。
也可以实现辅助编号的操作,参考ArcGis辅助编号(半自动)功能的插件式实现
上一篇:Java实现邮箱发送


下一篇: sublime text3快速生成html头部信息(转)