Delphi 完全时尚手册之 Visual Style 篇 (界面不错) 转自http://blog.csdn.net/iseekcode/article/details/4733229

来源:互联网 发布:广电总局 知乎 编辑:程序博客网 时间:2024/05/19 05:02

======================================================
注:本文源代码点此下载
======================================================

这里先说说两个概念:theme(主题)和 visual style 。theme 最早出现在 microsoft plus! for windows 95 中,是 windows 中 wallpaper、cursors、fonts、sounds 、icons 等的设置值集合。visual style 在 windows xp 中才被引入,visual style 规定了 contorls 的外观,另外还包括使用这些外观的一套 api 。使用 visual style 必须要 comctl32.dll 6,而 comctl32.dll 6 是不能被分发到以前版本的 windows 中的,所以只能在 windows xp 下使用 visual style。

delphi 7 (后面简称 7 吧)对 visual style 提供了较好的支持。首先 7 将那个 menifest 封装成了 vcl - txpmenifest,另外增加了 uxtheme.pas 单元,里面是对 visual style 的一套 api 及其结构、常数等的引用声明(大概有 47 个api 函数),更重要的是 7 还增加一个 themes.pas 单元,里面是对这套 api 的更进一步的简化和封装,7 下的 win32 控件对 visual style 的支持较 delphi 6有了很大改观,就是这个单元的功劳。

一般来说要使 7 下编译的程序在 windows xp 具有 visual style ,只需在主窗体上放入 vcl - txpmenifest 即可,但对于一些非标准或是自画的控件,还是经典的界面。这里就说一下如何用 themes.pas 中提供的方法使这些控件具有 visual style 。

themes.pas 中只有一个类:tthemeservices 。这个类有一个重要的属性: themesenabled (boolean 类型),就是判断在当前程序能不能使用 visual style ,这个属性只有在程序使用了 txpmenifest 并且在 windows xp 下运行并且使用了 windows xp 的主题(即桌面主题不是 windows 经典)才为 true ,由于程序要运行在以前版本的 windows 下,所以你的程序也得提供这个属性为 false 时的处理过程(一般就是原有的处理过程)。还要提一下这个类封装的几个重要函数:drawedge 用来画控件边界的,drawelement 用来画整个界面的,drawtext 用来写字的。这个类还处理了 wm_themechanged 消息,这样当我们在改变桌面主题后,程序会自动调整外观。再说一下我们最常用到的一个函数(多态函数):getelementdetails ,这个函数的返回值在上面的几个 drawxxx 函数中要用到,这个函数的输入值是 24 个枚举类型中的元素,这 24 个枚举类型在 themes.pas 单元开头定义(从第二个 tthemedbutton 开始直到 tthemedwindow)。最后,我们不去直接使用这个类,在 themes.pas 单元中有一函数:

function themeservices: tthemeservices;返回值就是这个类,所以我们直接使用这个方法,7 的 vcl 里都是这样做。

好,下面就来个简单的例子。delphi 的 tpanel 控件不是标准控件,我们就来在它上面实现一下 visual style 。在 delphi 7 中新建一工程,在主窗体上放入 txpmenifest ,在 unit1 单元引用 themes 单元,在

tform1 = class(tform)前面加入下代码(主要是重载 tcustompanel 的 paint 方法):

tvspanel = class(tcustompanel)

private

//

protected

procedure paint; override;

public

//

end;重载的 paint 方法实现如下:

procedure tvspanel.paint;

var

details: tthemedelementdetails;

begin

inherited;

if themeservices.themesenabled then

begin

details := themeservices.getelementdetails(tbpushbuttonhot);{这里画个按钮处于 hot 状态下的样子}

performerasebackground(self, canvas.handle);{擦除画按钮时的背景}

themeservices.drawelement(canvas.handle, details, clientrect);

themeservices.drawtext(canvas.handle, details, caption, clientrect,

dt_expandtabs or dt_vcenter or dt_center or dt_singleline, 0);

end;

end;tcustompanel 的改动完成,再就是在主窗体的 create 事件中实例化 tvspanel ,代码如下:

procedure tform1.formcreate(sender: tobject);

var

apanel: tvspanel;

begin

apanel := tvspanel.create(application);

apanel.left := 100;

apanel.top := 100;

apanel.width := 200;

apanel.height := 30;

apanel.caption := '具有 button 风格的 panel';

apanel.parent := self;

end;好了,运行看看 visual style 效果是不是出来了。我的程序里用 thintwindow 做了个浮动窗口并且加上了 visual style ,效果还不错,如果再细致点的话,就做出和 windows xp 下微软拼音输入法那个浮动条完全相同的界面了。

另外,还有几个重要的东西没有提及,比如 part 和 state ,大家可以查 msdn ,在 user interface design and development windows shell shell refrence visual styles refrence 里,也可 email 与我讨论


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/