How to Set the Window Class Name of a Windows Form App?
-
Anyone know how to set the Window class name for the main form of a Windows Forms .Net application? By default, they get class names like "WindowsForms10.Window.8.app3" -- I searched through all the files in the project and can't see where that's coming from. I didn't see anything in the base Form or in the Application object that does it, either... Unfortunately, the overloaded nature of the words "window" and "class" make searching the help herculean task. I need to have a well-defined class name (not the semi-random one that is autogenerated), so that I can find the app at runtime with FindWindow(); since the titlebar may be localized and may have additional stuff (like "My Localized AppName - Current document name.ext"), I would rather use a class name which could be more dependable. Matt Gerrans
-
Anyone know how to set the Window class name for the main form of a Windows Forms .Net application? By default, they get class names like "WindowsForms10.Window.8.app3" -- I searched through all the files in the project and can't see where that's coming from. I didn't see anything in the base Form or in the Application object that does it, either... Unfortunately, the overloaded nature of the words "window" and "class" make searching the help herculean task. I need to have a well-defined class name (not the semi-random one that is autogenerated), so that I can find the app at runtime with FindWindow(); since the titlebar may be localized and may have additional stuff (like "My Localized AppName - Current document name.ext"), I would rather use a class name which could be more dependable. Matt Gerrans
The "semi-random" one is not actually random - if you specify a registered window class. The window class is actually used in well-defined pattern. If the control does not have a window class associated with it, then it becomes random (though the basic pattern is still used). Just like with VC++, MFC, etc., you need to either use a window class that is already registered (like Static, Button, etc.) or register your own a la
RegisterClassEx
, which uses theWNDCLASSEX
structure. You can P/Invoke this easily enough. Register the class (with a classname of your choosing, so long as it doesn't already exist) once, then overrideCreateParams
in yourControl
derivative and set theCreateParams.ClassName
as well as any other extended, window, or class styles you want. Also, if you're merely looking for your application, you might consider enumeratingProcess.GetProcesses
(which can also gets the processes given an executable name). Using theProcess
class's properties, you can discover additional information. If you need anHWND
at any point, just useProcess.MainWindowHandle
.Microsoft MVP, Visual C# My Articles
-
The "semi-random" one is not actually random - if you specify a registered window class. The window class is actually used in well-defined pattern. If the control does not have a window class associated with it, then it becomes random (though the basic pattern is still used). Just like with VC++, MFC, etc., you need to either use a window class that is already registered (like Static, Button, etc.) or register your own a la
RegisterClassEx
, which uses theWNDCLASSEX
structure. You can P/Invoke this easily enough. Register the class (with a classname of your choosing, so long as it doesn't already exist) once, then overrideCreateParams
in yourControl
derivative and set theCreateParams.ClassName
as well as any other extended, window, or class styles you want. Also, if you're merely looking for your application, you might consider enumeratingProcess.GetProcesses
(which can also gets the processes given an executable name). Using theProcess
class's properties, you can discover additional information. If you need anHWND
at any point, just useProcess.MainWindowHandle
.Microsoft MVP, Visual C# My Articles
Hi Heath, I'm not clear on how you can change the
CreateParams
of the app's main form, since it is a protected member of theControl
class, which is a few bases up the hierarchy from theForm
class. Are you talking about creating a derivation ofControl
and replacing the main form with it? I had considered theProcess
stuff, but was worried it wouldn't work with limited user accounts. However, I've just tried that and it works fine. I didn't know (or expect) that it had thatMainWindowHandle
property, either; that works out excellently; in fact, it looks like it is quite easy to get all the useful process and window information from theProcess
object (unlike in the Windows API, where it is a bit more complicated to connect processes information to window information). I think I'll go this route and be able to take care of what I need to do by using the process list. Thanks Heath! Matt Gerrans -
Hi Heath, I'm not clear on how you can change the
CreateParams
of the app's main form, since it is a protected member of theControl
class, which is a few bases up the hierarchy from theForm
class. Are you talking about creating a derivation ofControl
and replacing the main form with it? I had considered theProcess
stuff, but was worried it wouldn't work with limited user accounts. However, I've just tried that and it works fine. I didn't know (or expect) that it had thatMainWindowHandle
property, either; that works out excellently; in fact, it looks like it is quite easy to get all the useful process and window information from theProcess
object (unlike in the Windows API, where it is a bit more complicated to connect processes information to window information). I think I'll go this route and be able to take care of what I need to do by using the process list. Thanks Heath! Matt GerransGerrans wrote: I'm not clear on how you can change the CreateParams of the app's main form, since it is a protected member of the Control class, which is a few bases up the hierarchy from the Form class. By extending the
Form
class with your own class, then override theCreateParams
. If you want to just change a style, you typically get thebase.CreateParams
, modify, and return it; but in your case you're changing the class name - a pretty big change. Construct yourCreateParams
, change the styles and class name, etc., then return it. Gerrans wrote: I didn't know (or expect) that it had that MainWindowHandle property You should browse through the Class Library documentation in the .NET Framework SDK. It's not like you have to memorize everything, but you should recognize a naming pattern that Microsoft sticks to about 98% of the time (according to FxCop). That makes it easy to find what you need, especially if you have some idea of what to look for.Microsoft MVP, Visual C# My Articles