在Asp.net获取经纬度的经纬度?(Get latitude and longitude of address in Asp.net?)
如何在asp.net MVC4中获取某个位置的经纬度?
例如,我如何获得哈菲兹中心,拉合尔,旁遮普邦,巴基斯坦的经度和纬度?
How can I get a latitude and longitude of some location in asp.net MVC4?
For example how can I get the latitude and longitude of Hafeez center, Lahore, Punjab, Pakistan?
原文:https://stackoverflow.com/questions/21112397
最满意答案
以下是一些使用.Net Automation库将所有文本复制到剪贴板的代码。
启动一个新的
WinForms
项目,然后添加以下引用:
- WindowsBase
- UIAutomationTypes
- UIAutomationClient
- System.Xaml
- PresentationCore
- PresentationFramework
- 系统管理
该代码还介绍了如何在Visual Studio中设置菜单项以将内容复制到剪贴板。
编辑: UI自动化只返回可见的树形视图项目。 因此,要复制所有项目,将查找符号结果窗口设置为前景,然后发送
{PGDN}
,并复制下一批项目。 重复此过程直到找不到新项目。 最好是使用ScrollPattern
,但是当试图设置滚动时它会抛出Exception
。编辑2:试图通过在单独的线程上运行来改进AutomationElement FindAll的性能。 在某些情况下似乎很慢。
编辑3:通过使TreeView窗口非常大来提高性能。 可以在大约10秒钟内复制约400件物品。
编辑4:处理实现
IDisposable
对象。 更好的消息报告。 更好地处理流程参数。 把窗户放回原来的大小。using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Management; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Automation; using System.Windows.Forms; namespace CopyFindSymbolResults { // This program tries to find the 'Find Symbol Results' window in visual studio // and copy all the text to the clipboard. // // The Find Symbol Results window uses a TreeView control that has the class name 'LiteTreeView32' // In the future if this changes, then it's possible to pass in the class name as the first argument. // Use TOOLS -> Spy++ to determine the class name. // // After compiling this code into an Exe, add a menu item (TOOLS -> Copy Find Symbol Results) in Visual Studio by: // 1) TOOLS -> External Tools... // (Note: in the 'Menu contents:' list, count which item the new item is, starting at base-1). // Title: Copy Find Symbol Results // Command: C:\<Path>\CopyFindSymbolResults.exe (e.g. C:\Windows\ is one option) // 2) TOOLS -> Customize... -> Keyboard... (button) // Show Commands Containing: tools.externalcommand // Then select the n'th one, where n is the count from step 1). // static class Program { enum Tabify { No = 0, Yes = 1, Prompt = 2, } [STAThread] static void Main(String[] args) { String className = "LiteTreeView32"; Tabify tabify = Tabify.Prompt; if (args.Length > 0) { String arg0 = args[0].Trim(); if (arg0.Length > 0) className = arg0; if (args.Length > 1) { int x = 0; if (int.TryParse(args[1], out x)) tabify = (Tabify) x; } } DateTime startTime = DateTime.Now; Data data = new Data() { className = className }; Thread t = new Thread((o) => { GetText((Data) o); }); t.IsBackground = true; t.Start(data); lock(data) { Monitor.Wait(data); } if (data.p == null || data.p.MainWindowHandle == IntPtr.Zero) { System.Windows.Forms.MessageBox.Show("Cannot find Microsoft Visual Studio process."); return; } try { SimpleWindow owner = new SimpleWindow { Handle = data.MainWindowHandle }; if (data.appRoot == null) { System.Windows.Forms.MessageBox.Show(owner, "Cannot find AutomationElement from process MainWindowHandle: " + data.MainWindowHandle); return; } if (data.treeViewNotFound) { System.Windows.Forms.MessageBox.Show(owner, "AutomationElement cannot find the tree view window with class name: " + data.className); return; } String text = data.text; if (text.Length == 0) { // otherwise Clipboard.SetText throws exception System.Windows.Forms.MessageBox.Show(owner, "No text was found: " + data.p.MainWindowTitle); return; } TimeSpan ts = DateTime.Now - startTime; if (tabify == Tabify.Prompt) { var dr = System.Windows.Forms.MessageBox.Show(owner, "Replace dashes and colons for easy pasting into Excel?", "Tabify", System.Windows.Forms.MessageBoxButtons.YesNo); if (dr == System.Windows.Forms.DialogResult.Yes) tabify = Tabify.Yes; ts = TimeSpan.Zero; // prevent second prompt } if (tabify == Tabify.Yes) { text = text.Replace(" - ", "\t"); text = text.Replace(" : ", "\t"); } System.Windows.Forms.Clipboard.SetText(text); String msg = "Data is ready on the clipboard."; var icon = System.Windows.Forms.MessageBoxIcon.None; if (data.lines != data.count) { msg = String.Format("Only {0} of {1} rows copied.", data.lines, data.count); icon = System.Windows.Forms.MessageBoxIcon.Error; } if (ts.TotalSeconds > 4 || data.lines != data.count) System.Windows.Forms.MessageBox.Show(owner, msg, "", System.Windows.Forms.MessageBoxButtons.OK, icon); } finally { data.p.Dispose(); } } private class SimpleWindow : System.Windows.Forms.IWin32Window { public IntPtr Handle { get; set; } } private const int TVM_GETCOUNT = 0x1100 + 5; [DllImport("user32.dll")] static extern int SendMessage(IntPtr hWnd, int msg, int wparam, int lparam); [DllImport("user32.dll", SetLastError = true)] static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint); private class Data { public int lines = 0; public int count = 0; public IntPtr MainWindowHandle = IntPtr.Zero; public IntPtr TreeViewHandle = IntPtr.Zero; public Process p; public AutomationElement appRoot = null; public String text = null; public String className = null; public bool treeViewNotFound = false; } private static void GetText(Data data) { Process p = GetParentProcess(); data.p = p; if (p == null || p.MainWindowHandle == IntPtr.Zero) { data.text = ""; lock(data) { Monitor.Pulse(data); } return; } data.MainWindowHandle = p.MainWindowHandle; AutomationElement appRoot = AutomationElement.FromHandle(p.MainWindowHandle); data.appRoot = appRoot; if (appRoot == null) { data.text = ""; lock(data) { Monitor.Pulse(data); } return; } AutomationElement treeView = appRoot.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, data.className)); if (treeView == null) { data.text = ""; data.treeViewNotFound = true; lock(data) { Monitor.Pulse(data); } return; } data.TreeViewHandle = new IntPtr(treeView.Current.NativeWindowHandle); data.count = SendMessage(data.TreeViewHandle, TVM_GETCOUNT, 0, 0); RECT rect = new RECT(); GetWindowRect(data.TreeViewHandle, out rect); // making the window really large makes it so less calls to FindAll are required MoveWindow(data.TreeViewHandle, 0, 0, 800, 32767, false); int TV_FIRST = 0x1100; int TVM_SELECTITEM = (TV_FIRST + 11); int TVGN_CARET = TVGN_CARET = 0x9; // if a vertical scrollbar is detected, then scroll to the top sending a TVM_SELECTITEM command var vbar = treeView.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "Vertical Scroll Bar")); if (vbar != null) { SendMessage(data.TreeViewHandle, TVM_SELECTITEM, TVGN_CARET, 0); // select the first item } StringBuilder sb = new StringBuilder(); Hashtable ht = new Hashtable(); int chunk = 0; while (true) { bool foundNew = false; AutomationElementCollection treeViewItems = treeView.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TreeItem)); if (treeViewItems.Count == 0) break; if (ht.Count == 0) { chunk = treeViewItems.Count - 1; } foreach (AutomationElement ele in treeViewItems) { try { String n = ele.Current.Name; if (!ht.ContainsKey(n)) { ht[n] = n; foundNew = true; data.lines++; sb.AppendLine(n); } } catch {} } if (!foundNew || data.lines == data.count) break; int x = Math.Min(data.count-1, data.lines + chunk); SendMessage(data.TreeViewHandle, TVM_SELECTITEM, TVGN_CARET, x); } data.text = sb.ToString(); MoveWindow(data.TreeViewHandle, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, false); lock(data) { Monitor.Pulse(data); } } // this program expects to be launched from Visual Studio // alternative approach is to look for "Microsoft Visual Studio" in main window title // but there could be multiple instances running. private static Process GetParentProcess() { // from thread: http://stackoverflow.com/questions/2531837/how-can-i-get-the-pid-of-the-parent-process-of-my-application int myId = 0; using (Process current = Process.GetCurrentProcess()) myId = current.Id; String query = String.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId); using (var search = new ManagementObjectSearcher("root\\CIMV2", query)) { using (ManagementObjectCollection list = search.Get()) { using (ManagementObjectCollection.ManagementObjectEnumerator results = list.GetEnumerator()) { if (!results.MoveNext()) return null; using (var queryObj = results.Current) { uint parentId = (uint) queryObj["ParentProcessId"]; return Process.GetProcessById((int) parentId); } } } } } [DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] private struct RECT { public int Left; public int Top; public int Right; public int Bottom; } } }
Here is some code that uses the .Net Automation library to copy all the text to the clipboard.
Start a new
WinForms
project and then add the following references:
- WindowsBase
- UIAutomationTypes
- UIAutomationClient
- System.Xaml
- PresentationCore
- PresentationFramework
- System.Management
The code also explains how to setup a menu item in visual studio to copy the contents to the clipboard.
Edit: The UI Automation only returns visible tree view items. Thus, to copy all the items, the find symbol results window is set as foreground, and then a
{PGDN}
is sent, and the next batch of items is copied. This process is repeated until no new items are found. It would have been preferable to use theScrollPattern
, however it threw anException
when trying to set the scroll.Edit 2: Tried to improve the performance of AutomationElement FindAll by running on a separate thread. Seems to be slow in some cases.
Edit 3: Improved performance by making the TreeView window very large. Can copy about 400 items in about 10 seconds.
Edit 4: Dispose objects implementing
IDisposable
. Better message reporting. Better handling of process args. Put window back to its original size.using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Management; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Automation; using System.Windows.Forms; namespace CopyFindSymbolResults { // This program tries to find the 'Find Symbol Results' window in visual studio // and copy all the text to the clipboard. // // The Find Symbol Results window uses a TreeView control that has the class name 'LiteTreeView32' // In the future if this changes, then it's possible to pass in the class name as the first argument. // Use TOOLS -> Spy++ to determine the class name. // // After compiling this code into an Exe, add a menu item (TOOLS -> Copy Find Symbol Results) in Visual Studio by: // 1) TOOLS -> External Tools... // (Note: in the 'Menu contents:' list, count which item the new item is, starting at base-1). // Title: Copy Find Symbol Results // Command: C:\<Path>\CopyFindSymbolResults.exe (e.g. C:\Windows\ is one option) // 2) TOOLS -> Customize... -> Keyboard... (button) // Show Commands Containing: tools.externalcommand // Then select the n'th one, where n is the count from step 1). // static class Program { enum Tabify { No = 0, Yes = 1, Prompt = 2, } [STAThread] static void Main(String[] args) { String className = "LiteTreeView32"; Tabify tabify = Tabify.Prompt; if (args.Length > 0) { String arg0 = args[0].Trim(); if (arg0.Length > 0) className = arg0; if (args.Length > 1) { int x = 0; if (int.TryParse(args[1], out x)) tabify = (Tabify) x; } } DateTime startTime = DateTime.Now; Data data = new Data() { className = className }; Thread t = new Thread((o) => { GetText((Data) o); }); t.IsBackground = true; t.Start(data); lock(data) { Monitor.Wait(data); } if (data.p == null || data.p.MainWindowHandle == IntPtr.Zero) { System.Windows.Forms.MessageBox.Show("Cannot find Microsoft Visual Studio process."); return; } try { SimpleWindow owner = new SimpleWindow { Handle = data.MainWindowHandle }; if (data.appRoot == null) { System.Windows.Forms.MessageBox.Show(owner, "Cannot find AutomationElement from process MainWindowHandle: " + data.MainWindowHandle); return; } if (data.treeViewNotFound) { System.Windows.Forms.MessageBox.Show(owner, "AutomationElement cannot find the tree view window with class name: " + data.className); return; } String text = data.text; if (text.Length == 0) { // otherwise Clipboard.SetText throws exception System.Windows.Forms.MessageBox.Show(owner, "No text was found: " + data.p.MainWindowTitle); return; } TimeSpan ts = DateTime.Now - startTime; if (tabify == Tabify.Prompt) { var dr = System.Windows.Forms.MessageBox.Show(owner, "Replace dashes and colons for easy pasting into Excel?", "Tabify", System.Windows.Forms.MessageBoxButtons.YesNo); if (dr == System.Windows.Forms.DialogResult.Yes) tabify = Tabify.Yes; ts = TimeSpan.Zero; // prevent second prompt } if (tabify == Tabify.Yes) { text = text.Replace(" - ", "\t"); text = text.Replace(" : ", "\t"); } System.Windows.Forms.Clipboard.SetText(text); String msg = "Data is ready on the clipboard."; var icon = System.Windows.Forms.MessageBoxIcon.None; if (data.lines != data.count) { msg = String.Format("Only {0} of {1} rows copied.", data.lines, data.count); icon = System.Windows.Forms.MessageBoxIcon.Error; } if (ts.TotalSeconds > 4 || data.lines != data.count) System.Windows.Forms.MessageBox.Show(owner, msg, "", System.Windows.Forms.MessageBoxButtons.OK, icon); } finally { data.p.Dispose(); } } private class SimpleWindow : System.Windows.Forms.IWin32Window { public IntPtr Handle { get; set; } } private const int TVM_GETCOUNT = 0x1100 + 5; [DllImport("user32.dll")] static extern int SendMessage(IntPtr hWnd, int msg, int wparam, int lparam); [DllImport("user32.dll", SetLastError = true)] static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint); private class Data { public int lines = 0; public int count = 0; public IntPtr MainWindowHandle = IntPtr.Zero; public IntPtr TreeViewHandle = IntPtr.Zero; public Process p; public AutomationElement appRoot = null; public String text = null; public String className = null; public bool treeViewNotFound = false; } private static void GetText(Data data) { Process p = GetParentProcess(); data.p = p; if (p == null || p.MainWindowHandle == IntPtr.Zero) { data.text = ""; lock(data) { Monitor.Pulse(data); } return; } data.MainWindowHandle = p.MainWindowHandle; AutomationElement appRoot = AutomationElement.FromHandle(p.MainWindowHandle); data.appRoot = appRoot; if (appRoot == null) { data.text = ""; lock(data) { Monitor.Pulse(data); } return; } AutomationElement treeView = appRoot.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, data.className)); if (treeView == null) { data.text = ""; data.treeViewNotFound = true; lock(data) { Monitor.Pulse(data); } return; } data.TreeViewHandle = new IntPtr(treeView.Current.NativeWindowHandle); data.count = SendMessage(data.TreeViewHandle, TVM_GETCOUNT, 0, 0); RECT rect = new RECT(); GetWindowRect(data.TreeViewHandle, out rect); // making the window really large makes it so less calls to FindAll are required MoveWindow(data.TreeViewHandle, 0, 0, 800, 32767, false); int TV_FIRST = 0x1100; int TVM_SELECTITEM = (TV_FIRST + 11); int TVGN_CARET = TVGN_CARET = 0x9; // if a vertical scrollbar is detected, then scroll to the top sending a TVM_SELECTITEM command var vbar = treeView.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "Vertical Scroll Bar")); if (vbar != null) { SendMessage(data.TreeViewHandle, TVM_SELECTITEM, TVGN_CARET, 0); // select the first item } StringBuilder sb = new StringBuilder(); Hashtable ht = new Hashtable(); int chunk = 0; while (true) { bool foundNew = false; AutomationElementCollection treeViewItems = treeView.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TreeItem)); if (treeViewItems.Count == 0) break; if (ht.Count == 0) { chunk = treeViewItems.Count - 1; } foreach (AutomationElement ele in treeViewItems) { try { String n = ele.Current.Name; if (!ht.ContainsKey(n)) { ht[n] = n; foundNew = true; data.lines++; sb.AppendLine(n); } } catch {} } if (!foundNew || data.lines == data.count) break; int x = Math.Min(data.count-1, data.lines + chunk); SendMessage(data.TreeViewHandle, TVM_SELECTITEM, TVGN_CARET, x); } data.text = sb.ToString(); MoveWindow(data.TreeViewHandle, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, false); lock(data) { Monitor.Pulse(data); } } // this program expects to be launched from Visual Studio // alternative approach is to look for "Microsoft Visual Studio" in main window title // but there could be multiple instances running. private static Process GetParentProcess() { // from thread: http://stackoverflow.com/questions/2531837/how-can-i-get-the-pid-of-the-parent-process-of-my-application int myId = 0; using (Process current = Process.GetCurrentProcess()) myId = current.Id; String query = String.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId); using (var search = new ManagementObjectSearcher("root\\CIMV2", query)) { using (ManagementObjectCollection list = search.Get()) { using (ManagementObjectCollection.ManagementObjectEnumerator results = list.GetEnumerator()) { if (!results.MoveNext()) return null; using (var queryObj = results.Current) { uint parentId = (uint) queryObj["ParentProcessId"]; return Process.GetProcessById((int) parentId); } } } } } [DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] private struct RECT { public int Left; public int Top; public int Right; public int Bottom; } } }
相关问答
更多-
以下是一些使用.Net Automation库将所有文本复制到剪贴板的代码。 启动一个新的WinForms项目,然后添加以下引用: WindowsBase UIAutomationTypes UIAutomationClient System.Xaml PresentationCore PresentationFramework 系统管理 该代码还介绍了如何在Visual Studio中设置菜单项以将内容复制到剪贴板。 编辑: UI自动化只返回可见的树形视图项目。 因此,要复制所有项目,将查找符号结果窗口设 ...
-
使用'在文件中查找'(ctrl + shift + f)而不是'Find'(ctrl + f) 编辑:正如Jay在评论中提到的那样,如果您使用对话框左上角的选择器将“快速查找”更改为“在文件中查找”,则也可以通过“查找”对话框进入该对话框。 Use 'Find in Files' (ctrl+shift+f) instead of 'Find' (ctrl+f) edit: As Jay mentions in comments, you can also get there via the 'Find' ...
-
要立即返回窗口,您可以进入Debug --> Windows --> Immediate或者您可以使用Ctrl Alt 我可以使用它来恢复。 To get the immediate window back you can go to Debug --> Windows --> Immediate or you can use CtrlAltI either will work to get it back.
-
要使用键盘选择多行(从当前行开始,向下选择): 按HOME(将光标移动到当前行的第一个字符) 如果光标不是一直向左(即Col 1),请再次按HOME 按住SHIFT键的同时按向下箭头(每个后续的向下箭头将选择一个额外的行) 或者,向上箭头可用于从起始行中选择行。 附加信息 :SHIFT,CTRL和所有ARROWS的组合可用于各种光标移动和字符选择,例如: CTRL +向左(将光标移动到当前单词的开头) CTRL + Right(将光标移动到下一个单词的开头) SHIFT + LEFT(选择前一个字符) CT ...
-
您发现哪个Visual Studio命令窗口命令最有用?(Which Visual Studio Command Window commands to you find most useful?)[2023-05-19]
- 打开文件。 其自动完成功能使其在包含许多文件的解决方案中具有无可估量的价值。 of - open file. Its autocomplete feature makes it invaluable in solutions with many files. -
我不知道在Visual Studio 2008 UI中有什么办法(我甚至检查过2010年 - 什么都没有)。 但是,有一种方法可以使用注册表对此进行更改。 以下链接应该有所帮助。 它基于Visual Studio 2005,它也适用于Visual Studio 2008。 只需更改注册表位置即可。 来自:HKEY_CURRENT_USER \ Software \ Microsoft \ VisualStudio \ 8.0 \ Find 要:HKEY_CURRENT_USER \ Software \ M ...
-
您是否安装了Power Productivity Tools扩展程序? 如果是这样,可能是突出显示当前行扩展导致问题。 由于某种原因,“查找结果”窗格不符合“当前行(扩展名)和当前行不活动(扩展名)”设置的“字体和颜色”对话框中的选择。 我找到解决问题的唯一方法是关闭该扩展程序。 工具 - >选项...->生产率电动工具 - >所有扩展 - >突出显示当前行 - >关闭。 当然,这样做会消除代码编辑器窗口中扩展的好处。 好像是个bug。 I ended up changing the color of Pl ...
-
在Visual Studio 2015中无法找到我的“在文件中查找”窗口(Can't find my “Find in Files” window in Visual Studio 2015)[2022-04-06]
Window->Reset Window Layout应恢复为默认布局。 如果您以前导出了您的设置(在丢失“在文件中查找”窗口之前),您也可以重新导入这些设置。 这是在工具菜单下找到的。 如果需要,您可以清除“所有设置”,然后选择General Settings->Window Layouts仅限General Settings->Window Layouts 。 Window->Reset Window Layout should revert to the default layout. If you ... -
我相信您的VS 2010 SDK未安装(或者它已损坏并需要重新安装)。 安装SDK后,模板将立即显示在[ProgramFiles]\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\ ,并可以使用。 有关详细信息,请参阅此页面。 I believe your VS 2010 SDK is not installed (or it is broken and requires re-installing). As soon as the SDK ...
-
看看~/Library/Application Support/Code/User/workspaceStorage 应该有一堆带有随机文件名的文件夹。 在我的linux和Mac设置中,我在这些文件夹中找到的唯一东西是一些名为ms-vscode.cpptools子文件夹,但没有别的。 因此,当我需要清除符号数据库时,我只删除workspaceStorage文件夹。 您应该检查其他扩展中没有其他内容 - 如果有,则可以尝试删除单个ms-vscode.cpptools文件夹。 Have a look in ~/ ...