Dzyann Dzyann - 3 months ago 38
C# Question

Is there a way to know if there is a IWin32Window already created?

I am trying to integrate my application with several others.
In one case I am getting the following exception:

System.InvalidOperationException was unhandled by user code
Message=SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.


This exception is because i am trying to do:

System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);


And the application I am trying to integrate with has already created some IWin32Window.

I have searched around and what I get is that I basically should not try to
SetCompatibleTextRenderingDefault(false)
if there is already a IWin32Window.
But since my application is going to be integrated in various others the conditions in each case change.

For now I am planning to change my application to receive a parameter so depending on the context I could run the
SetCompatibleTextRenderingDefault(false)
or not.

But I would like, instead, to know if there is a way that I could verify if there is already a IWin32Window created before doing
SetCompatibleTextRenderingDefault(false)
.

I am sorry if this is a dumb question but I haven't really worked much with Winforms before.

I would appreciate any help you can give me.

Update

After reading the first answer and some of the comments, I want to mention that the context where I am having this problem is rather complex and I can not completely explain it here. I appreciate the feedback and I understand what you are saying.

I think the spirit of my question was misunderstood by the context I added, I know why the error is happening and I know ways to solve it and get a work around.

I just would like to know if there is a way to know if there is an IWin32Window already created?

After all, the exception is happening because the IWin32Window is already created, so I was wondering how can you know that.

Thanks again.

Answer

There is no way to know if a IWin32Window was already created in my context (the framework doesn't offer it).

The only possible work around, is having your own Custom Form that inherits from Winforms Form. That way you can get when the forms are being created.

I can not do that, because my app gets integrated with many other, and I could not force them all to use other than Windows Form.

By setting:

System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

We were trying to save some time to the applications we were integrating with, however when we started having this error we removed the code, and stablished they should be the ones to configure the compatibility settings.

Comments