Adding custom forms to the Project Browser window

Adding custom forms to the Project Browser window

obstaclestone
Observer Observer
234 Views
4 Replies
Message 1 of 5

Adding custom forms to the Project Browser window

obstaclestone
Observer
Observer

Good afternoon.

I need to add a grid with button to the project browser window under the heading. Is there any way to implement this? I tried to do this:

using System;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;

namespace ProjectManagerModification
{
	
	[TransactionAttribute(TransactionMode.Manual)]
	[RegenerationAttribute(RegenerationOption.Manual)]
	
	public class ModificationProjectManagerCommand : IExternalCommand
	{
	
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			
			var uiApp = commandData.Application;
			var uiDoc = uiApp.ActiveUIDocument;
		
			IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
		    
			//Process[] processes = Process.GetProcesses();
			//var targetProcess = processes.FirstOrDefault(p => p.MainWindowTitle.Contains("Revit"));

			if (mainWindowHandle != IntPtr.Zero)
			{
				FindSubWindow(uiApp, mainWindowHandle);
			}
		
			return Result.Succeeded;
		    
		}
		
		[DllImport("user32.dll")]
		private static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
	    
		[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
		static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
		
		private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
	    
		public static void FindSubWindow(Autodesk.Revit.UI.UIApplication uiApp, IntPtr mainWindowHandle)
		{
			EnumChildWindows(mainWindowHandle, (hWnd, lParam) =>
			{
				StringBuilder sb = new StringBuilder(256);
				GetWindowText(hWnd, sb, sb.Capacity);
				if (sb.ToString().Contains("Project Browser"))
				{     
		        	
					System.Windows.Forms.MessageBox.Show("Find subwindow with title: " + sb.ToString());
		            
					System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
					panel.Dock = DockStyle.Fill;
			
					System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
					textBox.Text = "Text example";
					textBox.Dock = DockStyle.Fill;
			
					panel.Controls.Add(textBox);
			        
					System.Windows.Forms.Form subWindow = System.Windows.Forms.Form.FromHandle(hWnd) as System.Windows.Forms.Form;
			        
					subWindow.Controls.Add(panel);
					subWindow.Controls.SetChildIndex(panel, 0);
		
					return false;
				}
				return true;
			}, IntPtr.Zero);
		}
	}
}



The idea is to add a panel with a button under the title of the project Manager window, clicking on which will open a window with filters to hide TreeView elements in the project browser.

Perhaps Windows Forms are not suitable for this window.

0 Likes
235 Views
4 Replies
Replies (4)
Message 2 of 5

franciscopossetto
Advocate
Advocate

Hey, I have not done this before, but it sounds cool.

If you cannot get the grid working directly in the Project Browser I think you could try a dockable panel as an alternative. This alternative would be like making the project browser again but it could work.

Do you want to do anything else with the grid, besides filtering and opening the view, sheet, etc?

Github:
https://github.com/franpossetto
0 Likes
Message 3 of 5

obstaclestone
Observer
Observer

Thanks for the reply.
I haven't tried to access the grid yet. It is worth trying to parse the project manager window to understand which elements are included in it.
The catch in this case is in the 72nd line of code:

subWindow.Controls.Add(panel);

The following error occurs: "System.NullReferenceException. An object reference does not point to an instance of an object".

0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni

Well, this is definitely not officially supported by the Revit API, and hacking the Revit UI by unofficial means has proven  rather tricky...

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 5

mhannonQ65N2
Collaborator
Collaborator

Your problem is that the Properties Browser is not a WinForms Form or Control so System.Windows.Forms.Form.FromHandle(hWnd) on line 70 will return null.

0 Likes