//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System;
using System.Runtime.InteropServices;
[assembly
: CommandClass
(typeof(BlackBox
.AutoCAD.TitleBarText.Commands))]
// Inspired by:
// http://www.theswamp.org/index.php?topic=46380.0
// http://adndevblog.typepad.com/autocad/2013/01/changing-the-displayed-autocad-title-bar-text.html?cid=6a0167607c2431970b01a73d7aa899970d
namespace BlackBox.AutoCAD.TitleBarText
{
public class Commands
{
//private string progID = "AutoCAD.Application.18"; // 2010
private string progID = "AutoCAD.Application.19"; // 2013
private string curTitle = "";
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern long GetActiveWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern long GetWindowText(long hWnd, string lpString, long nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern long SetWindowText(long hwnd, String lpString);
[DllImport("user32.dll", EntryPoint = "FindWindowA", SetLastError = true)]
static extern long FindWindow(string ZeroOnly, string lpWindowName);
[CommandMethod("TitleBarText")]
public void TitleBarText()
{
Document doc = acApp.DocumentManager.MdiActiveDocument;
try
{
Type acType = Type.GetTypeFromProgID(progID);
AcadApplication acAppCom =
acApp.AcadApplication as AcadApplication;
if (acAppCom == null)
return;
acAppCom.Visible = true;
System.Threading.Thread.Sleep(2000);
long acadhnd = 0;
string title
= new string(' ',
256); acadhnd = GetActiveWindow();
acadhnd = FindWindow(null, acAppCom.Caption);
GetWindowText(acadhnd, title, 125);
if (curTitle == "")
curTitle = title;
doc.Editor.WriteMessage("\nCurrent Title Bar Text: {0}\n", title);
PromptStringOptions pso =
new PromptStringOptions
("\nEnter Title Bar Text: "); pso.AllowSpaces = true;
PromptResult pr = doc.Editor.GetString(pso);
if (pr.Status != PromptStatus.OK)
return;
SetWindowText(acadhnd, pr.StringResult);
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage("\n; error: System.Exception: {0}\n", ex.Message);
}
}
[LispFunction("vla-SetTitleBarText")]
public bool TitleBarText(ResultBuffer resbuf)
{
Document doc = acApp.DocumentManager.MdiActiveDocument;
try
{
if (resbuf == null)
throw new TooFewArgsException
();
TypedValue[] args = resbuf.AsArray();
int i = args.Length;
if (args.Length > 1)
throw new TooManyArgsException
();
if (args[0].TypeCode != (int)LispDataType.Text)
throw new ArgumentTypeException
("stringp", args
[0]);
Type acType = Type.GetTypeFromProgID(progID);
AcadApplication acAppCom =
acApp.AcadApplication as AcadApplication;
if (acAppCom == null)
return false;
acAppCom.Visible = true;
System.Threading.Thread.Sleep(2000);
long acadhnd = 0;
string title
= new string(' ',
256); acadhnd = GetActiveWindow();
acadhnd = FindWindow(null, acAppCom.Caption);
GetWindowText(acadhnd, title, 125);
if (curTitle == "")
curTitle = title;
SetWindowText(acadhnd, (string)args[0].Value);
return true;
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage("\n; error: System.Exception: {0}\n", ex.Message);
return false;
}
}
}
/// <summary>
/// Special thanks to Gile for his LispException classes:
/// </summary>
class LispException : System.Exception
{
public LispException(string msg) : base(msg) { }
}
class TooFewArgsException : LispException
{
public TooFewArgsException() : base("too few arguments") { }
}
class TooManyArgsException : LispException
{
public TooManyArgsException() : base("too many arguments") { }
}
class ArgumentTypeException : LispException
{
public ArgumentTypeException(string s, TypedValue tv)
: base(string.Format(
"invalid argument type: {0}: {1}",
s, tv.TypeCode == (int)LispDataType.Nil ? "nil" : tv.Value))
{ }
}
}