zoukankan      html  css  js  c++  java
  • Delphi 完全时尚手册之 Visual Style 篇

     这里先说说两个概念: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 里

    参考:http://blog.csdn.net/iseekcode/article/details/4733229

  • 相关阅读:
    Pthread使用总结
    OpenCV同态滤波
    重复文件重新创建
    Consider using the `--user` option or check the permissions.
    错误:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
    python tkinter模版
    tkinter页面卡死
    视频下载
    tkinter学习系列(三)之Label控件
    Entry小部件:
  • 原文地址:https://www.cnblogs.com/findumars/p/3530599.html
Copyright © 2011-2022 走看看