Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

[BCB]MENU CONTEXTUEL STYLE XP


Information sur la source

Catégorie :Graphique Niveau : Expert Date de création : 20/12/2001 Date de mise à jour : 20/12/2001 18:07:26 Vu / téléchargé: 6 676 / 391

Note :
Aucune note

Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

Description

Cliquez pour voir la capture en taille normale
Crée un menu comme windows XP en modifiant le composant standard TMainMenu.

 

Source

  • //*****Fichier .h***************
  • //---------------------------------------------------------------------------
  • #ifndef Unit1H
  • #define Unit1H
  • //---------------------------------------------------------------------------
  • #include <Classes.hpp>
  • #include <Controls.hpp>
  • #include <StdCtrls.hpp>
  • #include <Forms.hpp>
  • #include <Menus.hpp>
  • #include <ImgList.hpp>
  • //---------------------------------------------------------------------------
  • const AnsiString BLANK_LINE="-";
  • class TForm1 : public TForm
  • {
  • __published:
  • TMainMenu *MainMenu1;
  • TMenuItem *Example1;
  • TMenuItem *MenuItemOne1;
  • TMenuItem *MenuItemThree1;
  • TMenuItem *MenuItemFour1;
  • TMenuItem *MenuItemFive1;
  • TMenuItem *MenuItemTen1;
  • TImageList *ImageList1;
  • TMenuItem *N1;
  • TMenuItem *N2;
  • TLabel *Label1;
  • TLabel *Label2;
  • void __fastcall Example1Click(TObject *Sender);
  • void __fastcall Example1DrawItem(TObject *Sender, TCanvas *ACanvas,
  • TRect &ARect, bool Selected);
  • void __fastcall FormCreate(TObject *Sender);
  • private:
  • TColor MainMenuBackground;
  • TColor MainMenuHighlightColor;
  • TColor MainMenuTextColor;
  • TColor MainMenuTextBackground;
  • TColor MainMenuHighlightTextColor;
  • TColor Custom;
  • TColor VerticalColor;
  • TColor MenuColor;
  • TColor HighlightColor;
  • TColor BorderColor;
  • TColor NormalTextColor;
  • TColor NormalTextBackground;
  • TColor HighlightTextColor;
  • TColor DisabledTextColor;
  • int VerticalWidth;
  • int FocusRectRightIndent;
  • int FocusRectLeftIndent;
  • int LeftTextPos;
  • int SideBuffer;
  • int MenuIncreaseWidth;
  • int Offset;
  • int MenuItemHeight;
  • int ItemOffset;
  • TIcon *Icon;
  • Graphics::TBitmap *Image;
  • protected:
  • void __fastcall MyExpandItemWidth(TObject *Sender, TCanvas *ACanvas,
  • int &Width, int &Height);
  • void __fastcall MyDrawItem(TObject* Sender, TCanvas* ACanvas,
  • const TRect &ARect, bool Selected);
  • public:
  • __fastcall TForm1(TComponent* Owner);
  • };
  • //---------------------------------------------------------------------------
  • extern PACKAGE TForm1 *Form1;
  • //---------------------------------------------------------------------------
  • #endif
  • //*********Fichier .cpp*****************
  • #include <vcl.h>
  • #pragma hdrstop
  • #include "Unit1.h"
  • //---------------------------------------------------------------------------
  • #pragma package(smart_init)
  • #pragma resource "*.dfm"
  • TForm1 *Form1;
  • //---------------------------------------------------------------------------
  • __fastcall TForm1::TForm1(TComponent* Owner)
  • : TForm(Owner)
  • {
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::Example1DrawItem(TObject *Sender, TCanvas *ACanvas,
  • TRect &ARect, bool Selected)
  • {
  • TRect FocusRectBorder;
  • TRect FocusRectFill;
  • TMenuItem *MenuItem = ((TMenuItem*)Sender);
  • AnsiString Text = MenuItem->Caption;
  • ACanvas->Brush->Color = MainMenuBackground;
  • ACanvas->FillRect(ARect);
  • if(Text == "")
  • return;
  • if(Selected)
  • {
  • FocusRectBorder = ARect;
  • ACanvas->Brush->Color = BorderColor;
  • ACanvas->FrameRect(FocusRectBorder);
  • FocusRectFill = ARect;
  • FocusRectFill.Top += SideBuffer;
  • FocusRectFill.Right -= SideBuffer;
  • FocusRectFill.Left += SideBuffer;
  • FocusRectFill.Bottom -= SideBuffer;
  • ACanvas->Brush->Color = MainMenuHighlightColor;
  • ACanvas->FillRect(FocusRectFill);
  • ACanvas->Font->Color = MainMenuHighlightTextColor;
  • }
  • else
  • {
  • ACanvas->Font->Color = MainMenuTextColor;
  • }
  • int TextLength;
  • TRect TextRect;
  • TextLength = Text.Length();
  • TextRect = ARect;
  • TextRect.Left += 5;
  • TextRect.Top += 1;
  • DrawText(ACanvas->Handle,Text.c_str(), TextLength, &TextRect, 0);
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::MyDrawItem(TObject* Sender, TCanvas* ACanvas,
  • const TRect &ARect, bool Selected)
  • {
  • int TopPos, TextLength;
  • AnsiString Text;
  • TRect TempRect;
  • TRect VerticalRect;
  • TRect FocusRectBorder;
  • TRect FocusRectFill;
  • TRect TextRect;
  • TMenuItem *MenuItem = ((TMenuItem*)Sender);
  • Text = MenuItem->Caption;
  • ACanvas->Brush->Color = MenuColor;
  • ACanvas->FillRect(ARect);
  • if(Text==BLANK_LINE)
  • {
  • VerticalRect = ARect;
  • VerticalRect.Top -= SideBuffer;
  • VerticalRect.Right = VerticalWidth;
  • VerticalRect.Bottom += SideBuffer;
  • ACanvas->Brush->Color = VerticalColor;
  • ACanvas->FillRect(VerticalRect);
  • ACanvas->MoveTo(VerticalWidth,ARect.Top+ARect.Height()/2);
  • ACanvas->LineTo(ARect.Right,ARect.Top+ARect.Height()/2);
  • return;
  • }
  • TextLength = Text.Length();
  • if(Selected)
  • {
  • VerticalRect = ARect;
  • VerticalRect.Top -= SideBuffer;
  • VerticalRect.Right = VerticalWidth;
  • VerticalRect.Bottom += SideBuffer;
  • ACanvas->Brush->Color = VerticalColor;
  • ACanvas->FillRect(VerticalRect);
  • if(MenuItem->Enabled)
  • {
  • FocusRectBorder = ARect;
  • FocusRectBorder.Left += FocusRectLeftIndent - SideBuffer;
  • FocusRectBorder.Right -= FocusRectRightIndent - SideBuffer;
  • ACanvas->Brush->Color = BorderColor;
  • ACanvas->FrameRect(FocusRectBorder);
  • FocusRectFill = ARect;
  • FocusRectFill.Right -= FocusRectRightIndent;
  • FocusRectFill.Left += FocusRectLeftIndent;
  • FocusRectFill.Bottom -= SideBuffer;
  • FocusRectFill.Top += SideBuffer;
  • ACanvas->Brush->Color = HighlightColor;
  • ACanvas->FillRect(FocusRectFill);
  • ACanvas->Font->Color = HighlightTextColor;
  • ACanvas->Font->Style = TFontStyles() << fsBold;
  • }
  • else
  • {
  • ACanvas->Font->Style = TFontStyles();
  • ACanvas->Brush->Color = NormalTextBackground;
  • ACanvas->Font->Color = DisabledTextColor;
  • }
  • }
  • else
  • {
  • VerticalRect = ARect;
  • VerticalRect.Top -= SideBuffer;
  • VerticalRect.Right = VerticalWidth;
  • VerticalRect.Bottom += SideBuffer;
  • ACanvas->Brush->Color = VerticalColor;
  • ACanvas->FillRect(VerticalRect);
  • if(MenuItem->Enabled)
  • {
  • ACanvas->Brush->Color = NormalTextBackground;
  • ACanvas->Font->Color = NormalTextColor;
  • }
  • else
  • {
  • ACanvas->Brush->Color = NormalTextBackground;
  • ACanvas->Font->Color = DisabledTextColor;
  • }
  • }
  • TextRect = ARect;
  • TextRect.Left += LeftTextPos;
  • if(Offset > 0)
  • TextRect.Top += Offset/2 + SideBuffer;
  • else
  • TextRect.Top += 2 + SideBuffer;
  • TextRect.Top += SideBuffer;
  • if(Menu->Images != NULL)
  • {
  • Icon = new TIcon();
  • Menu->Images->GetIcon(MenuItem->ImageIndex,Icon);
  • ACanvas->Draw(5,ARect.Top+ItemOffset+1,Icon);
  • delete Icon;
  • }
  • DrawText(ACanvas->Handle,Text.c_str(), TextLength, &TextRect, 0);
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::MyExpandItemWidth(TObject *Sender,
  • TCanvas *ACanvas, int &Width, int &Height)
  • {
  • Width += MenuIncreaseWidth;
  • Height += Offset;
  • MenuItemHeight = Height;
  • ItemOffset = Offset/2;
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::FormCreate(TObject *Sender)
  • {
  • //**************couleur du highlight
  • Custom=TColor(RGB(255,200,200));
  • MainMenuBackground = clSilver;
  • MainMenuHighlightColor = Custom;
  • MainMenuTextColor = clBlack;
  • MainMenuTextBackground = clSilver;
  • MainMenuHighlightTextColor = clBlack;
  • VerticalColor = clSilver;
  • MenuColor = clWhite;
  • HighlightColor = Custom;
  • BorderColor = clBlack;
  • NormalTextColor = clBlack;
  • NormalTextBackground = clWhite;
  • HighlightTextColor = clBlack;
  • DisabledTextColor = clSilver;
  • VerticalWidth = 26;
  • FocusRectRightIndent = 3;
  • FocusRectLeftIndent = 3;
  • LeftTextPos = 35;
  • SideBuffer = 1;
  • if(Menu->Images == NULL)
  • MenuIncreaseWidth = 100;
  • else
  • MenuIncreaseWidth = 50;
  • Offset = 5;
  • }
  • //---------------------------------------------------------------------------
  • void __fastcall TForm1::Example1Click(TObject *Sender)
  • {
  • TMenuItem *MenuItem = ((TMenuItem*)Sender);
  • if(MenuItem->Count > 0)
  • {
  • for(int i=0; i <= MenuItem->Count-1; i++)
  • {
  • MenuItem->Items[i]->OnMeasureItem = MyExpandItemWidth;
  • MenuItem->Items[i]->OnDrawItem = MyDrawItem;
  • if(MenuItem->Items[i]->Count > 0)
  • {
  • for(int x=0; x <= MenuItem->Items[i]->Count-1; x++)
  • {
  • MenuItem->Items[i]->Items[x]->OnMeasureItem = MyExpandItemWidth;
  • MenuItem->Items[i]->Items[x]->OnDrawItem = MyDrawItem;
  • }
  • }
  • }
  • }
  • }
  • //---------------------------------------------------------------------------
//*****Fichier .h***************
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Menus.hpp>
#include <ImgList.hpp>
//---------------------------------------------------------------------------
const AnsiString BLANK_LINE="-";


class TForm1 : public TForm
{
__published:
   TMainMenu *MainMenu1;
   TMenuItem *Example1;
   TMenuItem *MenuItemOne1;
   TMenuItem *MenuItemThree1;
   TMenuItem *MenuItemFour1;
   TMenuItem *MenuItemFive1;
   TMenuItem *MenuItemTen1;
   TImageList *ImageList1;
   TMenuItem *N1;
   TMenuItem *N2;
        TLabel *Label1;
        TLabel *Label2;
   void __fastcall Example1Click(TObject *Sender);
   void __fastcall Example1DrawItem(TObject *Sender, TCanvas *ACanvas,
          TRect &ARect, bool Selected);
   void __fastcall FormCreate(TObject *Sender);

private:
   TColor MainMenuBackground;
   TColor MainMenuHighlightColor;
   TColor MainMenuTextColor;
   TColor MainMenuTextBackground;
   TColor MainMenuHighlightTextColor;
   TColor Custom;
   TColor VerticalColor;
   TColor MenuColor;
   TColor HighlightColor;
   TColor BorderColor;
   TColor NormalTextColor;
   TColor NormalTextBackground;
   TColor HighlightTextColor;
   TColor DisabledTextColor;
   int VerticalWidth;
   int FocusRectRightIndent;
   int FocusRectLeftIndent;
   int LeftTextPos;
   int SideBuffer;
   int MenuIncreaseWidth;
   int Offset;

   int MenuItemHeight;
   int ItemOffset;
   TIcon *Icon;
   Graphics::TBitmap *Image;

protected:
   void __fastcall MyExpandItemWidth(TObject *Sender, TCanvas *ACanvas,
      int &Width, int &Height);
   void __fastcall MyDrawItem(TObject* Sender, TCanvas* ACanvas,
   const TRect &ARect, bool Selected);

public:	
   __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


//*********Fichier .cpp*****************

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Example1DrawItem(TObject *Sender, TCanvas *ACanvas,
      TRect &ARect, bool Selected)
{
   TRect FocusRectBorder;
   TRect FocusRectFill;
   TMenuItem *MenuItem = ((TMenuItem*)Sender);

   AnsiString Text = MenuItem->Caption;

   ACanvas->Brush->Color = MainMenuBackground;
   ACanvas->FillRect(ARect);

   if(Text == "")
      return;

   if(Selected)
   {

      FocusRectBorder = ARect;
      ACanvas->Brush->Color = BorderColor;
      ACanvas->FrameRect(FocusRectBorder);


      FocusRectFill = ARect;
      FocusRectFill.Top += SideBuffer;
      FocusRectFill.Right -= SideBuffer;
      FocusRectFill.Left += SideBuffer;
      FocusRectFill.Bottom -= SideBuffer;
      ACanvas->Brush->Color = MainMenuHighlightColor;
      ACanvas->FillRect(FocusRectFill);

     ACanvas->Font->Color = MainMenuHighlightTextColor;
   }
   else
   {
      ACanvas->Font->Color = MainMenuTextColor;
   }


   int TextLength;
   TRect TextRect;

   TextLength = Text.Length();
   TextRect = ARect;

   TextRect.Left += 5;
   TextRect.Top += 1;

   DrawText(ACanvas->Handle,Text.c_str(), TextLength, &TextRect, 0);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::MyDrawItem(TObject* Sender, TCanvas* ACanvas,
   const TRect &ARect, bool Selected)
{
   int TopPos, TextLength;
   AnsiString Text;
   TRect TempRect;
   TRect VerticalRect;
   TRect FocusRectBorder;
   TRect FocusRectFill;
   TRect TextRect;

   TMenuItem *MenuItem = ((TMenuItem*)Sender);

   Text = MenuItem->Caption;

   ACanvas->Brush->Color = MenuColor;
   ACanvas->FillRect(ARect);


   if(Text==BLANK_LINE)
   {

      VerticalRect = ARect;
      VerticalRect.Top -= SideBuffer;
      VerticalRect.Right = VerticalWidth;
      VerticalRect.Bottom += SideBuffer;
      ACanvas->Brush->Color = VerticalColor;
      ACanvas->FillRect(VerticalRect);


      ACanvas->MoveTo(VerticalWidth,ARect.Top+ARect.Height()/2);
      ACanvas->LineTo(ARect.Right,ARect.Top+ARect.Height()/2);
      return;
   }


   TextLength = Text.Length();

   if(Selected)
   {
      VerticalRect = ARect;
      VerticalRect.Top -= SideBuffer;
      VerticalRect.Right = VerticalWidth;
      VerticalRect.Bottom += SideBuffer;
      ACanvas->Brush->Color = VerticalColor;
      ACanvas->FillRect(VerticalRect);

      if(MenuItem->Enabled)
      {
        FocusRectBorder = ARect;
         FocusRectBorder.Left += FocusRectLeftIndent - SideBuffer;
         FocusRectBorder.Right -= FocusRectRightIndent - SideBuffer;
         ACanvas->Brush->Color = BorderColor;
         ACanvas->FrameRect(FocusRectBorder);

         FocusRectFill = ARect;
         FocusRectFill.Right -= FocusRectRightIndent;
         FocusRectFill.Left += FocusRectLeftIndent;
         FocusRectFill.Bottom -= SideBuffer;
         FocusRectFill.Top += SideBuffer;
         ACanvas->Brush->Color = HighlightColor;
         ACanvas->FillRect(FocusRectFill);

         ACanvas->Font->Color = HighlightTextColor;
         ACanvas->Font->Style = TFontStyles() << fsBold;
      }
      else
      {

         ACanvas->Font->Style = TFontStyles();
         ACanvas->Brush->Color = NormalTextBackground;
         ACanvas->Font->Color = DisabledTextColor;
      }

   }
   else
   {

      VerticalRect = ARect;
      VerticalRect.Top -= SideBuffer;
      VerticalRect.Right = VerticalWidth;
      VerticalRect.Bottom += SideBuffer;
      ACanvas->Brush->Color = VerticalColor;
      ACanvas->FillRect(VerticalRect);

      if(MenuItem->Enabled)
      {
         ACanvas->Brush->Color = NormalTextBackground;
         ACanvas->Font->Color = NormalTextColor;
      }
      else
      {
         ACanvas->Brush->Color = NormalTextBackground;
         ACanvas->Font->Color = DisabledTextColor;
      }

   }

   TextRect = ARect;
   TextRect.Left += LeftTextPos;
   if(Offset > 0)
      TextRect.Top += Offset/2 + SideBuffer;
   else
      TextRect.Top += 2 + SideBuffer;

   TextRect.Top += SideBuffer;


   if(Menu->Images != NULL)
   {
      Icon = new TIcon();
      Menu->Images->GetIcon(MenuItem->ImageIndex,Icon);
      ACanvas->Draw(5,ARect.Top+ItemOffset+1,Icon);
      delete Icon;
   }


   DrawText(ACanvas->Handle,Text.c_str(), TextLength, &TextRect, 0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::MyExpandItemWidth(TObject *Sender,
   TCanvas *ACanvas, int &Width, int &Height)
{
   Width += MenuIncreaseWidth;
   Height += Offset;
   MenuItemHeight = Height;
   ItemOffset = Offset/2;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
//**************couleur du highlight 
   Custom=TColor(RGB(255,200,200));
   MainMenuBackground = clSilver;
   MainMenuHighlightColor = Custom;
   MainMenuTextColor = clBlack;
   MainMenuTextBackground = clSilver;
   MainMenuHighlightTextColor = clBlack;
   VerticalColor = clSilver;
   MenuColor = clWhite;
   HighlightColor = Custom;
   BorderColor = clBlack;
   NormalTextColor = clBlack;
   NormalTextBackground = clWhite;
   HighlightTextColor = clBlack;
   DisabledTextColor = clSilver;
   VerticalWidth = 26;
   FocusRectRightIndent = 3;
   FocusRectLeftIndent = 3;
   LeftTextPos = 35;
   SideBuffer = 1;

   if(Menu->Images == NULL)
      MenuIncreaseWidth = 100;
   else
      MenuIncreaseWidth = 50;

   Offset = 5;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Example1Click(TObject *Sender)
{
   TMenuItem *MenuItem = ((TMenuItem*)Sender);
   if(MenuItem->Count > 0)
   {
      for(int i=0; i <= MenuItem->Count-1; i++)
      {
         MenuItem->Items[i]->OnMeasureItem = MyExpandItemWidth;
         MenuItem->Items[i]->OnDrawItem = MyDrawItem;
         if(MenuItem->Items[i]->Count > 0)
         {
            for(int x=0; x <= MenuItem->Items[i]->Count-1; x++)
            {
               MenuItem->Items[i]->Items[x]->OnMeasureItem = MyExpandItemWidth;
               MenuItem->Items[i]->Items[x]->OnDrawItem = MyDrawItem;
            }
         }
      }
   }
}
//---------------------------------------------------------------------------



 

Conclusion

Crée sur Borland C++ Builder 5
 

Fichier Zip

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
  • Project1.bprTélécharger ce fichier [Réservé aux membres club]4 314 octets
  • Project1.cppTélécharger ce fichier [Réservé aux membres club]Voir ce fichier635 octets
  • Project1.exeTélécharger ce fichier [Réservé aux membres club]51 200 octets
  • Project1.resTélécharger ce fichier [Réservé aux membres club]876 octets
  • Unit1.cppTélécharger ce fichier [Réservé aux membres club]Voir ce fichier6 911 octets
  • Unit1.dfmTélécharger ce fichier [Réservé aux membres club]52 775 octets
  • Unit1.hTélécharger ce fichier [Réservé aux membres club]Voir ce fichier2 189 octets
  • Unit1.objTélécharger ce fichier [Réservé aux membres club]56 685 octets

Télécharger le zip

Commentaires et avis

signaler à un administrateur
Commentaire de victorcoasne le 18/08/2004 10:02:22

Cette aplication n'a pas pu démarrer manque VCL50.bpl

ou un truc comme ça

signaler à un administrateur
Commentaire de Dragonmaster le 29/09/2004 18:45:32

De même pour moi...

signaler à un administrateur
Commentaire de victorcoasne le 26/06/2005 21:45:30

Yaurait pas le même pour VC++ ou Dev-C++ (GCC)

signaler à un administrateur
Commentaire de tibob51 le 26/10/2006 22:59:43

VCL50.bpl est un fichier utilisé par borland,il se place dans le dossier systéme de windows.
Ansi qu'un autre dont je ne connait plus le nom, il sont utilisé si le prog tourne avec le vcl (équivalent du mfc).
Il existe un moyen de faire tourner un prog sans la nécessité
mais la taille de l'exe augmentera.(il faut regarder dans les option de compillation).
Sinon niveau expert c'est pas vraiment ça.
C'est encore loin de là.


Ajouter un commentaire



Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Comparez les prix Nouvelle version

Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,296 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.