打印本文 打印本文 关闭窗口 关闭窗口
DELPHI的奇异菜单的编写
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2218  更新时间:2009/4/23 18:38:56  文章录入:mintao  责任编辑:mintao
ext. After drawing the text, this work must be reversed; the old font must be selected, and the new font deleted. If the new font isn''''t deleted, there will be a memory leak, and - if the routine is executed many times - Windows (especially 95/98) will run out of resources, and crash.
一旦你已设置好了TlogFont结构体,剩下唯一要做的事是改变lfEscapement的值为目的值并且用CreateFontIndirect来产生一个新字体。在使用这个新字体之前,必须用SelectObject来选择它。另一种方法是在描绘文字之前用这个新字体对象的句柄赋给窗体的canvas的字体对象的句柄。在描绘完文字后,这个过程要巅倒;旧字体必须被选中,新字体被删除。如果新字体没有被删除,会造成内存泄漏,并且-----如果程序被执行多次------ Windows (尤其是 95/98)会耗尽资源,并且
死机。
Stylish Lines
流行的线条
When you draw lines, the individual pixels usually don''''t matter; you simply set the line style, and it''''s drawn by Windows. Sometimes however, you need to do something special and draw a line style not provided by Windows. This can be done using a Windows API function named LineDDA, defined in Figure 13.
当你描绘线条时,单独的象素通常是不重要的;你只须简单地设置线条的类型,它将交给Windows来描绘。然而有时你想要做一些特殊的并且Windows没有提供的线条类型。这可以用一个名叫LineDDA的API函数来实现,在Figure 13中可以看到它的定义。

function LineDDA(
  nXStart,         // x-coordinate of line''''s starting point.
X坐标起点
  nYStart,         // y-coordinate of line''''s starting point.
Y坐标起点
  nXEnd,           // x-coordinate of line''''s ending point.
X坐标终点
  YEnd : Integer; // y-coordinate of line''''s ending point.
Y坐标终点
   // Address of application-defined callback function.
应用程序定义的回调函数的地址
  lpLineFunc : TFNLineDDAProc;
  lpData : LPARAM // Address of application-defined data.
应用程序定义的数据的地址
): BOOL; stdcall;

Figure 13: Object Pascal declaration for the Windows API function, LineDDA.


The first four parameters are the starting and ending points of the line. The fifth parameter is a callback function that will be called every time a pixel should be drawn. You put your drawing routines there. The last parameter is a user parameter that will be passed to the callback function. You can pass any Integer or pointer to the function, because it is an LParam (in Win32, it is translated to a Longint). The callback function must take the form shown here:
这开始的四个参数是线条的开始和结束点。第五个参数是一个回调函数,每次像素被描绘时都将被调用到。你可以将你的描绘过程写在这里。最后一个参数是用户定义的可以传给回调函数。你可以传递任何整数或指针给这个函数,因为它是
一个Lparam型(在WIN32,它是被解释成Longint型)。这个回调函数必须使用象如下的形式:
procedure CallBackDDA(x, y: Integer;
  UserParam: LParam); stdcall;

where x and y are the coordinates of the drawn point, and UserParam is a parameter that is passed to the function. This function must be declared as stdcall. The routine in Figure 14 draws a line of bitmaps, and Figure 15 shows the result.
这里X和Y都是被描绘的坐标点,而UserParam是一个参数。这个函数必须被子定义为stdcall。Figure 14中的程序描绘了一个BMP线条,而Figure 15则显示结果。

type
  TForm1 = class(TForm)
    ImageList1: TImageList;
     procedure FormPaint(Sender: TObject);
     procedure FormResize(Sender: TObject);
   end;
var
  Form1: TForm1;
procedure CallDDA(x, y: Integer; Form: TForm1); stdcall;
implementation
{ $R *.DFM }
procedure CallDDA(x, y: Integer; Form: TForm1);
begin
   if x mod 13 = 0 then
    Form.ImageList1.Draw(Form.Canvas, x, y, 0);
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
  LineDDA(0, 0, ClientWidth, ClientHeight,
          @CallDDA, Integer(Self));
end;
procedure TForm1.FormResize(Sender: TObject);
begin
  Invalidate;
end;
Figure 14: Code to draw a line of bitmaps. 
Figure 15: Window with a custom line.

This routine handles the form''''s OnPaint event, calling LineDDA, so every time the form must be painted, it redraws the line. Another event that is handled is OnResize, which invalidates the form client area, so the line must be redrawn when someone changes its size. The LineDDA callback function, CallDDA, is very simple. At every 13th point it is called, it draws the bitmap stored in the ImageList. As you may notice, Self is passed as the last parameter to the callback function, so it can access the instance data.
这个程序处理窗体的OnPaint事件,调用LineDDA,所以每次窗体被描绘时,它将重画这条线。另一个事件是OnResize,它使窗体的客户区无效,所以当有人改变它的大小时线条亦将重画。LineDDA回调函数,CallDDA都是非常简单的。每当被调用了13次后,它将描绘存贮在ImageList中的位图。也许你注意到,SELF被作为最后一个参数传递给回调函数,所以它可以存取程序的数据。

Conclusion
结论
Since owner drawing was exposed on TMainMenu in Delphi 4, there have been many ways to augment your menus. Using the techniques we''''ve discussed here, you can easily enhance your Delphi application''''s menus with custom text, bitmaps, and colors.
既然owner drawing在Delphi 4的TmainMenu中已出现了,它就可以有很多方法来扩展你的菜单功能。使用我们在上面讨论过的技巧,你能够轻易地用自定义文字,位图,和颜色来加强你的DELPHI应用程序的菜单功能。


 

 

上一页  [1] [2] [3] 

打印本文 打印本文 关闭窗口 关闭窗口