使用API函数如何将一副图片画到菜单上??(100分)

  • 主题发起人 主题发起人 kiss2
  • 开始时间 开始时间
K

kiss2

Unregistered / Unconfirmed
GUEST, unregistred user!
菜单也是用API函数建立的!<br>不是加载图标,而是将一副图片贴到菜单上去
 
很久以前写的一段:给你看看~<br>注意用bmp图片~<br><br>在form上放置Image1、MainMenu1。 &nbsp; <br>其中MainMenu1设了10几个菜单选项, 这可以根据你的图片的高度自己重新设置!<br>------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; Menus, jpeg, ExtCtrls, &nbsp;StdCtrls, Buttons; &nbsp;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; MainMenu1: TMainMenu;<br>&nbsp; &nbsp; main1: TMenuItem;<br>&nbsp; &nbsp; caption1: TMenuItem;<br>&nbsp; &nbsp; caption2: TMenuItem;<br>&nbsp; &nbsp; caption3: TMenuItem;<br>&nbsp; &nbsp; caption4: TMenuItem;<br>&nbsp; &nbsp; caption5: TMenuItem;<br>&nbsp; &nbsp; caption6: TMenuItem;<br>&nbsp; &nbsp; Image1: TImage;<br>&nbsp; &nbsp; caption61: TMenuItem;<br>&nbsp; &nbsp; caption81: TMenuItem;<br>&nbsp; &nbsp; caption62: TMenuItem;<br>&nbsp; &nbsp; caption63: TMenuItem;<br>&nbsp; &nbsp; caption64: TMenuItem;<br>&nbsp; &nbsp; caption65: TMenuItem;<br>&nbsp; &nbsp; caption66: TMenuItem;<br>&nbsp; &nbsp; N451: TMenuItem;<br>&nbsp; &nbsp; N23461: TMenuItem;<br>&nbsp; &nbsp; N231: TMenuItem;<br>&nbsp; &nbsp; N41: TMenuItem;<br>&nbsp; &nbsp; N51: TMenuItem;<br>&nbsp; &nbsp; N61: TMenuItem;<br>&nbsp; &nbsp; N71: TMenuItem;<br>&nbsp; &nbsp; N81: TMenuItem;<br>&nbsp; &nbsp; N91: TMenuItem;<br>&nbsp; &nbsp; N101: TMenuItem;<br>&nbsp; &nbsp; N111: TMenuItem;<br>&nbsp; &nbsp; N121: TMenuItem;<br>&nbsp; &nbsp; N131: TMenuItem;<br>&nbsp; &nbsp; N24351: TMenuItem;<br>&nbsp; &nbsp; N2561: TMenuItem;<br>&nbsp; &nbsp; N23462: TMenuItem;<br>&nbsp; &nbsp; N23471: TMenuItem;<br>&nbsp; &nbsp; N34561: TMenuItem;<br>&nbsp; &nbsp; N35761: TMenuItem;<br>&nbsp; &nbsp; N43561: TMenuItem;<br>&nbsp; &nbsp; N241: TMenuItem;<br>&nbsp; &nbsp; N23463: TMenuItem;<br>&nbsp; &nbsp; N2351: TMenuItem;<br>&nbsp; &nbsp; N23464: TMenuItem;<br>&nbsp; &nbsp; N1231: TMenuItem;<br>&nbsp; &nbsp; N45761: TMenuItem;<br>&nbsp; &nbsp; N23465: TMenuItem;<br>&nbsp; &nbsp; procedure caption1MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption2MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption3MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption4MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption5MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption6MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption1DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption2DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption3DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption4DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption5DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption6DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption61DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption81DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption62DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption64DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption63DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption66DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption65DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; ARect: TRect; Selected: Boolean);<br>&nbsp; &nbsp; procedure caption61MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption81MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption62MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption64MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption63MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption66MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure caption65MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; &nbsp; &nbsp; var Width, Height: Integer);<br>&nbsp; &nbsp; procedure BitBtn1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp;//自定义<br>&nbsp; procedure DrawItem(Sender: TMenuItem; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean;StrOut:String);<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; i,ih,ind,iw,irate:integer;<br>&nbsp; rtemp:trect;<br>&nbsp; ig1,ig2:integer;<br>implementation<br><br>{$R *.DFM}<br>procedure TForm1.DrawItem(Sender: TMenuItem; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean;StrOut:String);<br>var j:integer;<br>begin<br>&nbsp;i:=arect.Bottom-arect.Top;<br>&nbsp;ind:=sender.MenuIndex;<br>&nbsp;ih:=round(image1.Height/13*ind);<br>&nbsp;stretchBlt(acanvas.Handle,arect.Left,arect.Top,iw,i,image1.Canvas.Handle,0,ih,image1.Width,round(image1.Height/13),srccopy);<br>&nbsp; &nbsp;if selected then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;acanvas.Font.Color:=clwhite;<br>&nbsp; &nbsp; &nbsp; &nbsp;rtemp:=arect;<br>&nbsp; &nbsp; &nbsp; &nbsp;rtemp.Left:=rtemp.Left+iw;<br>&nbsp; &nbsp; &nbsp; &nbsp;ig1:=round((rtemp.Right-rtemp.Left)/10);<br>&nbsp; &nbsp; &nbsp; &nbsp;rtemp.Right:=rtemp.Left+ig1;<br>&nbsp; &nbsp; &nbsp; &nbsp;for j:=0 &nbsp;to 9 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;acanvas.Brush.Color:=rgb(0,0,j*25);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;acanvas.FillRect(rtemp);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rtemp.Left:=rtemp.Left+ig1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rtemp.Right:=rtemp.Left+ig1;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; acanvas.Brush.Color:=cl3dlight;<br>&nbsp; &nbsp; &nbsp; &nbsp; rtemp:=arect;<br>&nbsp; &nbsp; &nbsp; &nbsp; rtemp.Left:=rtemp.Left+iw;<br>&nbsp; &nbsp; &nbsp; &nbsp; acanvas.FillRect(rtemp);<br>&nbsp; &nbsp; &nbsp; &nbsp; acanvas.Font.Color:=clblack;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;acanvas.Brush.Style:=bsclear;<br>&nbsp; &nbsp;acanvas.TextOut(arect.Left+iw+5,arect.Top,strout);<br>end;<br><br>procedure TForm1.caption1MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+50;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption2MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+50;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption3MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption4MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption5MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption6MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption1DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'234643623562346');<br>end;<br><br>procedure TForm1.caption2DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption2');<br>end;<br><br>procedure TForm1.caption3DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption3');<br>end;<br><br>procedure TForm1.caption4DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption4');<br>end;<br><br>procedure TForm1.caption5DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption5');<br>end;<br><br>procedure TForm1.caption6DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption6');<br>end;<br><br>procedure TForm1.caption61DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption7');<br>end;<br><br>procedure TForm1.caption81DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption8');<br>end;<br><br>procedure TForm1.caption62DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption9');<br>end;<br><br>procedure TForm1.caption64DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption10');<br>end;<br><br>procedure TForm1.caption63DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption11');<br>end;<br><br>procedure TForm1.caption66DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption12');<br>end;<br><br>procedure TForm1.caption65DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>begin<br>DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption13');<br>end;<br><br>procedure TForm1.caption61MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption81MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption62MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption64MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption63MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption66MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>procedure TForm1.caption65MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>begin<br>height:=acanvas.TextHeight('caption1')+5;<br>width:=acanvas.TextWidth('caption1')+5;<br>irate:=round(image1.height/(height*13));<br>iw:=round(image1.width/irate);<br>width:=width+iw;<br>end;<br><br>end.<br>----------------------------------<br>等于重新绘制菜单! &nbsp; 将图片一块一块画上去的,能实现你要求的功能! <br><br>D6 下通过! &nbsp; 其中有代码冗余,你自己可以优化一下! &nbsp; &nbsp; &nbsp;<br>
 
thxs,我用API创建的菜单,哪来的<br>procedure TForm1.caption6DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>procedure TForm1.caption61MeasureItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; var Width, Height: Integer);<br>过程啊??
 
你把上面代码的procedure TForm1.caption6DrawItem(Sender: TObject; ACanvas: TCanvas;<br>&nbsp; ARect: TRect; Selected: Boolean);<br>写成过程myDrawItem<br>在你创建的菜单mymenu.DrawItem:=myDrawItem;<br>应该可以吧!你试试!
 
后退
顶部