|
Trouver une ressource
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
Description
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 !
Télécharger le zip
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Comparez les prix Nouvelle version
|