@jan_tappenbeck wrote:
hi!
is it possible to close the xref-Panel by code and how?
regards Jan
If the XREF palette is floating, the example code below should close it. However, if it is docked, the problem is not so simple.
You can convert it to VB here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using System.Runtime.InteropServices;
using WinApi.Windows;
namespace CloseXRefPaletteExample
{
public static class Commands
{
const int WM_CLOSE = 16;
[CommandMethod("XREFCLOSE", CommandFlags.Session)]
public static void XrefCloseCommand()
{
IntPtr handle = WindowFinder.FindThreadWindow(null, "External References");
if(handle != IntPtr.Zero)
NativeMethods.PostMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
namespace WinApi.Windows
{
public class WindowFinder: WindowEnumerator
{
string classname;
string text;
IntPtr result = IntPtr.Zero;
public WindowFinder(string classname, string text)
{
if(classname == null && text == null)
throw new ArgumentException("Requires classname or text to be non-null");
this.text = text;
this.classname = classname;
}
protected override bool OnWindow(IntPtr handle)
{
if(!string.IsNullOrEmpty(classname))
{
if(string.Equals(classname, GetWindowClass(handle), StringComparison.CurrentCultureIgnoreCase))
{
if(!string.IsNullOrEmpty(text))
{
if(string.Equals(text, GetWindowText(handle), StringComparison.CurrentCultureIgnoreCase))
{
result = handle;
return false;
}
}
}
}
else if(!string.IsNullOrEmpty(text))
{
if(string.Equals(text, GetWindowText(handle), StringComparison.CurrentCultureIgnoreCase))
{
result = handle;
return false;
}
}
return true;
}
public static IntPtr FindWindow(string classname, string text = null)
{
WindowFinder finder = new WindowFinder(classname, text);
finder.EnumWindows();
return finder.result;
}
public static IntPtr FindThreadWindow(string classname, string text = null)
{
WindowFinder finder = new WindowFinder(classname, text);
finder.EnumThreadWindows();
return finder.result;
}
}
public class WindowEnumerator
{
Func<IntPtr, bool> func = null;
NativeMethods.EnumThreadWindowsCallback callback;
protected WindowEnumerator()
{
this.callback = this.EnumWindowsCallback;
}
public WindowEnumerator(Func<IntPtr, bool> func)
: this()
{
if(func == null)
throw new ArgumentNullException("func");
this.func = func;
}
protected virtual bool OnWindow(IntPtr handle)
{
return this.func(handle);
}
bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
{
return this.OnWindow(handle);
}
public static void EnumWindows(Func<IntPtr, bool> func)
{
WindowEnumerator enumerator = new WindowEnumerator(func);
enumerator.EnumWindows();
}
public void EnumWindows()
{
NativeMethods.EnumWindows(callback, IntPtr.Zero);
}
public void EnumThreadWindows()
{
NativeMethods.EnumThreadWindows(NativeMethods.GetCurrentThreadId(), callback, NativeMethods.NullHandleRef);
}
protected static string GetWindowClass(IntPtr hwnd)
{
StringBuilder sb = new StringBuilder(512);
NativeMethods.GetClassName(hwnd, sb, 512);
return sb.ToString();
}
protected static string GetWindowText(IntPtr hwnd)
{
StringBuilder sb = new StringBuilder(512);
NativeMethods.GetWindowText(hwnd, sb, 512);
return sb.ToString();
}
}
[System.Security.SuppressUnmanagedCodeSecurity()]
internal class NativeMethods
{
internal static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
internal delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern bool EnumThreadWindows(int dwThreadId, EnumThreadWindowsCallback lpfn, HandleRef lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern int GetCurrentThreadId();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern bool IsWindowVisible(HandleRef hWnd);
[DllImport("User32.dll")]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("User32.dll")]
internal static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
internal static extern bool PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}
}