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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to use uxtheme APIs in custom control?

How to use uxtheme APIs in custom control?

Scheduled Pinned Locked Moved C#
helpquestiontutorialdiscussionlearning
9 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.
  • L Offline
    L Offline
    Li kai Liu Angus
    wrote on last edited by
    #1

    Hi, I'm wrting a custom control that will use theme library to draw my control. But whenever I call OpenThemeData, all that I get is a NULL theme handle. Is there any trick to do before calling this function? I saw one guy posting the same question a couple months ago and sought no answer. I also read James T. Johnson & akr0's discussion on this issue. However, not quite understand what James mean by using P/Invoke. What's P/Invoke? (I know it may be a dump question, but I'm a beginner...) Any help will be appreciated. It would be better if you can provide a code snippet to illustrate. Thanks:)

    J 1 Reply Last reply
    0
    • L Li kai Liu Angus

      Hi, I'm wrting a custom control that will use theme library to draw my control. But whenever I call OpenThemeData, all that I get is a NULL theme handle. Is there any trick to do before calling this function? I saw one guy posting the same question a couple months ago and sought no answer. I also read James T. Johnson & akr0's discussion on this issue. However, not quite understand what James mean by using P/Invoke. What's P/Invoke? (I know it may be a dump question, but I'm a beginner...) Any help will be appreciated. It would be better if you can provide a code snippet to illustrate. Thanks:)

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Li-kai Liu (Angus) wrote: I'm wrting a custom control that will use theme library to draw my control. Unless you are doing something special the "easy" thing to do is to create a manifest file for your application and set the FlatStyle of your controls to FlatStyle.System. If you want to go this route, Chris Sells' website[^] has a manifest file for VS.NET, you can use this same file for your application just rename it to myapp.exe.manifest. That said, on to your other question. Li-kai Liu (Angus) wrote: What's P/Invoke? P/Invoke is a method of executing functions from a C-style dll in a .NET program. The first step is to declare the function you wish to use and the file that it is in. I'll use OpenThemeData as an example. There is a step embedded in this first step, in that you have to convert all the types used into .NET equivalent types. This is something you learn with practice and time.

      [DllImport("uxtheme.dll")]
      public extern static IntPtr OpenThemeData(IntPtr hwnd, string pszClassList);

      Note that since the second parameter is a wide string (unicode) I could just pass the string in directly and not have to involve the MarshalAs attribute. For more information look up the DllImport and MarshalAs attributes up in MSDN. If you have to pass structures around you'll probably also need to look at the StructLayout attribute. HTH, James

      L 1 Reply Last reply
      0
      • J James T Johnson

        Li-kai Liu (Angus) wrote: I'm wrting a custom control that will use theme library to draw my control. Unless you are doing something special the "easy" thing to do is to create a manifest file for your application and set the FlatStyle of your controls to FlatStyle.System. If you want to go this route, Chris Sells' website[^] has a manifest file for VS.NET, you can use this same file for your application just rename it to myapp.exe.manifest. That said, on to your other question. Li-kai Liu (Angus) wrote: What's P/Invoke? P/Invoke is a method of executing functions from a C-style dll in a .NET program. The first step is to declare the function you wish to use and the file that it is in. I'll use OpenThemeData as an example. There is a step embedded in this first step, in that you have to convert all the types used into .NET equivalent types. This is something you learn with practice and time.

        [DllImport("uxtheme.dll")]
        public extern static IntPtr OpenThemeData(IntPtr hwnd, string pszClassList);

        Note that since the second parameter is a wide string (unicode) I could just pass the string in directly and not have to involve the MarshalAs attribute. For more information look up the DllImport and MarshalAs attributes up in MSDN. If you have to pass structures around you'll probably also need to look at the StructLayout attribute. HTH, James

        L Offline
        L Offline
        Li kai Liu Angus
        wrote on last edited by
        #3

        Hi, James The thing you addressed in the second part using P/Inovoke is what I've done in my program. But it's still not working. I've already had my unmanaged function declared as you did in the post. And it looks to me that OpenThemeData doesn't work as it should do. I had another uxtheme API static public extern bool IsThemeActive(); It works very well. But just don't know why OpenThemeData always return Null whenever I call it. Part of the code in my control is as follow: protected override void OnPaint(PaintEventArgs pe) { IntPtr hTheme = OpenThemeData(this.Handle , "button"); if (hTheme != IntPtr.Zero) { // draw themed control } else { // draw normal control } } However, OpenThemeData always return Null... Am I doing right with this? Thanks!

        J 1 Reply Last reply
        0
        • L Li kai Liu Angus

          Hi, James The thing you addressed in the second part using P/Inovoke is what I've done in my program. But it's still not working. I've already had my unmanaged function declared as you did in the post. And it looks to me that OpenThemeData doesn't work as it should do. I had another uxtheme API static public extern bool IsThemeActive(); It works very well. But just don't know why OpenThemeData always return Null whenever I call it. Part of the code in my control is as follow: protected override void OnPaint(PaintEventArgs pe) { IntPtr hTheme = OpenThemeData(this.Handle , "button"); if (hTheme != IntPtr.Zero) { // draw themed control } else { // draw normal control } } However, OpenThemeData always return Null... Am I doing right with this? Thanks!

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          I think it is a result of using OpenThemeData incorrectly; but the docs aren't that clear on how it should be used. :eek: I only have one suggestion and it isn't a very good one. Put the class name in all caps ("BUTTON") or try "Button" I tried to download the MSDN sample app but I just got a 404 instead :(( James

          L 1 Reply Last reply
          0
          • J James T Johnson

            I think it is a result of using OpenThemeData incorrectly; but the docs aren't that clear on how it should be used. :eek: I only have one suggestion and it isn't a very good one. Put the class name in all caps ("BUTTON") or try "Button" I tried to download the MSDN sample app but I just got a 404 instead :(( James

            L Offline
            L Offline
            Li kai Liu Angus
            wrote on last edited by
            #5

            have tired "BUTTON" & "Button". still the same:(( sample app? Do you mean ThemeExplore?? Li-kai

            J 1 Reply Last reply
            0
            • L Li kai Liu Angus

              have tired "BUTTON" & "Button". still the same:(( sample app? Do you mean ThemeExplore?? Li-kai

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              Li-kai Liu (Angus) wrote: Do you mean ThemeExplore yep; I wanted to see how it used the various functions. James

              L 1 Reply Last reply
              0
              • J James T Johnson

                Li-kai Liu (Angus) wrote: Do you mean ThemeExplore yep; I wanted to see how it used the various functions. James

                L Offline
                L Offline
                Li kai Liu Angus
                wrote on last edited by
                #7

                I put a copy at http://www.ykliu.com/ThemeExplorer.zip if you wish to have a look. Maybe a bit slow when downloading:) Li-kai

                J 1 Reply Last reply
                0
                • L Li kai Liu Angus

                  I put a copy at http://www.ykliu.com/ThemeExplorer.zip if you wish to have a look. Maybe a bit slow when downloading:) Li-kai

                  J Offline
                  J Offline
                  James T Johnson
                  wrote on last edited by
                  #8

                  Thanks, Looking at the source it passes in NULL for the handle and a valid class name, so this might be the problem you are experiencing. James

                  L 1 Reply Last reply
                  0
                  • J James T Johnson

                    Thanks, Looking at the source it passes in NULL for the handle and a valid class name, so this might be the problem you are experiencing. James

                    L Offline
                    L Offline
                    Li kai Liu Angus
                    wrote on last edited by
                    #9

                    Thanks a lot!! You've been always helpful:)

                    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