線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2630
推到 Plurk!
推到 Facebook!

用ToolBar改變MainMenu字型大小,還有一個小問題

尚未結案
brook
資深會員


發表:57
回覆:323
積分:371
註冊:2002-07-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2015-04-07 09:09:37 IP:220.128.xxx.xxx 訂閱
當第一層的菜單有子菜單時,當按滑鼠會展開子菜單,滑鼠移到第二個菜單,也會展開子菜單,
可是當移到沒有子菜單的目錄時,再移回來時就不會自動展開子菜單,和沒有ToolBar時有小小的不同,
不知有沒有辦法解決?

[code cpp]
//---------------------------------------------------------------------------
#ifndef ToolBarMenuH
#define ToolBarMenuH
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp><br /> #include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMainMenu *MainMenu1;
TMenuItem *AA1;
TMenuItem *BB1;
TMenuItem *AAA1;
TMenuItem *BBB1;
TMenuItem *CCC1;
TButton *Button1;
TMenuItem *BBB2;
TMenuItem *BBB3;
TToolBar *ToolBar1;
TImageList *ImageList1;
TFontDialog *FontDialog1;
TMenuItem *CC1;
void __fastcall MyDrawItem(TObject *Sender,TCanvas *ACanvas,const TRect &ARect, bool Selected);
void __fastcall MyMeasureItem(TObject *Sender, TCanvas *ACanvas, int &Width, int &Height);
void __fastcall Button1Click(TObject *Sender);

private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "ToolBarMenu.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void vSetMenuItem(TMenuItem *MenuItem, TMenuMeasureItemEvent MyMeasureItem, TMenuDrawItemEvent MyDrawItem)
{
//设置每一项菜单。
if(MenuItem->Count > 0)
{
for(int i=0; i < MenuItem->Count; i )
{
//设置每项菜单高度和宽度回调函数。
MenuItem->Items[i]->OnMeasureItem = MyMeasureItem;
//设置每项菜单自画回调函数。
MenuItem->Items[i]->OnDrawItem = MyDrawItem;
//设置下一级字菜单。
if(MenuItem->Items[i]->Count > 0) vSetMenuItem(MenuItem->Items[i],MyMeasureItem,MyDrawItem);
}
MenuItem->GetInterfaceTable();
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
MainMenu1->Images = ImageList1; //沒設第2層不會改字型大小
vSetMenuItem(ToolBar1->Menu->Items,MyMeasureItem,MyDrawItem);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MyDrawItem(TObject *Sender,TCanvas *ACanvas,const TRect &ARect, bool Selected)
{
HFONT OldFont;
TMenuItem *MenuItem = ((TMenuItem*)Sender);
HDC dc = ACanvas->Handle;
if(MenuItem->Caption=="-")
{
// 画一条分隔直线。
ACanvas->MoveTo(ARect.Left,ARect.Top ARect.Height()/2);
ACanvas->LineTo(ARect.Right,ARect.Top ARect.Height()/2);
return;
}
//如果菜单有效。
if(!MenuItem->Enabled) SetTextColor (dc, GetSysColor(COLOR_GRAYTEXT));
// if(MenuItem->Enabled) SetTextColor (dc, GetSysColor(COLOR_MENUTEXT));
// else SetTextColor (dc, GetSysColor(COLOR_GRAYTEXT));
OldFont = (HFONT)SelectObject(dc, ToolBar1->Font->Handle);
SetBkMode (dc, TRANSPARENT);
// DrawText (dc, "AAA", 3,&ARect, DT_TOP | DT_SINGLELINE | DT_EXPANDTABS);
// SetTextColor (dc, ToolBar1->Font->Color);
DrawTextW(ACanvas->Handle,MenuItem->Caption.c_str(), MenuItem->Caption.Length(), &ARect, DT_CENTER);
SelectObject (dc, OldFont);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MyMeasureItem(TObject *Sender, TCanvas *ACanvas, int &Width,
int &Height)
{
HFONT OldFont;
TMenuItem *MenuItem = ((TMenuItem*)Sender);
OldFont = (HFONT)SelectObject(ACanvas->Handle, ToolBar1->Font->Handle);
Width = ACanvas->TextWidth(MenuItem->Caption) 8;
Height = ACanvas->TextHeight(MenuItem->Caption) 2;
SelectObject(ACanvas->Handle, OldFont);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (FontDialog1->Execute ())
{
ToolBar1->Font->Assign(FontDialog1->Font);
}
}
//---------------------------------------------------------------------------
[/code]

系統時間:2024-04-20 10:01:57
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!