⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 用delphi实现自定义颜色对话框及 其构件.htm

📁 对于学习很有帮助
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Welcome to Delphi Tips !</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<link rel="stylesheet" href="../tips.css"></head>

<body bgcolor="#D8D0C8" text="#675350">
<div align="left"> 
  <table border="1" cellpadding="0" cellspacing="0" width="579" height="63" bordercolorlight="#784400" bordercolordark="#D8D0C8">
    <tr>
      <td width="194" height="86"><img src="../images/title.gif" width="340" height="85"></td>
      <td width="377" height="86" valign="top"><img src="../images/dong.jpg" width="251" height="85"> 
	&nbsp;&nbsp;</td>
  </tr>
</table>
  <br>
</div>
<div align="left">

  <table border="1" cellpadding="0" cellspacing="0" width="578" height="18" bordercolorlight="#784400" bordercolordark="#CCCCCC" class="p2">
    <tr bgcolor="#B0A498"> 
      <td width="574" height="18"> &gt;<a href="../index.htm">DelphiTips首页</a> 
	&gt; <a href="main.htm">控件应用</a></td>
  </tr>
</table>
  <br>
  <table width="96%" border="1" bordercolorlight="#784400" cellspacing="0" bordercolordark="#D8D0C8">
    <tr bgcolor="#B0A498" class="p1"> 
      <td colspan="3"> 
	<div align="center">用Delphi实现自定义颜色对话框及 其构件</div>
      </td>
      <td width="22%" class="p2"> 
	<div align="right">&lt;&lt;<a href="007.htm">上一篇</a></div>
      </td>
      <td width="25%" class="p2"> 
	<div align="right"><a href="009.htm">下一篇</a>&gt;&gt;</div>
      </td>
    </tr>
    <tr class="p2"> 
      <td colspan="5"> 
	<table width="100%" border="0" class="p2">
	  <tr>
	    <td width="3%">&nbsp;</td>
	    <td width="94%">
	      <p class="p2">太原理工大学电机系胡晓鹰<br>
		武汉水利电力大学计算中心陈谦<br>
		----在开发证券分析软件中,经常要绘制各种股票的分析曲线。为了使得软件的功能更加方便.灵活,用户希望能够按照自己的喜好自定义各种曲线的颜色。在WORD97的[格式]菜单下的字体对话框中有类似的功能。当用户单击字体对话框中的颜色下拉框时,各种颜色的简单图案和字体的颜色名称一起显示出来,这样处理的结果显然比只提供一个装有颜色名称的下拉框效果要好的多。<br>
		<br>
		----一、自定义颜色对话框的实现<br>
		<br>
		----在Delphi中,我们可以使用TComboBox实现类似的功能。在TcomboBox构件中有一个Style属性,决定TcomboBox的显示属性。通常可选取csDropDown,csSimple,csDropDownList,csOwnerDrawFixed,csOwnerDrawVariable等。其中当选取csOwnerDrawFixed时表示创建一个自画下拉框,下拉框的每一项的高度由ItemHeight属性决定。并且必须在TcomboBox的OnDrawItem事件中响应自画过程。OnDrawItem的定义为:<br>
		<br>
		property OnDrawItem:TDrawItemEvent;<br>
		TDrawItemEvent=procedure(Control:TWinControl;Index:IntegerRect:TRect;State:TOwnerDrawState) 
		of object;<br>
		&nbsp;&nbsp;&nbsp; 其中的三个参数的含义为:<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Control: 包含下拉框的TComboBox<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Index:自画的下拉框在<br>
		TComboBox的Items属性中的索引号<br>
		&nbsp;&nbsp;&nbsp; Rect:自画的位置<br>
		----因此,知道了需要自画的矩形的位置(Rect参数)和在TComboBox中的索引号(Index参数),我们可以使用TcomboBox的Canvas属性在其画布上自画。<br>
		<br>
		----具体的实现过程如下:<br>
		----1.新建一个工程文件,设置其默认窗体的有关属性为:<br>
		----Caption自定义下拉框<br>
		----NameForm1<br>
		----PositionpoScreenCenter<br>
		----2.在窗体中放置两个TcomboBox构件,设置其属性如下:<br>
		----Name Style ItemHeight OnDrawItem<br>
		----ColorCombo1 csOwnerDrawFixed 20 ColorComboDrawItem<br>
		----ColorCombo2 csOwnerDrawFixed 30 ColorComboDrawItem<br>
		----3.双击ColorCombo1和ColorCombo2的Items属性旁的圆点按纽,在&quot;StringListEditor&quot;对话框中输入<br>
		----黑色<br>
		----蓝色<br>
		----蓝绿<br>
		----鲜绿<br>
		----红色<br>
		----黄色<br>
		----等各种颜色的名称<br>
		----4.在ColorCombo1的OnDrawItem事件中加入如下代码<br>
		procedure TForm1.ColorComboDrawItem(Control:TWinControl;Index:Integer;Rect:TRect;State:OwnerDrawState);<br>
		var<br>
		&nbsp;&nbsp;&nbsp; TempColor&nbsp;&nbsp;&nbsp; :TColor;//自画颜色<br>
		&nbsp;&nbsp;&nbsp; TempBrushColor:TColor;//临时颜色<br>
		begin<br>
		&nbsp;&nbsp;&nbsp; with (ControlasTComboBox) do<br>
		//在Combo的Canvas上自画<br>
		&nbsp;&nbsp;&nbsp; begin<br>
		&nbsp;&nbsp;&nbsp; TempBrushColor:=Canvas.Brush.Color;<br>
		//保存原来的的颜色<br>
		&nbsp;&nbsp;&nbsp; Canvas.FillRect(Rect);<br>
		&nbsp;&nbsp;&nbsp; case Index of//根据Index的不同,定义不同自画的颜色<br>
		0://黑色<br>
		TempColor:=clBlack;<br>
		1://蓝色<br>
		TempColor:=clBlue;<br>
		2://蓝绿<br>
		TempColor:=clAqua;<br>
		3://鲜绿<br>
		TempColor:=clLime;<br>
		4://红色<br>
		TempColor:=clRed;<br>
		5://黄色<br>
		TempColor:=clyellow;<br>
		//可以在此加入对其它颜色的响应<br>
		end;<br>
		<br>
		Canvas.Brush.Color:=TempColor;<br>
		//自画颜色矩形<br>
		Canvas.Rectangle(Rect.Left+4,Rect.Top+1,(Rect.Right+Rect.Left)div3,Rect.Bottom-1);<br>
		Canvas.Brush.Color:=TempBrushColor;<br>
		//显示与颜色对应的字符串<br>
		Canvas.TextOut((Rect.Left+Rect.Right)div2,Rect.Top+1,Items[Index]);<br>
		end;<br>
		end;<br>
		----5.保存,运行文件,我们可以看到和WORD中颜色下拉框相同的效果<br>
		----有兴趣的读者,可以在文中所示的位置加入对其它颜色处理。<br>
		----以上程序在Delphi3.0,4.0上通过。<br>
		----二、自定义颜色对话框构件的编写<br>
		----对许多Delphi程序员来说,如何编写自己的Delphi构件还是比较陌生的,Delphi构件实际上是从Tcomponent类继承发展而来,编写构件实际就是编写特殊的类。下面我们就以自定义颜色对话框为例介绍构件的编写。<br>
		<br>
		----下面TColorComboBox是从TcomboBox类继承来的,当点击右边的下拉箭头时弹出和下拉items对应的各种颜色自画框。<br>
		<br>
		----1.选中Component菜单项中的NewComponent选项。在AncestorType框中选TcomboBox,在ClassName框中填入TColorComboBox,在PalettePage框中选Samples,在UnitFileName框中填入ColorComboBox.pas,然后点击OK按钮。<br>
		<br>
		----2.选中Component菜单项中的InstallComponent选项,点击Intonewpackage,在packagename框中写入路径和ColorComboDpk.dpk,点击ok,生成ColorComboDpk.bpl文件。<br>
		<br>
		----3.使用Tools菜单中的ImageEditor来创建编辑文件ColorComBox.dcr,为TColorComboBox类建立位图。<br>
		<br>
		----4.在Create中加入对字体大小高度的规定及对控件的Style属性(设成csOwnerDrawFixed)的规定,在Create后执行的CreateWnd中初始化颜色的items,如果不需要那么多颜色项,可以以后在生成控件的items属性中直接删除不需要的颜色。<br>
		<br>
		----5.在DrawItem事件中加入颜色自画程序,此事件在OnDrawItem之前发生。<br>
		<br>
		----实现程序如下:<br>
		<br>
		unit ColorComboBox;<br>
		interface<br>
		uses<br>
		Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,<br>
		StdCtrls;<br>
		type<br>
		TColorComboBox=class(TComboBox)<br>
		private<br>
		{Privatede clarations}<br>
		FOnDrawItem:TDrawItemEvent;<br>
		procedure DrawItem(Index:Integer;Rect:TRect;State:TOwnerDrawState);override;<br>
		protected<br>
		{Protected declarations}<br>
		public<br>
		{Publicde clarations}<br>
		constructorCreate(AOwner:TComponent);override;<br>
		procedure CreateWnd;override;<br>
		published<br>
		{Published declarations}<br>
		property OnDrawItem:TDrawItemEvent Read FOnDrawItem write FOnDrawItem;<br>
		end;<br>
		procedure Register;<br>
		<br>
		implementation<br>
		<br>
		procedure Register;//注册构件<br>
		begin<br>
		RegisterComponents('Samples',[TColorComboBox]);<br>
		end;<br>
		<br>
		constructorTColorComboBox.Create<br>
		(AOwner:TComponent);//构件的初始化<br>
		begin<br>
		inheritedCreate(AOwner);<br>
		Style:=csOwnerDrawFixed;//构件的初始类型<br>
		ItemHeight:=20;<br>
		Font.Size:=10;<br>
		end;<br>
		<br>
		procedure TColorComboBox.CreateWnd;<br>
		//颜色构件的Items属性初始化<br>
		begin<br>
		inheritedCreateWnd;<br>
		Items.Clear;<br>
		Items.Add('黑色');<br>
		Items.Add('蓝色');<br>
		Items.Add('蓝绿');<br>
		Items.Add('鲜绿');<br>
		Items.Add('粉红');<br>
		Items.Add('红色');<br>
		Items.Add('黄色');<br>
		Items.Add('白色');<br>
		Items.Add('深蓝');<br>
		Items.Add('青色');<br>
		Items.Add('绿色');<br>
		Items.Add('紫色');<br>
		Items.Add('深红');<br>
		Items.Add('深黄');<br>
		Items.Add('深灰');<br>
		Items.Add('银色');<br>
		----//若不需要这么多颜色可在构件的items属性中删除不需要的颜色<br>
		<br>
		----end;<br>
		<br>
		----//重载DrawItem过程<br>
		<br>
		procedure TColorComboBox.DrawItem(Index:Integer;Rect:TRect;State:TOwnerDrawState);<br>
		var<br>
		&nbsp;&nbsp;&nbsp; TempColor&nbsp;&nbsp;&nbsp; :TColor;//自画颜色<br>
		&nbsp;&nbsp;&nbsp; TempBrushColor:TColor;//临时颜色<br>
		begin//本构件的默认自画设置<br>
		&nbsp;&nbsp;&nbsp; TempBrushColor:=Canvas.Brush.Color;<br>
		//保存原来的的颜色<br>
		&nbsp;&nbsp;&nbsp; Canvas.FillRect(Rect);<br>
		<br>
		if Items[index]='黑色' then<br>
		TempColor:=clBlack<br>
		else if Items[index]='蓝色' then<br>
		TempColor:=clBlue<br>
		else if Items[index]='蓝绿' then<br>
		TempColor:=clAqua<br>
		else if Items[index]='鲜绿' then<br>
		TempColor:=clLime<br>
		else if Items[index]='粉红' then<br>
		TempColor:=clFuchsia<br>
		else if Items[index]='红色' then<br>
		TempColor:=clRed<br>
		else if Items[index]='黄色' then<br>
		TempColor:=clYellow<br>
		else if Items[index]='白色' then<br>
		TempColor:=clWhite<br>
		else if Items[index]='深蓝' then<br>
		TempColor:=clNavy<br>
		else if Items[index]='青色' then<br>
		TempColor:=clTeal<br>
		elseifItems[index]='绿色'then<br>
		TempColor:=clGreen<br>
		else if Items[index]='紫色' then<br>
		TempColor:=clPurple<br>
		else if Items[index]='深红' then<br>
		TempColor:=clMaroon<br>
		else if Items[index]='深黄' then<br>
		TempColor:=clOlive<br>
		else if Items[index]='深灰' then<br>
		TempColor:=clGray<br>
		else if Items[index]='银色' then<br>
		else TempColor:=clSilver;<br>
		<br>
		&nbsp;&nbsp;&nbsp; Canvas.Brush.Color:=TempColor;<br>
		&nbsp;&nbsp;&nbsp; //自画颜色矩形<br>
		Canvas.Rectangle(Rect.Left+4,Rect.Top+1,(Rect.Right+Rect.Left)div3,Rect.Bottom-1);<br>
		Canvas.Brush.Color:=TempBrushColor;<br>
		//显示与颜色对应的字符串<br>
		Canvas.TextOut((Rect.Left+Rect.Right)div2,Rect.Top+1,Items[Index]);<br>
		end;<br>
		end.<br>
		----此控件可以在所有需要颜色选项的程序中使用而且非常方便和美观,并且使编程节省很多时间,增加了程序可靠性和可读性。<br>
		<br>
		----三、自定义颜色对话框构件的使用<br>
		<br>
		----当注册完自定义颜色构件后,可以从Delphi构件模板的Sample页中选择自定义颜色构件,和使用Delphi本身构件没有区别。</p>
	      <p> </p>
	    </td>
	    <td width="3%">&nbsp;</td>
	  </tr>
	</table>
      </td>
    </tr>
    <tr bgcolor="#B0A498" class="p2"> 
      <td height="22" colspan="2">&nbsp; <marquee behavior="alternate">如果你有什么好的资料,可以寄给我哟:)</marquee></td>
      <td height="22" width="45%"> 
	<div align="right"><a href="../index.htm">&lt;&lt;回到首页</a></div>
      </td>
      <td height="22" width="22%"> 
	<div align="right">&lt;&lt;<a href="007.htm">上一篇</a></div>
      </td>
      <td height="22" width="25%"> 
	<div align="right"><a href="009.htm">下一篇</a>&gt;&gt;</div>
      </td>
    </tr>
  </table>
  <br>
  <br>
  <hr size=1 noshade width=500>
  <table width="75%" border="0" align="center" class="p2">
    <tr> 
      <td colspan="2">&nbsp;</td>
      <td width="24%">
	<table width="95%" border="1" bordercolorlight="#663300" bordercolordark="#CCCCCC" cellspacing="0" class="p2">
	  <tr>
	    <td>
	      <div align="center">Delphi 技巧集</div>
	    </td>
	  </tr>
	</table>
      </td>
      <td width="38%">&nbsp;</td>
    </tr>
    <tr> 
      <td width="16%">&nbsp;</td>
      <td colspan="2">Copyright 1999.11 by 东子</td>
      <td width="38%"><a href="../jintongbao@188.net">Mail to me!</a></td>
    </tr>
    <tr> 
      <td colspan="4"> 
	<div align="center">感谢广州视窗提供主页空间</div>
      </td>
    </tr>
  </table>
  <br>
</div><div align="center"><center>
  </center></div>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -