Hi everyone. I'm trying to make an add-in which uses a windows form. For that, I copied the code used in the "Getting Started" walkthrough in the Revit API guide, and modifided it. I'm always getting the same error message: "SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.". Here is the code that I've tried to use.
CsAddPanel.cs:
using System;using System.Reflection; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using System.Windows.Media.Imaging; using System.Windows.Forms; namespace Walkthrough { /// <remarks> /// This application's main class. The class must be Public. /// </remarks> public class CsAddPanel : IExternalApplication { // Both OnStartup and OnShutdown must be implemented as public method public Result OnStartup(UIControlledApplication application) { // Add a new ribbon panel RibbonPanel ribbonPanel = application.CreateRibbonPanel("NewRibbonPanel"); // Create a push button to trigger a command add it to the ribbon panel. string thisAssemblyPath = Assembly.GetExecutingAssembly().Location; PushButtonData buttonData = new PushButtonData("cmdHelloWorld", "Hello World", thisAssemblyPath, "Walkthrough.HelloWorld"); PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton; // Optionally, other properties may be assigned to the button // a) tool-tip pushButton.ToolTip = "Say hello to the entire world."; // b) large bitmap Uri uriImage = new Uri(@"C:\globe.png"); BitmapImage largeImage = new BitmapImage(uriImage); pushButton.LargeImage = largeImage; return Result.Succeeded; } public Result OnShutdown(UIControlledApplication application) { // nothing to clean up in this simple case return Result.Succeeded; } } /// <remarks> /// The "HelloWorld" external command. The class must be Public. /// </remarks> [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class HelloWorld : IExternalCommand { // The main Execute method (inherited from IExternalCommand) must be public public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements) { Form1 form1 = new Form1(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); form1.ShowDialog(); //TaskDialog.Show("Revit", "Hello World"); return Autodesk.Revit.UI.Result.Succeeded; } } }
Here's a screenshot of the form that I'm trying to use (and of the SharpDevelop views):
Finally, here's a screenshot of the error message in revit:
Thank you
Leandro
Solved! Go to Solution.
Solved by RPTHOMAS108. Go to Solution.
I'm not familiar with 'Application.SetCompatibleTextRenderingDefault(false)' it appears to be a legacy setting and is probably not required (since false is apparently the default from 2.0 onwards). The error is asking you to put the line:
Application.SetCompatibleTextRenderingDefault(false);
before the line:
Form1 form1 = new Form1()
You should probably do the same with Application.EnableVisualStyles.
Take the line out completely you'll likely notice no difference from the intended functionality of the sample.
Reading further I believe the call is only valid for standalone apps. If the app is hosted by another then there is little chance a Window hasn't already been created before you call this function.
Can't find what you're looking for? Ask the community or share your knowledge.