Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trouble with Windows Forms, in method "SetCompatibleTextRenderingDefault"

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Anonymous
4033 Views, 4 Replies

Trouble with Windows Forms, in method "SetCompatibleTextRenderingDefault"

Anonymous
Not applicable

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):

formview.jpg

 

Finally, here's a screenshot of the error message in revit:

errormsg.jpg

 

Thank you

Leandro

 

0 Likes

Trouble with Windows Forms, in method "SetCompatibleTextRenderingDefault"

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):

formview.jpg

 

Finally, here's a screenshot of the error message in revit:

errormsg.jpg

 

Thank you

Leandro

 

4 REPLIES 4
Message 2 of 5
RPTHOMAS108
in reply to: Anonymous

RPTHOMAS108
Mentor
Mentor

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.

0 Likes

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.

Message 3 of 5
Anonymous
in reply to: RPTHOMAS108

Anonymous
Not applicable

I've tried it, but it didn't worked.

0 Likes

I've tried it, but it didn't worked.

Message 4 of 5
RPTHOMAS108
in reply to: Anonymous

RPTHOMAS108
Mentor
Mentor
Accepted solution

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.

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.

Message 5 of 5
Anonymous
in reply to: RPTHOMAS108

Anonymous
Not applicable

It worked fine! Thanks a lot.

0 Likes

It worked fine! Thanks a lot.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report