日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

用Delphi完成自定義顏色對話框及其構(gòu)件

用Delphi完成自定義顏色對話框及其構(gòu)件

更新時(shí)間:2022-04-29 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

在 開 發(fā) 證 券 分 析 軟 件 中, 經(jīng) 常 要 繪 制 各 種 股 票 的 分 析 曲 線。 為 了 使 得 軟
件 的 功 能 更 加 方 便. 靈 活, 用 戶 希 望 能 夠 按 照 自 己 的 喜 好 自 定 義 各 種 曲 線 的
顏 色。 在 W O R D97 的[ 格 式] 菜 單 下 的 字 體 對 話 框 中 有 類 似 的 功 能。 當(dāng) 用 戶
單 擊 字 體 對 話 框 中 的 顏 色 下 拉 框 時(shí), 各 種 顏 色 的 簡 單 圖 案 和 字 體 的 顏 色 名
稱 一 起 顯 示 出 來, 這 樣 處 理 的 結(jié) 果 顯 然 比 只 提 供 一 個(gè) 裝 有 顏 色 名 稱 的 下 拉
框 效 果 要 好 的 多。

---- 一、 自 定 義 顏 色 對 話 框 的 實(shí) 現(xiàn)

---- 在Delphi 中, 我 們 可 以 使 用TComboBox 實(shí) 現(xiàn) 類 似 的 功 能。 在TcomboBox 構(gòu) 件 中 有 一
個(gè)Style 屬 性, 決 定TcomboBox 的 顯 示 屬 性。 通 常 可 選 取csDropDown, csSimple, csDropDownList,
csOwnerDrawFixed, csOwnerDrawVariable 等。 其 中 當(dāng) 選 取csOwnerDrawFixed 時(shí) 表 示 創(chuàng) 建 一 個(gè) 自
畫 下 拉 框, 下 拉 框 的 每 一 項(xiàng) 的 高 度 由ItemHeight 屬 性 決 定。 并 且 必 須 在TcomboBox
的OnDrawItem 事 件 中 響 應(yīng) 自 畫 過 程。 OnDrawItem 的 定 義 為:

property OnDrawItem: TDrawItemEvent;
TDrawItemEvent = procedure
(Control: TWinControl; Index: Integer Rect:
TRect; State: TOwnerDrawState) of object;
其 中 的 三 個(gè) 參 數(shù) 的 含 義 為:
Control:   包 含 下 拉 框 的TComboBox
Index: 自 畫 的 下 拉 框 在
TComboBox 的Items 屬 性 中 的 索 引 號(hào)
Rect: 自 畫 的 位 置
---- 因 此, 知 道 了 需 要 自 畫 的 矩 形 的 位 置(Rect 參 數(shù)) 和 在TComboBox 中 的 索 引 號(hào)
(Index 參 數(shù)), 我 們 可 以 使 用TcomboBox 的Canvas 屬 性 在 其 畫 布 上 自 畫。

---- 具 體 的 實(shí) 現(xiàn) 過 程 如 下:

---- 1 . 新 建 一 個(gè) 工 程 文 件, 設(shè) 置 其 默 認(rèn) 窗 體 的 有 關(guān) 屬 性 為:

---- Caption 自 定 義 下 拉 框

---- Name Form1

---- Position poScreenCenter

---- 2 . 在 窗 體 中 放 置 兩 個(gè)TcomboBox 構(gòu) 件, 設(shè) 置 其 屬 性 如 下:

---- Name Style ItemHeight OnDrawItem

---- ColorCombo1 csOwnerDrawFixed 20 ColorComboDrawItem

---- ColorCombo2 csOwnerDrawFixed 30 ColorComboDrawItem

---- 3 . 雙 擊ColorCombo1 和ColorCombo2 的Items 屬 性 旁 的 圓 點(diǎn) 按 紐, 在"String List Editor" 對 話 框
中 輸 入

---- 黑 色

---- 藍(lán) 色

---- 藍(lán) 綠

---- 鮮 綠

---- 紅 色

---- 黃 色

---- 等 各 種 顏 色 的 名 稱

---- 4 . 在ColorCombo1 的OnDrawItem 事 件 中 加 入 如 下 代 碼

procedure TForm1.ColorComboDrawItem
(Control: TWinControl; Index: Integer;
Rect: TRect; State: OwnerDrawState);
var
TempColor :TColor; // 自 畫 顏 色
TempBrushColor :TColor; // 臨 時(shí) 顏 色
begin
with (Control as TComboBox) do
// 在Combo 的Canvas 上 自 畫
begin
TempBrushColor:=Canvas.Brush.Color;
// 保 存 原 來 的 的 顏 色
Canvas.FillRect(Rect);
case Index of // 根 據(jù)Index 的 不 同,
定 義 不 同 自 畫 的 顏 色
0: // 黑 色
TempColor:=clBlack;
1: // 藍(lán) 色
TempColor:=clBlue;
2: // 藍(lán) 綠
TempColor:=clAqua;
3: // 鮮 綠
TempColor:=clLime;
4: // 紅 色
TempColor:=clRed;
5: // 黃 色
                           TempColor:=clyellow;
// 可 以 在 此 加 入 對 其 它 顏 色 的 響 應(yīng)
end;

Canvas.Brush.Color:=TempColor;
// 自 畫 顏 色 矩 形
Canvas.Rectangle(Rect.Left+4,
Rect.Top+1,
(Rect.Right+Rect.Left) div 3,
Rect.Bottom-1);
Canvas.Brush.Color:=TempBrushColor;
// 顯 示 與 顏 色 對 應(yīng) 的 字 符 串
Canvas.TextOut((Rect.Left+Rect.Right) div 2,
Rect.Top+1,
Items[Index]);
end;
end;
---- 5 . 保 存, 運(yùn) 行 文 件, 我 們 可 以 看 到 和 W O R D 中 顏 色 下 拉 框 相 同 的 效 果
---- 有 興 趣 的 讀 者, 可 以 在 文 中 所 示 的 位 置 加 入 對 其 它 顏 色 處 理。

---- 以 上 程 序 在Delphi 3.0,4.0 上 通 過。

---- 二、 自 定 義 顏 色 對 話 框 構(gòu) 件 的 編 寫

---- 對 許 多Delphi 程 序 員 來 說, 如 何 編 寫 自 己 的Delphi 構(gòu) 件 還 是 比 較 陌 生 的, Delphi
構(gòu) 件 實(shí) 際 上 是 從Tcomponent 類 繼 承 發(fā) 展 而 來, 編 寫 構(gòu) 件 實(shí) 際 就 是 編 寫 特 殊 的
類。 下 面 我 們 就 以 自 定 義 顏 色 對 話 框 為 例 介 紹 構(gòu) 件 的 編 寫。

---- 下 面TColorComboBox 是 從TcomboBox 類 繼 承 來 的, 當(dāng) 點(diǎn) 擊 右 邊 的 下 拉 箭 頭 時(shí) 彈 出
和 下 拉items 對 應(yīng) 的 各 種 顏 色 自 畫 框。

---- 1. 選 中Component 菜 單 項(xiàng) 中 的New Component 選 項(xiàng)。 在Ancestor Type 框 中 選TcomboBox, 在Class
Name 框 中 填 入TColorComboBox, 在Palette Page 框 中 選Samples, 在Unit File Name 框 中 填 入
ColorComboBox.pas, 然 后 點(diǎn) 擊OK 按 鈕。

---- 2. 選 中Component 菜 單 項(xiàng) 中 的Install Component 選 項(xiàng), 點(diǎn) 擊Into new package, 在package name 框 中 寫
入 路 徑 和ColorComboDpk.dpk, 點(diǎn) 擊ok, 生 成ColorComboDpk.bpl 文 件。

---- 3. 使 用Tools 菜 單 中 的Image Editor 來 創(chuàng) 建 編 輯 文 件ColorComBox.dcr, 為TColorComboBox 類 建
立 位 圖。

---- 4. 在Create 中 加 入 對 字 體 大 小 高 度 的 規(guī) 定 及 對 控 件 的Style 屬 性( 設(shè) 成
csOwnerDrawFixed) 的 規(guī) 定, 在Create 后 執(zhí) 行 的CreateWnd 中 初 始 化 顏 色 的items, 如 果 不 需 要
那 么 多 顏 色 項(xiàng), 可 以 以 后 在 生 成 控 件 的items 屬 性 中 直 接 刪 除 不 需 要 的 顏 色。

---- 5. 在DrawItem 事 件 中 加 入 顏 色 自 畫 程 序, 此 事 件 在On DrawItem 之 前 發(fā) 生。

---- 實(shí) 現(xiàn) 程 序 如 下:

unit ColorComboBox;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TColorComboBox = class(TComboBox)
private
{ Private declarations }
FOnDrawItem : TDrawItemEvent;
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);override;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner : TComponent);override;
procedure CreateWnd;override;
published
{ Published declarations }
property OnDrawItem : TDrawItemEvent
Read FOnDrawItem write FOnDrawItem;
end;
procedure Register;

implementation

procedure Register; // 注 冊 構(gòu) 件
begin
RegisterComponents('Samples', [TColorComboBox]);
end;

constructor TColorComboBox.Create
(AOwner : TComponent); // 構(gòu) 件 的 初 始 化
begin
inherited Create(AOwner);
Style := csOwnerDrawFixed; // 構(gòu) 件 的 初 始 類 型
ItemHeight := 20;
Font.Size := 10;
end;

procedure TColorComboBox.CreateWnd;
// 顏 色 構(gòu) 件 的Items 屬 性 初 始 化
begin
inherited CreateWnd;
Items.Clear;
Items.Add(' 黑 色');
Items.Add(' 藍(lán) 色');
Items.Add(' 藍(lán) 綠');
Items.Add(' 鮮 綠');
Items.Add(' 粉 紅');
Items.Add(' 紅 色');
Items.Add(' 黃 色');
Items.Add(' 白 色');
Items.Add(' 深 藍(lán)');
Items.Add(' 青 色');
Items.Add(' 綠 色');
Items.Add(' 紫 色');
Items.Add(' 深 紅');
Items.Add(' 深 黃');
Items.Add(' 深 灰');
Items.Add(' 銀 色');
---- // 若 不 需 要 這 么 多 顏 色 可 在 構(gòu) 件 的items 屬 性 中 刪 除 不 需 要 的 顏 色

---- end;

---- // 重 載DrawItem 過 程

procedure TColorComboBox.DrawItem
(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
var
TempColor :TColor; // 自 畫 顏 色
TempBrushColor :TColor; // 臨 時(shí) 顏 色
begin // 本 構(gòu) 件 的 默 認(rèn) 自 畫 設(shè) 置
TempBrushColor:=Canvas.Brush.Color;
// 保 存 原 來 的 的 顏 色
Canvas.FillRect(Rect);

if Items[index]=' 黑 色' then
TempColor := clBlack
else if Items[index]=' 藍(lán) 色' then
TempColor := clBlue
else if Items[index]=' 藍(lán) 綠' then
TempColor := clAqua
else if Items[index]=' 鮮 綠' then
TempColor := clLime
else if Items[index]=' 粉 紅' then
TempColor := clFuchsia
else if Items[index]=' 紅 色' then
TempColor := clRed
else if Items[index]=' 黃 色' then
TempColor := clYellow
else if Items[index]=' 白 色' then
TempColor := clWhite
else if Items[index]=' 深 藍(lán)' then
TempColor := clNavy
else if Items[index]=' 青 色' then
TempColor := clTeal
else if Items[index]=' 綠 色' then
TempColor := clGreen
else if Items[index]=' 紫 色' then
TempColor := clPurple
else if Items[index]=' 深 紅' then
TempColor := clMaroon
else if Items[index]=' 深 黃' then
TempColor := clOlive
else if Items[index]= ' 深 灰' then
TempColor := clGray
else if Items[index]=' 銀 色' then
else TempColor := clSilver;

Canvas.Brush.Color:=TempColor;
// 自 畫 顏 色 矩 形
Canvas.Rectangle(Rect.Left+4,
Rect.Top+1,
(Rect.Right+Rect.Left) div 3,
Rect.Bottom-1);
Canvas.Brush.Color:=TempBrushColor;
// 顯 示 與 顏 色 對 應(yīng) 的 字 符 串
Canvas.TextOut((Rect.Left+Rect.Right) div 2,
Rect.Top+1,
Items[Index]);
end;
end.
---- 此 控 件 可 以 在 所 有 需 要 顏 色 選 項(xiàng) 的 程 序 中 使 用 而 且 非 常 方 便 和 美 觀, 并
且 使 編 程 節(jié) 省 很 多 時(shí) 間, 增 加 了 程 序 可 靠 性 和 可 讀 性。

---- 三、 自 定 義 顏 色 對 話 框 構(gòu) 件 的 使 用

---- 當(dāng) 注 冊 完 自 定 義 顏 色 構(gòu) 件 后, 可 以 從Delphi 構(gòu) 件 模 板 的Sample 頁 中 選 擇 自 定 義
顏 色 構(gòu) 件, 和 使 用Delphi 本 身 構(gòu) 件 沒 有 區(qū) 別。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

主站蜘蛛池模板: 国产在线精彩视频 | 欧美日韩国产一区二区三区播放 | 国产精品视频全国免费观看 | 日本亚洲精品色婷婷在线影院 | 国产精品久久久久久久久久久威 | 久久日本经典片免费看 | 欧美精品国产日韩综合在线 | 91日韩视频在线观看 | 狠狠色丁香久久婷婷综合五月 | 日本zzzwww | 大陆精品自在线拍国语 | 国产欧美精品综合一区 | 成人小视频在线观看免费 | 日韩资源站 | 国产永久免费爽视频在线 | 手机在线看片不卡中文字幕 | 日本免费不卡视频一区二区三区 | 色天天干 | 婷婷久久爱www | 久久久久777777人人人视频 | 日日爽天天干 | 亚洲精品在线网 | 日本三级三级三级免费看 | 国产区视频| www.xxxx日本| 免费看国产片 | 亚洲欧美日韩中文综合在线不卡 | 伊人9999 | 日韩精品一区二区三区在线观看l | 亚洲手机中文字幕 | 欧美成人性视频播放 | 伊色综合久久之综合久久 | 中文字幕一区日韩在线视频 | 色老头综合免费视频 | 在线国产三级 | 午夜特级毛片 | 亚洲精品午夜久久aaa级久久久 | 91亚洲视频在线观看 | 欧美人善zozσ性伦交 | 久久99国产精品亚洲 | 欧美猛操 |