<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: vb.net - close xref-panel by code in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7484203#M29190</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3357387"&gt;@jan_tappenbeck&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is it possible to close the xref-Panel by code and how?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;If the XREF palette is floating, the example code below should close it. However, if it is docked, the problem is not so simple.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can convert it to VB &lt;A href="http://converter.telerik.com/" target="_blank"&gt;here&lt;/A&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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 &amp;amp;&amp;amp; 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&amp;lt;IntPtr, bool&amp;gt; func = null;
      NativeMethods.EnumThreadWindowsCallback callback;

      protected WindowEnumerator()
      {
         this.callback = this.EnumWindowsCallback;
      }

      public WindowEnumerator(Func&amp;lt;IntPtr, bool&amp;gt; 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&amp;lt;IntPtr, bool&amp;gt; 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);

   }

}&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 Oct 2017 16:05:22 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2017-10-23T16:05:22Z</dc:date>
    <item>
      <title>vb.net - close xref-panel by code</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7483653#M29189</link>
      <description>&lt;P&gt;hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is it possible to close the xref-Panel by code and how?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 13:48:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7483653#M29189</guid>
      <dc:creator>jan_tappenbeck</dc:creator>
      <dc:date>2017-10-23T13:48:11Z</dc:date>
    </item>
    <item>
      <title>Re: vb.net - close xref-panel by code</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7484203#M29190</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3357387"&gt;@jan_tappenbeck&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is it possible to close the xref-Panel by code and how?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;If the XREF palette is floating, the example code below should close it. However, if it is docked, the problem is not so simple.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can convert it to VB &lt;A href="http://converter.telerik.com/" target="_blank"&gt;here&lt;/A&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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 &amp;amp;&amp;amp; 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&amp;lt;IntPtr, bool&amp;gt; func = null;
      NativeMethods.EnumThreadWindowsCallback callback;

      protected WindowEnumerator()
      {
         this.callback = this.EnumWindowsCallback;
      }

      public WindowEnumerator(Func&amp;lt;IntPtr, bool&amp;gt; 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&amp;lt;IntPtr, bool&amp;gt; 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);

   }

}&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Oct 2017 16:05:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7484203#M29190</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-10-23T16:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: vb.net - close xref-panel by code</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7489014#M29191</link>
      <description>&lt;P&gt;hi !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;first thanks for this example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but i have a Little problem in line&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Imports WinApi.Windows&lt;/PRE&gt;&lt;P&gt;this line will be marked because&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;the define namespace or type include not a public member or could not be found.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then there are some possible examples listed - but then other code-parts will be marked!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;could you give some more informations about the imported source ? should i add a reference to a special dll??&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 06:14:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7489014#M29191</guid>
      <dc:creator>jan_tappenbeck</dc:creator>
      <dc:date>2017-10-25T06:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: vb.net - close xref-panel by code</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7492626#M29192</link>
      <description>&lt;P&gt;hi !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i get some help in german vb-forum (&lt;A href="https://www.vb-paradise.de/index.php/Thread/124282-Problem-mit-einem-Imports-WinApi-Windows/#post1079727" target="_blank"&gt;https://www.vb-paradise.de/index.php/Thread/124282-Problem-mit-einem-Imports-WinApi-Windows/#post1079727&lt;/A&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;alternative for imports WinApi.Windows you have to modify this code-part.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;CommandMethod("XREFCLOSE", CommandFlags.Session)&amp;gt; _
Public Shared Sub XrefCloseCommand()
    Dim handle As IntPtr = WinApi.Windows.WindowFinder.FindThreadWindow(Nothing, "External References")
    If handle &amp;lt;&amp;gt; IntPtr.Zero Then
        WinApi.Windows.NativeMethods.PostMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
    End If
End Sub&lt;/PRE&gt;&lt;P&gt;now it works!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards Jan&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 08:09:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-close-xref-panel-by-code/m-p/7492626#M29192</guid>
      <dc:creator>jan_tappenbeck</dc:creator>
      <dc:date>2017-10-26T08:09:29Z</dc:date>
    </item>
  </channel>
</rss>

