Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. How to Set the Window Class Name of a Windows Form App?

How to Set the Window Class Name of a Windows Form App?

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpwinformsalgorithmshelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Gerrans
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • M 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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 the WNDCLASSEX 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 override CreateParams in your Control derivative and set the CreateParams.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 enumerating Process.GetProcesses (which can also gets the processes given an executable name). Using the Process class's properties, you can discover additional information. If you need an HWND at any point, just use Process.MainWindowHandle.

      Microsoft MVP, Visual C# My Articles

      M 1 Reply Last reply
      0
      • H Heath Stewart

        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 the WNDCLASSEX 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 override CreateParams in your Control derivative and set the CreateParams.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 enumerating Process.GetProcesses (which can also gets the processes given an executable name). Using the Process class's properties, you can discover additional information. If you need an HWND at any point, just use Process.MainWindowHandle.

        Microsoft MVP, Visual C# My Articles

        M Offline
        M Offline
        Matt Gerrans
        wrote on last edited by
        #3

        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 the Control class, which is a few bases up the hierarchy from the Form class. Are you talking about creating a derivation of Control and replacing the main form with it? I had considered the Process 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 that MainWindowHandle 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 the Process 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

        H 1 Reply Last reply
        0
        • M 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 the Control class, which is a few bases up the hierarchy from the Form class. Are you talking about creating a derivation of Control and replacing the main form with it? I had considered the Process 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 that MainWindowHandle 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 the Process 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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Gerrans 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 the CreateParams. If you want to just change a style, you typically get the base.CreateParams, modify, and return it; but in your case you're changing the class name - a pretty big change. Construct your CreateParams, 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups