vb.net - close xref-panel by code

vb.net - close xref-panel by code

jan_tappenbeck
Collaborator Collaborator
811 Views
3 Replies
Message 1 of 4

vb.net - close xref-panel by code

jan_tappenbeck
Collaborator
Collaborator

hi!

 

is it possible to close the xref-Panel by code and how?

 

regards Jan

0 Likes
812 Views
3 Replies
Replies (3)
Message 2 of 4

ActivistInvestor
Mentor
Mentor

@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);

   }

}
0 Likes
Message 3 of 4

jan_tappenbeck
Collaborator
Collaborator

hi !

 

first thanks for this example.

 

but i have a Little problem in line

 

Imports WinApi.Windows

this line will be marked because

 

 

the define namespace or type include not a public member or could not be found.

 

then there are some possible examples listed - but then other code-parts will be marked!

 

could you give some more informations about the imported source ? should i add a reference to a special dll??

 

regards Jan

0 Likes
Message 4 of 4

jan_tappenbeck
Collaborator
Collaborator

hi !

 

i get some help in german vb-forum (https://www.vb-paradise.de/index.php/Thread/124282-Problem-mit-einem-Imports-WinApi-Windows/#post107...)

 

alternative for imports WinApi.Windows you have to modify this code-part.

 

<CommandMethod("XREFCLOSE", CommandFlags.Session)> _
Public Shared Sub XrefCloseCommand()
    Dim handle As IntPtr = WinApi.Windows.WindowFinder.FindThreadWindow(Nothing, "External References")
    If handle <> IntPtr.Zero Then
        WinApi.Windows.NativeMethods.PostMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
    End If
End Sub

now it works!

 

 

regards Jan

0 Likes