|
[code]// Add reference to UIAutomationClient.dll
// and UIAutomationTypes.dll
using System.Windows.Automation;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
// Based on http://www.pinvoke.net
// /default.aspx/user32/SendInput.html
// /default.aspx/Structures/MOUSEINPUT.html
// /default.aspx/user32/mouse_event.html
[DllImport("user32.dll" , SetLastError = true )]
static extern uint SendInput(uint nInputs,
INPUT[] pInputs, int cbSize);
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct INPUT
{
public int type;
public MOUSEINPUT mi;
public INPUT(uint flag)
{
type = 0; // Mouse input
mi.dx = 0;
mi.dy = 0;
mi.mouseData = 0;
mi.time = 0;
mi.dwExtraInfo = IntPtr.Zero;
mi.dwFlags = flag;
}
}
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
[CommandMethod("XRefPalRefresh" )]
public void XRefPalRefresh()
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
try
{
System.Diagnostics.Process p
= Process.GetCurrentProcess();
AutomationElement acadAutoElem =
AutomationElement.RootElement.FindFirst(
TreeScope.Children,
new PropertyCondition(
AutomationElement.ProcessIdProperty, p.Id));
// Control Id retreived for the Refresh button
// in AutoCAD 2016 using Spy++
string btnRefreshhexID = "000075FB" ;
string btnRefreshdecimalID =
System.Convert.ToInt32(btnRefreshhexID, 16)
.ToString();
AutomationElement refreshBtnAutoElem
= acadAutoElem.FindFirst(
TreeScope.Descendants,
new PropertyCondition(
AutomationElement.AutomationIdProperty,
btnRefreshdecimalID));
if (refreshBtnAutoElem == null)
{
ed.WriteMessage("Refresh button in
External References
palette was not identified !");
return ;
}
// Using UI's Invoke pattern
// Does work for simple buttons but not for
// the Refresh button in
//'s external references palette.
//InvokePattern ipClickRefreshBtn =
// (InvokePattern)refreshBtnAutoElem.GetCurrentPattern
// (InvokePattern.Pattern);
//ipClickRefreshBtn.Invoke();
//'s resort to clicking by location.
System.Windows.Point point
= refreshBtnAutoElem.GetClickablePoint();
System.Windows.Forms.Cursor.Position
= new System.Drawing.Point(
(int )point.X, (int )point.Y);
INPUT input1 = new INPUT(MOUSEEVENTF_LEFTDOWN);
INPUT input2 = new INPUT(MOUSEEVENTF_LEFTUP);
SendInput(2, new [] { input1, input2 },
Marshal.SizeOf(typeof(INPUT)));
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
https://adndevblog.typepad.com/autocad/balaji-ramamoorthy/page/2/[/code] |
|