terence_lee
初階會員
發表:62 回覆:82 積分:28 註冊:2004-07-15
發送簡訊給我
|
各位先進好:
想請問PopupMenu是否能夠在左邊載入圖片呢?就是類似window系統的點選"開始"功能表時,會出現window 2000的logo的功能?謝謝大大們的指教.
|
nlj859
資深會員
發表:139 回覆:375 積分:322 註冊:2004-03-20
發送簡訊給我
|
Hello terence_lee, 在PopupMenu和MainMenu裡面的Item,都會有一個屬性Bitmap,可以讓你放上.bmp的小圖片.試試看吧.
|
m8815010
版主
發表:99 回覆:372 積分:289 註冊:2003-11-13
發送簡訊給我
|
引言:
各位先進好:
想請問PopupMenu是否能夠在左邊載入圖片呢?就是類似window系統的點選"開始"功能表時,會出現window 2000的logo的功能?謝謝大大們的指教.
terence_lee你好: 最方便的方式應該誠如 class="code">
In Unit.h
~~
class TForm1 : public TForm
{
__published: // IDE-managed Components
TPopupMenu *PopupMenu1; <--Add
void __fastcall FormCreate(TObject *Sender); <--Add
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall MenuItemDraw(TObject *Sender, TCanvas *ACanvas, <--Add
const TRect &ARect, bool Selected);
void __fastcall MenuItemMeasureItem(TObject *Sender, TCanvas *ACanvas, <--Add
int &Width, int &Height);
};
In Unit.cpp
~~
TForm1 *Form1; //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TMenuItem* Itm; for (int i=1;i<=8;i ) {
Itm=new TMenuItem(PopupMenu1);
PopupMenu1->Items->Add(Itm);
Itm->Caption="星期" IntToStr(i);
Itm->OnDrawItem=MenuItemDraw;
} PopupMenu1->Items->Items[0]->OnMeasureItem=MenuItemMeasureItem;
PopupMenu1->OwnerDraw=true; this->PopupMenu=PopupMenu1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MenuItemDraw(TObject *Sender, TCanvas *ACanvas,
const TRect &ARect, bool Selected)
{
TMenuItem* Itm=(TMenuItem*)Sender; if (Itm->MenuIndex==0) {
Graphics::TBitmap *bmp=new Graphics::TBitmap();
bmp->LoadFromFile("C:\\winxp.bmp");
ACanvas->Draw(1,1,bmp);
delete bmp;
} ACanvas->TextOut(ARect.left 20,ARect.top 4,Itm->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MenuItemMeasureItem(TObject *Sender, TCanvas *ACanvas,
int &Width, int &Height)
{
Width =20;
}
sample result: 注意事項
1. 圖像的長度要注意和總MenuItem高度和差不多,看起來才剛剛好!
2. MenuItem的Caption要往後印一些,才不會重疊到圖像
3. 注意會有〝星期1(&1)〞的情況,即〝&〞這個char會被直接印出,這是用OwnerDraw的正常現像,覺得不妥應該可自行處理之
4. 我用的小圖是18x148 dimension
All,! 發表人 -
|
terence_lee
初階會員
發表:62 回覆:82 積分:28 註冊:2004-07-15
發送簡訊給我
|
謝謝大大們的回應:
想請問如果小弟的PopupMenu已經由程式創建了,不是在程式中建的,那麼要如何修改才可以達到m8815010所說的效果呢..小弟改了一下,不過全部PopupMenu caption都不見了!_! 而3. 注意會有〝星期1(&1)〞的情況,即〝&〞這個char會被直接印出,這是用OwnerDraw的正常現像,覺得不妥應該可自行處理之 那麼要如何處理&的符號使它消失呢?
|
axsoft
版主
發表:681 回覆:1056 積分:969 註冊:2002-03-13
發送簡訊給我
|
terence_lee您好:
其實用文字的方法也是不錯的選擇,參考下面的文章及範例 src="http://bdn.borland.com/article/images/26632/example.gif"> 範例下載: http://delphi.ktop.com.tw/loadfile.php?TOPICID=17865740&CC=399560 Unit1.CPP
//--------------------------------------------------------------------------- #include
#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; // Color the background behind the text
ACanvas->Brush->Color = MainMenuBackground;
ACanvas->FillRect(ARect); if(Text == "")
return; if(Selected)
{
// Draw the outline of the selection box
FocusRectBorder = ARect;
ACanvas->Brush->Color = BorderColor;
ACanvas->FrameRect(FocusRectBorder); // Draw the inside of the selection box
// Make is a little smaller so it does not erase the outline
FocusRectFill = ARect;
FocusRectFill.Top += SideBuffer;
FocusRectFill.Right -= SideBuffer;
FocusRectFill.Left += SideBuffer;
FocusRectFill.Bottom -= SideBuffer;
ACanvas->Brush->Color = MainMenuHighlightColor;
ACanvas->FillRect(FocusRectFill); // Set the color that we want our text drawn when the item is selected
ACanvas->Font->Color = MainMenuHighlightTextColor;
}
else
{
ACanvas->Font->Color = MainMenuTextColor;
} int TextLength;
TRect TextRect; TextLength = Text.Length();
TextRect = ARect;
// This determines where the text is drawn.
TextRect.Left += 5;
TextRect.Top += 1; // Draw the text
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; // Draw the Background erases anything that was there before.
ACanvas->Brush->Color = MenuColor;
ACanvas->FillRect(ARect); // Draw any seperator lines
if(Text==BLANK_LINE)
{
// Draw the Vertical Bar
VerticalRect = ARect;
VerticalRect.Top -= SideBuffer;
VerticalRect.Right = VerticalWidth;
VerticalRect.Bottom += SideBuffer;
ACanvas->Brush->Color = VerticalColor;
ACanvas->FillRect(VerticalRect); // Draw the Blank Line
ACanvas->MoveTo(VerticalWidth,ARect.Top+ARect.Height()/2);
ACanvas->LineTo(ARect.Right,ARect.Top+ARect.Height()/2);
return;
} // This is for non seperator lines TextLength = Text.Length(); if(Selected)
{
// Have to draw the vertical bar section to fill in the area that is not
// covered by the selection rect
VerticalRect = ARect;
VerticalRect.Top -= SideBuffer;
VerticalRect.Right = VerticalWidth;
VerticalRect.Bottom += SideBuffer;
ACanvas->Brush->Color = VerticalColor;
ACanvas->FillRect(VerticalRect); if(MenuItem->Enabled)
{
// The item is selected and enabled // Draw the focus rect outline border
FocusRectBorder = ARect;
FocusRectBorder.Left += FocusRectLeftIndent - SideBuffer;
FocusRectBorder.Right -= FocusRectRightIndent - SideBuffer;
ACanvas->Brush->Color = BorderColor;
ACanvas->FrameRect(FocusRectBorder); // Fill in the focus rect. Making it a little smaller so as to not
// draw over the outline
FocusRectFill = ARect;
FocusRectFill.Right -= FocusRectRightIndent;
FocusRectFill.Left += FocusRectLeftIndent;
FocusRectFill.Bottom -= SideBuffer;
FocusRectFill.Top += SideBuffer;
ACanvas->Brush->Color = HighlightColor;
ACanvas->FillRect(FocusRectFill); // Set the way we want to draw our text
ACanvas->Font->Color = HighlightTextColor;
ACanvas->Font->Style = TFontStyles() << fsBold;
}
else
{
// Menu item is not enabled so do not draw any selection
// rect and change the text color to reflect its unenabled state
// NOTE: The icon is still drawn normally and not disabled
ACanvas->Font->Style = TFontStyles();
ACanvas->Brush->Color = NormalTextBackground;
ACanvas->Font->Color = DisabledTextColor;
} }
else
{
// Fill in the vertical area
VerticalRect = ARect;
VerticalRect.Top -= SideBuffer;
VerticalRect.Right = VerticalWidth;
VerticalRect.Bottom += SideBuffer;
ACanvas->Brush->Color = VerticalColor;
ACanvas->FillRect(VerticalRect); // Set the text background color and font based on if it is enabled or not
if(MenuItem->Enabled)
{
ACanvas->Brush->Color = NormalTextBackground;
ACanvas->Font->Color = NormalTextColor;
}
else
{
ACanvas->Brush->Color = NormalTextBackground;
ACanvas->Font->Color = DisabledTextColor;
} } // Calculate out the Rect we want to draw our text in
TextRect = ARect;
TextRect.Left += LeftTextPos;
if(Offset > 0)
TextRect.Top += Offset/2 + SideBuffer;
else
TextRect.Top += 2 + SideBuffer; TextRect.Top += SideBuffer; // Draw any menu item icons
if(Menu->Images != NULL)
{
Icon = new TIcon();
Menu->Images->GetIcon(MenuItem->ImageIndex,Icon);
ACanvas->Draw(5,ARect.Top+ItemOffset+1,Icon);
delete Icon;
} // Draw the text
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)
{
Custom=TColor(RGB(185,239,245));
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;
}
}
}
}
}
//--------------------------------------------------------------------------- Unit1.H //--------------------------------------------------------------------------- #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
#include
//---------------------------------------------------------------------------
const AnsiString BLANK_LINE="-"; class TForm1 : public TForm
{
__published: // IDE-managed Components
TMainMenu *MainMenu1;
TMenuItem *Example1;
TMenuItem *MenuItemOne1;
TMenuItem *objectMenuItemOne1TMenuItem1;
TMenuItem *MenuItemThree1;
TMenuItem *MenuItemFour1;
TMenuItem *MenuItemFive1;
TMenuItem *MenuItemSix1;
TMenuItem *MenuItemSeven1;
TMenuItem *MenuItemEight1;
TMenuItem *MenuItemNine1;
TMenuItem *MenuItemTen1;
TMenuItem *SubMenuItemExample1;
TMenuItem *SubMenuOne1;
TMenuItem *SubMenuTwo1;
TMenuItem *SubMenuThree1;
TMenuItem *SubMenuFour1;
TMenuItem *SubMenuFive1;
TImageList *ImageList1;
TMenuItem *N1;
TMenuItem *N2;
void __fastcall Example1Click(TObject *Sender);
void __fastcall Example1DrawItem(TObject *Sender, TCanvas *ACanvas,
TRect &ARect, bool Selected);
void __fastcall FormCreate(TObject *Sender); private: // User declarations
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: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif /*生活是一種藝術,用心生活享受生活*/ 發表人 - axsoft 於 2004/09/30 16:57:43
|
m8815010
版主
發表:99 回覆:372 積分:289 註冊:2003-11-13
發送簡訊給我
|
引言:
謝謝大大們的回應:
想請問如果小弟的PopupMenu已經由程式創建了,不是在程式中建的,那麼要如何修改才可以達到m8815010所說的效果呢..小弟改了一下,不過全部PopupMenu caption都不見了!_! 而[red]3. 注意會有〝星期1(&1)〞的情況,即〝&〞這個char會被直接印出,這是用OwnerDraw的正常現像,覺得不妥應該可自行處理之 那麼要如何處理&的符號使它消失呢?
terence_lee你好: 嗯,你說的應該也沒問題才對! 首先我範例的>> 只要多加這一行即可,剩下的步驟只要將範例程式中原本的> >!
|
terence_lee
初階會員
發表:62 回覆:82 積分:28 註冊:2004-07-15
發送簡訊給我
|
再次謝謝大大們的熱列回應:
小弟正在努力測試中,謝謝大家!
|