Freigeben über


CommandLink wrapper in C++/CLI for you managed code under Vista

Catherine, una amiga mía, Technical Evangelist en DPE, wrote some C# code that will be shared soon during a "Light Up" lab event in Munich. When reading it, the first thing that came to my mind was: "Ce serait tellement plus élégant en C++/CLI". Before I share it, let me point you to a related folder you might find interesting if you want to take advantage of the new stuff in Vista from your managed application:

%ProgramFiles%\Microsoft SDKs\Windows\v1.0\Samples\CrossTechnologySamples\VistaBridge

I heard her and Jeff talk about this the other day and I'm guessing we'll see a lot more soon. But hey, I don't want to put pressure on their shoulders! ;-)

Don't hesitate to send me ideas on how to improve it:

#pragma once

#pragma comment( lib, "user32" )

#define _WIN32_WINNT 0x0600

#include <Windows.h>

#include <CommCtrl.h>

#include <vcclr.h>

#include <malloc.h>

using namespace System;

namespace CommandLinkControl {

public ref class CommandLink : Windows::Forms::Button {

    public:

      CommandLink() {

         FlatStyle = Windows::Forms::FlatStyle::System;

      }

      [ComponentModel::Category("Appearance")]

      [ComponentModel::Description("Specifies the note.")]

      [ComponentModel::BrowsableAttribute(true)]

      [ComponentModel::DefaultValue("(Note Text)")]

      property String ^ Note {

         String ^ get() {

            HWND hwnd = static_cast<HWND>(static_cast<void *>(Handle));

            LRESULT noteLength = Button_GetNoteLength(hwnd) + 1;

            if ( noteLength <= 1 )

               return String::Empty;

                  wchar_t * note = static_cast<wchar_t *>(_malloca(noteLength*sizeof(*note)));

            Button_GetNote(hwnd, note,& noteLength);

            String ^ s = gcnew String(note);

                  _freea(note);

            return s;

         }

         void set( String ^ value ) {

            pin_ptr<const wchar_t> p = PtrToStringChars(value) ;

            Button_SetNote(static_cast<HWND>(static_cast<void *>(Handle)), p);

         }

      }

     [ComponentModel::Category("Appearance")]

     [ComponentModel::Description("Indicates whether the button should be decorated with the security shield icon (Windows Vista only).")]

     [ComponentModel::BrowsableAttribute(true)]

     [ComponentModel::DefaultValue(false)]

     property bool ShieldIcon {

         bool get() {

            return shieldIconDisplayed;

         }

         void set( bool value ) {

            shieldIconDisplayed = value;

            Button_SetElevationRequiredState( static_cast<HWND>(static_cast<void *>(Handle)), value ? TRUE : FALSE);

         }

     }

      protected:

         property Windows::Forms::CreateParams ^ CreateParams {

            virtual Windows::Forms::CreateParams ^ get() override {

               Windows::Forms::CreateParams ^ cp = Button::CreateParams;

               if ( Environment::OSVersion->Version->Major >= 6) {

                  cp->Style |= BS_COMMANDLINK;

               }

               return cp;

            }

        }

         property Drawing::Size DefaultSize {

            virtual Drawing::Size get() override {

               return Drawing::Size(180,60);

            }

        }

   private:

      bool shieldIconDisplayed;

};

} // namespace CommandLinkControl

Comments

  • Anonymous
    June 10, 2006
    Great post, good demonstration of C++/CLI benefits.

    Remember that you have to "Register the Windows SDK Directories with Visual Studio 2005" prior to compiling this code. Otherwise the #include <CommCtrl.h> will default to old platform SDK version and complain about some of the macros.