Desktop Window Manager ——Delphi开发学习 .

原文地址:http://msdn.microsoft.com/en-us/library/windows/desktop/aa969540(v=vs.85).aspx

 

桌面合成功能,在Windows Vista版本中被引用,从根本上改变了应用程序在屏幕上显示像素的方式。当启用桌面合成时,单独的窗体不再像之前的Windows版本一样直接绘制到屏幕或主显示设备上。相反,他们的绘制被重新定向到视频内存中的离线表面,这些离线表面之后被渲染成桌面图片并且显示到屏幕上。

桌面合成由桌面窗体管理器(DWM)处理。通过桌面合成,DWM在桌面上启用视觉特效,比如玻璃窗体边框,3-D窗体过渡动画,窗体翻转和3D翻转,和高分辨率支持。

桌面窗体管理器作为Windows的服务运行。它可以被启用和禁用,通过控制面板中的管理工具项,在服务下面的Desktop Window Manager Session Manager。

很多DWM的功能可以被控制或应用程序通过DWM API访问。下面这些文档描述DWM API实现的功能和要求。

DWM概述


主题

描述

Enable and Control DWM Composition

启用和控制DWM合成

桌面窗体窗理器(DWM)的合成API提供一些函数来设置和查询DWM使用的基本信息。这些API允许你查询和更改合成状态。另外,你可以为不同的DWM窗体属性设置和查询渲染策略。

DWM Blur Behind Overview

DWM背景模糊概述

标志DWM效果的其中之一就是半透明以及模糊非客户区。DWM的API允许应用程序应用这些效果到他们顶层窗体的客户区。

DWM Thumbnail Overview

DWM缩略图概述

DWM可以显示应用程序窗体的缩略图。这不是静态的窗体截图,相反,它是动态的,在缩略图源窗体和目标窗体上的位置之间的固定连接接收实时缩略图渲染。通过鼠标停靠在任务栏的应用程序上或使用ALT+TAB键来查看和快速切换到应用程序,来允许快速查看正在运行的应用程序。

Accessing and Controlling DWM Frame Data

DWM边框数据的访问和控制

这个章节讨论用来调度和媒体表现的DWM API。

Performance Considerations and Best Practices

性能注意事项和最佳实践

这个章节呈现了一组使用DWM API的最佳实践。

Custom Window Frame Using DWM

使用DWM自定义窗体边框

这个章节演示如何使用DWM API来为你的应用程序创建自定义的窗体边框。

 

启用和控制DWM合成

 

桌面窗体管理器(DWM)合成API提供了一些函数来设置和查询被DWM使用的一些基本信息。这些API允许你查询和更改合成状态。另外,你可以为不同的DWM窗体属性设置和查询渲染策略。

这篇文章包含以下章节:

1.禁用DWM组合

2.接收变色信息

3.控制非客户区区域渲染

4.消息

1.DisablingDWM Composition:禁用DWM组合

注意 对于Windows 8 消费者预览版,在这个章节中的信息不再可用。DWM不能够再被编程禁用,也不能够在一个应用程序试图绘制主显示平面的时候禁用。下面的信息只适用于Windows 7以及更早的系统。

因为DWM使用图处理单元(GPU)来桌面合成,一定部分的应用程序可能为了兼容性而必须禁用DWM。完全控制桌面的应用程序,例如在全屏模式下运行的游戏,必须判断DWM是否启用,并且如果DWM启用,禁用它。为了实现这些,需要两个函数:DwmIsCompositionEnabledDwmEnableComposition

调用DwmEnableComposition 时,参数fEnable设置为DWM_EC_DISABLECOMPOSITION,它会禁用DWM合成,直到调用进程关闭或通过调用DwmEnableComposition(fEnalbe参数设置为DWM_EC_ENABLECOMPOSITION)来再次启用合成。当所有曾经禁用合成的应用程序都被关闭或通过调用DwmEnableCompostion来手动重启组合时,DWM合成将自动重启

注意 当一个应用程序试图在主显示平面上直接绘制的时候,DWM会自动禁用合成。合成会被禁用直到主显示平面被应用程序释放。


2.Retrievingthe Colorization Information:接收变色信息

 

窗体非客户区域的颜色由当前系统主题处理。提供这个颜色值,通过DWM API允许应用程序来匹配客户UI与系统颜色主题。

要访问这个颜色值以及监视颜色变化,使用DwmGetColorizationColor函数以及WM_DWMCOLORIZATIONCOLORCHANGED消息。

下面的例子演示如何来监视颜色更改以及访问新的颜色。

C:

  1. ...  
  2. case WM_DWMCOLORIZATIONCOLORCHANGED:  
  3. {  
  4.    g_currColor = (DWORD)wParam;  
  5.    g_opacityblend = (BOOL)lParam;  
  6. }  
  7. break;  
  8. ...  
...
case WM_DWMCOLORIZATIONCOLORCHANGED:
{
   g_currColor = (DWORD)wParam;
   g_opacityblend = (BOOL)lParam;
}
break;
...

D2010(引用Dwmapi.pas):

  1. procedure WMDWMCOLORIZATIONCOLORCHANGED(varMessage:TMessage);message WM_DWMCOLORIZATIONCOLORCHANGED;  
  2. var  
  3.  pcrColorization: DWORD;  
  4.  pfOpaqueBlend: BOOL;  
  5. begin  
  6.  pcrColorization:=Message.WParam;//当前主题颜色   
  7.  pfOpaqueBlend:=BOOL(Message.LParam);//是否启用透明效果   
  8. end;  
procedure WMDWMCOLORIZATIONCOLORCHANGED(varMessage:TMessage);message WM_DWMCOLORIZATIONCOLORCHANGED;
var
 pcrColorization: DWORD;
 pfOpaqueBlend: BOOL;
begin
 pcrColorization:=Message.WParam;//当前主题颜色
 pfOpaqueBlend:=BOOL(Message.LParam);//是否启用透明效果
end;

主题颜色和是否启用透明效果的当前状态可以在控制面板中看到:

 

 

3.Controlling Non-ClientRegion Rendering:控制非客户区区域渲染

 

DWM启用的两个可视效果分别是窗体非客户区域的透明以及过渡效果。一些应用程序可能为了风格或兼容原因而必须禁用或重新启用这些效果。下面的函数用来管理透明度和过渡效果的方式。

1. DwmGetWindowAttribute

2. DwmSetWindowAttribute

为了让应用程序接收当前非客户区渲染状态,使用带DWMWA_NCRENDERING_ENABLED属性的DwmGetWindowAttribute。下面的例子显示了一个典型的DwmGetWindowAttribute调用。

C:

  1. BOOL enabled = FALSE;  
  2. HRESULT hr = DwmGetWindowAttribute(hwnd,DWMWA_NCRENDERING_ENABLED, &enabled, sizeof(enabled));  
  3.    
BOOL enabled = FALSE;
HRESULT hr = DwmGetWindowAttribute(hwnd,DWMWA_NCRENDERING_ENABLED, &enabled, sizeof(enabled));
 

D2010(引用Dwmapi.pas):

  1. var  
  2.  Attribute: BOOL;  
  3.  HReturn:Integer;  
  4. begin  
  5.  Attribute := FALSE;  
  6.  HReturn := DwmGetWindowAttribute(Handle, DWMWA_NCRENDERING_ENABLED,@Attribute, sizeof(Attribute));  
var
 Attribute: BOOL;
 HReturn:Integer;
begin
 Attribute := FALSE;
 HReturn := DwmGetWindowAttribute(Handle, DWMWA_NCRENDERING_ENABLED,@Attribute, sizeof(Attribute));

注意 每个DWMWINDOWATTRIBUTE有一个实现的类型与之相关。请参阅每个枚举成员的详细信息。

DwmSetWindowAttribute允许应用程序设置非客户区域的渲染策略。它也决定了一个应用程序该如何处理DWM过渡效果。

下面的例子禁用了非客户区渲染。

C:

  1. HRESULT DisableNCRendering(HWND hwnd)  
  2. {  
  3.    HRESULT hr = S_OK;  
  4.    
  5.    DWMNCRENDERINGPOLICY ncrp = DWMNCRP_DISABLED;  
  6.    
  7.    // Disable non-client area rendering on the window.   
  8.    hr = DwmSetWindowAttribute(hwnd, DWMWA_NCRENDERING_POLICY, &ncrp,sizeof(ncrp));  
  9.    if (SUCCEEDED(hr))  
  10.     {  
  11.        // ...   
  12.     }  
  13.     
  14.    return hr;  
  15. }  
HRESULT DisableNCRendering(HWND hwnd)
{
   HRESULT hr = S_OK;
 
   DWMNCRENDERINGPOLICY ncrp = DWMNCRP_DISABLED;
 
   // Disable non-client area rendering on the window.
   hr = DwmSetWindowAttribute(hwnd, DWMWA_NCRENDERING_POLICY, &ncrp,sizeof(ncrp));
   if (SUCCEEDED(hr))
    {
       // ...
    }
  
   return hr;
}

D2010(引用Dwmapi.pas):

  1. var  
  2.  Attribute: DWORD;  
  3.  HReturn:Integer;  
  4. begin  
  5.  Attribute := DWMNCRP_DISABLED;  
  6.  HReturn := DwmSetWindowAttribute(Handle, DWMWA_NCRENDERING_POLICY,@Attribute, sizeof(Attribute));  
var
 Attribute: DWORD;
 HReturn:Integer;
begin
 Attribute := DWMNCRP_DISABLED;
 HReturn := DwmSetWindowAttribute(Handle, DWMWA_NCRENDERING_POLICY,@Attribute, sizeof(Attribute));

除了控制非客户区的非客户区渲染,DwmSetWindowAttribute也可以控制DWM过渡效果。过渡方式可以通过使用DWMWA_TRANSITIONS_FORCEDISABLED做为dwAttribute参数来设置。

4.Messaging:消息

 

下面的消息提供了DWM事件的通知。这些消息可以用来监视例如合成状态更改或系统颜色主题更改的变化。

1. WM_DWMCOLORIZATIONCOLORCHANGED

2. WM_DWMCOMPOSITIONCHANGED

3. WM_DWMNCRENDERINGCHANGED

4. WM_DWMWINDOWMAXIMIZEDCHANGE



DWM Blur Behind Overview:DWM背景模糊

 

标志桌面窗体管理器效果的其中之一就是半透明以及模糊非客户区。DWM API允许应用程序应用这些特效到顶层窗体的客户区。

注意Windows Vista Home Basic 版本不支持透明玻璃效果。通常在其他Windows版本上渲染为透明玻璃效果的区域会被渲染为不透明。

这篇文章讨论下列DWM允许的客户区背景模糊情景。

1. Adding Blur to a Specific Region of the Client Area 给客户区指定的区域添加模糊

2. Extending the Window Frame into the Client Area 扩展窗体边框到客户区


1.Adding Blur to a SpecificRegion of the Client Area:给客户区指定的区域添加模糊


应用程序可以在整个窗体的客户区或一个指定的子区域实现背景模糊效果。这允许应用程序添加与其他应用程序看起来不同的风格条和搜索条。

在这种情况下要用到的的API是DwmEnableBlurBehindWindow函数,它使用DWM Blur Behind ConstantsDWM_BLURBEHIND结构。

下面的例子函数,EnableBlurBehind,说明了如何来给整个窗体实现模糊效果。

C:

  1. HRESULT EnableBlurBehind(HWND hwnd)  
  2. {  
  3.    HRESULT hr = S_OK;  
  4.    
  5.    // Create and populate the blur-behind structure.   
  6.    DWM_BLURBEHIND bb = {0};  
  7.    
  8.    // Specify blur-behind and blur region.   
  9.    bb.dwFlags = DWM_BB_ENABLE;  
  10.    bb.fEnable = true;  
  11.    bb.hRgnBlur = NULL;  
  12.    
  13.    // Enable blur-behind.   
  14.    hr = DwmEnableBlurBehindWindow(hwnd, &bb);  
  15.    if (SUCCEEDED(hr))  
  16.     {  
  17.        // ...   
  18.     }  
  19.    return hr;  
  20. }  
  21.    
HRESULT EnableBlurBehind(HWND hwnd)
{
   HRESULT hr = S_OK;
 
   // Create and populate the blur-behind structure.
   DWM_BLURBEHIND bb = {0};
 
   // Specify blur-behind and blur region.
   bb.dwFlags = DWM_BB_ENABLE;
   bb.fEnable = true;
   bb.hRgnBlur = NULL;
 
   // Enable blur-behind.
   hr = DwmEnableBlurBehindWindow(hwnd, &bb);
   if (SUCCEEDED(hr))
    {
       // ...
    }
   return hr;
}
 

D2010(引用Dwmapi.pas):

  1. var  
  2.  pBlurBehind: TDwmBlurbehind;  
  3. begin  
  4.  ZeroMemory(@pBlurBehind,SizeOf(TDwmBlurbehind));  
  5.  pBlurBehind.dwFlags:=DWM_BB_ENABLE;  
  6.  pBlurBehind.fEnable:=True;  
  7.  pBlurBehind.hRgnBlur:=0;  
  8.  DwmEnableBlurBehindWindow(Handle, pBlurBehind);  
var
 pBlurBehind: TDwmBlurbehind;
begin
 ZeroMemory(@pBlurBehind,SizeOf(TDwmBlurbehind));
 pBlurBehind.dwFlags:=DWM_BB_ENABLE;
 pBlurBehind.fEnable:=True;
 pBlurBehind.hRgnBlur:=0;
 DwmEnableBlurBehindWindow(Handle, pBlurBehind);

注意给hRgnBlur参数指定的是NULL。这告诉DWM给整个窗体应用背景模糊。

下面的图片说明了将背景模糊效果应用到了整个窗体。

 

为了在一个子区域中应用模糊,将一个有效的句柄(HRGN)赋给hRgnBlur成员(DWM_BLURBEHIND结构的成员)并且添加DWM_BB_BLURREGION 标志给dwFlags成员。

当你给一个窗体的子区域应用背景模糊效果的时候,窗体的透明通道是非模糊区域使用的。在窗体的非模糊区可能会引起一些非预期的透明效果。因些,当你应用模糊效果到一个子区域的时候要小心。

 

2.Extending the Window Frameinto the Client Area:扩展窗体的边框到客户区

 

一个应用程序可以扩展模糊的窗体边框到客户区。当你应用背景模糊效果到一个带有停靠工具栏或与其他应用程序看起来不同的控件的窗体时是非常有用的。这个功能由DwmExtendFrameIntoClientArea函数实现。

通过使用DwmExtendFrameIntoClientArea来启用模糊,使用MARGINS结构来表明你要扩展到客户区到多少宽。下面的例子函数,ExtendIntoClientBottom扩展模糊在非客户区边框底部到客户区。

C:

  1. HRESULT ExtendIntoClientBottom(HWND hwnd)  
  2. {  
  3.    HRESULT hr = S_OK;  
  4.    
  5.    // Set the margins, extending the bottom margin.   
  6.    MARGINS margins = {0,0,0,25};  
  7.    
  8.    // Extend the frame on the bottom of the client area.   
  9.    hr = DwmExtendFrameIntoClientArea(hwnd,&margins);  
  10.    if (SUCCEEDED(hr))  
  11.     {  
  12.        // ...   
  13.     }  
  14.    return hr;  
  15. }  
HRESULT ExtendIntoClientBottom(HWND hwnd)
{
   HRESULT hr = S_OK;
 
   // Set the margins, extending the bottom margin.
   MARGINS margins = {0,0,0,25};
 
   // Extend the frame on the bottom of the client area.
   hr = DwmExtendFrameIntoClientArea(hwnd,&margins);
   if (SUCCEEDED(hr))
    {
       // ...
    }
   return hr;
}

D2010(引用Dwmapi.pas):

  1. var  
  2.  rcClient:TRect;  
  3. var  
  4.  pMarInset: UxTheme.TMargins;  
  5. begin  
  6.  pMarInset.cxLeftWidth:=0;  
  7.  pMarInset.cyTopHeight:=0;  
  8.  pMarInset.cxRightWidth:=0;  
  9.  pMarInset.cyBottomHeight:=25;  
  10.    
  11.  DwmExtendFrameIntoClientArea(Handle,pMarInset);  
  12. end;  
var
 rcClient:TRect;
var
 pMarInset: UxTheme.TMargins;
begin
 pMarInset.cxLeftWidth:=0;
 pMarInset.cyTopHeight:=0;
 pMarInset.cxRightWidth:=0;
 pMarInset.cyBottomHeight:=25;
 
 DwmExtendFrameIntoClientArea(Handle,pMarInset);
end;

下面的图片说明了背景模糊效果扩展到客户区的底部。

 

也可以通过DwmExtendFrameIntoClientArea方法来实现“玻璃板”的效果,模糊效果应用到整个窗体表示而没有一个可见的窗体边。下面的例子演示了这个客户区被渲染成没有窗体边的效果,

C:

  1. HRESULT ExtendIntoClientAll(HWND hwnd)  
  2. {  
  3.    HRESULT hr = S_OK;  
  4.    
  5.    // Negative margins have special meaning toDwmExtendFrameIntoClientArea.   
  6.    // Negative margins create the "sheet of glass" effect, wherethe client   
  7.    // area is rendered as a solid surface without a window border.   
  8.    MARGINS margins = {-1};  
  9.    
  10.    // Extend the frame across the whole window.   
  11.    hr = DwmExtendFrameIntoClientArea(hwnd,&margins);  
  12.    if (SUCCEEDED(hr))  
  13.     {  
  14.        // ...   
  15.     }  
  16.    return hr;  
  17. }  
HRESULT ExtendIntoClientAll(HWND hwnd)
{
   HRESULT hr = S_OK;
 
   // Negative margins have special meaning toDwmExtendFrameIntoClientArea.
   // Negative margins create the "sheet of glass" effect, wherethe client
   // area is rendered as a solid surface without a window border.
   MARGINS margins = {-1};
 
   // Extend the frame across the whole window.
   hr = DwmExtendFrameIntoClientArea(hwnd,&margins);
   if (SUCCEEDED(hr))
    {
       // ...
    }
   return hr;
}

D2010(引用Dwmapi.pas):

  1. var  
  2.  rcClient:TRect;  
  3. var  
  4.  pMarInset: UxTheme.TMargins;  
  5. begin  
  6.  pMarInset.cxLeftWidth:=-1;  
  7.  pMarInset.cyTopHeight:=-1;  
  8.  pMarInset.cxRightWidth:=-1;  
  9.  pMarInset.cyBottomHeight:=-1;  
  10.    
  11.  DwmExtendFrameIntoClientArea(Handle,pMarInset);  
  12. end;  
var
 rcClient:TRect;
var
 pMarInset: UxTheme.TMargins;
begin
 pMarInset.cxLeftWidth:=-1;
 pMarInset.cyTopHeight:=-1;
 pMarInset.cxRightWidth:=-1;
 pMarInset.cyBottomHeight:=-1;
 
 DwmExtendFrameIntoClientArea(Handle,pMarInset);
end;

下图展示了在“玻璃板”窗体风格中的背景模糊。

 


Performance Considerations and Best Practices:性能注意事项和最佳实践

 

 

这篇文章列举了一系列使用桌面窗体管理器(DWM)API最佳实践。

这篇文章包含下列章节:

1.  Application Practices for DWM DWM应用程序实践

2.  Drawing Practices for DWM DWM绘制实践

3.  DWM Blur-Behind Client Region DWM背景模糊客户区

 

1.Application Practices forDWM:DWM应用程序实践

 

如果你的应用程序需要处理每英寸点数(DPI)缩放,你可以将一个应用程序声明为DPI-Aware,并且通过在程序Manifest的dpi-aware标记或在程序初始期间通过调用SetProcessDPIAware函数来阻止自动缩放。

当DWM合成打开的时候,遮蔽的应用程序不再接收到WM_PAINT消息并且不再询问是否重新渲染。每个窗体的内容已经可以用来合成到桌面图片。

为了实现点击测试,顶层WS_EX_TRANSPARENT窗体应该与WS_EX_LAYERED风格结合。WS_EX_TRANSPARENT在经典模式下,没有重定向,对于同属于相同线程的同一个层次的子窗体是有用的,但不是顶层窗体。

使用区域或分层来创建异形或混合窗体。注意,在Windows Vista或之后版本中,只自定义绘制的顶层窗体的部分将不会在非绘制区域提供期望的陈旧内容。

一些API,例如GetDCOrgEx可以被用来决定准确的实际值。如果你的重定向窗体有一个设备上下文(DC),GetDCOrgEx返回的原点不再与在屏幕上你的窗体的原点相符合。这个原点会被替换成为窗体的后台缓冲平面的原点:(0,0)。

当所有其他失败,通过调用DwmSetWindowAttribute函数来禁用窗体的渲染。

 

2.Drawing Practices for DWM:DWM绘制实践

 

避免直接绘制到主显示平面。做这些会强制DWM来禁用合成,直接你的应用程序释放主设备平面。

评估你的应用程序是否必须提供它自己的双缓冲。DWM有效地双缓冲内容,并在一个单一的帧中显示窗体。

避免从显示DC中读取或写入。尽管这些区域可以被应用程序访问,并且Microsoft Win32 API支持在这里绘制,做这些可能会导致窗体失去它原先拥有的玻璃边。

避免混合Windows Graphics Device Interface(GDI)和MicrosoftDirectX,除非它们不重叠。如果必需混合时,要么绘制GDI内容到DirectX软件平面并在合成到屏幕之前组合他们,还要么在单独的窗体中绘制他们。

为了渲染,请使用BitBltStretchBlt 函数替代WindowsGDI+来呈现你的绘制。GDI+使用软件渲染来渲染一条扫描线在同一时间。这可能会在你的应用程序中引起忽隐忽现。

 

3.DWM Blur-Behind ClientRegion:DWM模糊客户区域

 

渲染模糊效果对于CPU和图形处理单元(GPU)来讲都是资源密集型操作。应用程序开发者应该考虑客户区模糊的实现,以便于它不消耗过多的资源。你应该使用特别注意下面情况:

 

1. 当你希望客户区模糊的大小是明显的,即便模糊区域本身没有发生刷新。模糊必须在任何窗体模糊区域下发生刷新的情况下被渲染,这会导致CPU和GPU的消耗。另外,在窗体上的操作(Move/Resize/Transitions)会导致更多的消耗。

2. 当你希望在模糊客户区有明显刷新。这会在每个刷新上需要模糊重绘并且会消耗很多资源。

3. 如果模糊预计将包括一个明显的区域,并且了预计刷新到该区域,我们强列建议你不要模糊客户区。

 

 

Custom Window Frame Using DWM:使用DWM扩展窗体边框 

 

这篇文章演示了如何使用桌面管理器(DWM)API为你的应用程序创建自定义窗体边框。

1.  介绍

2.  扩展客户边框

3.  去除标准边框

4.  在扩展边框窗体上绘制

5.  为自定义边框启用点击测试

6.  附录A:例子窗体过程

7.  附录B:绘制标题栏

8.  附录C:HitTestNCA函数

9.  相关主题

 

1.介绍

 

在Windows Vista和之后版本,应用程序的非客户区区域的显示(标题栏,图标,窗体边框,标题按钮)由DWM控制。使用DWM API,你可以改变DWM渲染窗体边框的方式。

DWM API的其中一个功能是能够扩展应用程序边框到客户区。它允许你整合客户UI元素——例如工具栏——到边框,给UI控件一个在应用程序界面中更加突出的位置。例如,在Windows Vista 上的Windows Internet Explorer 7通过扩展边框顶部边框来整合导航栏到窗体边框上,屏幕截图如下显示。


扩展窗体边框的功能也允许你创建自定义边框同时保持窗体的外观和感觉。例如,Microsoft Office Word 2007绘制Office按钮和快速访问工具栏内置到自定义边框同时提供标准最小化,最大小和关闭标题按钮,屏幕截图如下显示。

 


2.扩展客户边框

 

扩展边框到客户区的功能通过DwmExtendFrameIntoClientArea函数来实现。为了扩展边框,将目标窗体的句柄和边距插入值一起传递给DwmExtendFrameIntoClientArea。边距插入值决定了在窗体的四个边上扩展多少宽度的边框。

下面的代码演示了使用DwmExtendFrameIntoClientArea来扩展边框。

C:

  1. // Handle the window activation.   
  2. if (message == WM_ACTIVATE)  
  3. {  
  4.    // Extend the frame into the client area.   
  5.    MARGINS margins;  
  6.    
  7.    margins.cxLeftWidth = LEFTEXTENDWIDTH;      // 8   
  8.    margins.cxRightWidth = RIGHTEXTENDWIDTH;    // 8   
  9.    margins.cyBottomHeight = BOTTOMEXTENDWIDTH; // 20   
  10.    margins.cyTopHeight = TOPEXTENDWIDTH;       // 27   
  11.    
  12.    hr = DwmExtendFrameIntoClientArea(hWnd, &margins);  
  13.    
  14.    if (!SUCCEEDED(hr))  
  15.     {  
  16.        // Handle the error.   
  17.     }  
  18.    
  19.    fCallDWP = true;  
  20.    lRet = 0;  
  21. }  
// Handle the window activation.
if (message == WM_ACTIVATE)
{
   // Extend the frame into the client area.
   MARGINS margins;
 
   margins.cxLeftWidth = LEFTEXTENDWIDTH;      // 8
   margins.cxRightWidth = RIGHTEXTENDWIDTH;    // 8
   margins.cyBottomHeight = BOTTOMEXTENDWIDTH; // 20
   margins.cyTopHeight = TOPEXTENDWIDTH;       // 27
 
   hr = DwmExtendFrameIntoClientArea(hWnd, &margins);
 
   if (!SUCCEEDED(hr))
    {
       // Handle the error.
    }
 
   fCallDWP = true;
   lRet = 0;
}

注意,边框扩展是在WM_ACTIVATE消息中完成而不是在WM_CREATE消息中。这保证窗体在默认大小以及在最大化的时候边框扩展被妥善处理。

下面的图片展示了一个标准窗体边框(在左边)和相同边框扩展的(在右边)。边框使用上面例子代码扩展以及默认的Microsoft Visual Studio的 WNDCLASS/WNDCLASSEX背景(COLOR_WINDOW+1)。

 

这两个窗体的显示不同是十分细小的。两个窗体之间的唯一不同点是在左边窗体中客户区中的细黑线边框在右边窗体中不存在。失去边框的原因是它纳入到扩展边框,但是客户区的其余部分没有。为了扩展边框可以被看见,边框每个边的扩展区域必须有透明度为零的像素数据。在客户区周围的黑色边框有所有颜色值(红,蓝,绿和透明度)设置为零的像素数据。背景其余部分没有透明度设置为零的,所有扩展边框的其余部分是不可见的。

确保扩展边框可见的最方便的方法就是绘制整个客户区为黑色。为了实现这个,将你的WNDCLASS orWNDCLASSEX结构的hbrBackground成员初始为BLACK_BRUSH画刷的句柄。下面的图片显示相同的标准边框(左边)和扩展边框(右边)。然而这次,hbrBackground设置为从GetStockObject 函数获取的BLACK_BRUSH句柄。


 

3.去除标准边框

 

在你扩展你的应用程序边框并且使它可见之后,你可以去除标准边框。去除标准边框允许你控制边框每一边的宽度而不是简单的扩展标准边框。

要去掉标准窗体边框,你必须处理WM_NCCALCSIZE消息,当它的wParam值为TRUE的时候返回值为0。做了这些,你的应用程序可以使整个窗体区域做为客户区域,并且去除标准边框。

处理WM_NCCALCSIZE消息的结果是直到客户区域需要重新改变大小时可见。直到那时,窗体显示的初始视图带着标准边框和扩展边。为了解决这个问题,你必须要么重新调整你的窗体的尺寸,要么在窗体创建的时候执行一个初始WM_NCCALCSIZE消息的动作。这可以通过使用SetWindowPos函数移动你的窗体并重新调整窗体大小来实现。下面的代码演示了使用当前窗体矩形属性和SWP_FRAMECHANGED标志的SetWindowPos调用来强制发送WM_NCCALCSIZE消息。

C:

  1. // Handle window creation.   
  2. if (message == WM_CREATE)  
  3. {  
  4.    RECT rcClient;  
  5.    GetWindowRect(hWnd, &rcClient);  
  6.    
  7.    // Inform the application of the frame change.   
  8.    SetWindowPos(hWnd,  
  9.                  NULL,  
  10.                  rcClient.left, rcClient.top,  
  11.                  RECTWIDTH(rcClient),RECTHEIGHT(rcClient),  
  12.                  SWP_FRAMECHANGED);  
  13.    
  14.    fCallDWP = true;  
  15.    lRet = 0;  
  16. }  
// Handle window creation.
if (message == WM_CREATE)
{
   RECT rcClient;
   GetWindowRect(hWnd, &rcClient);
 
   // Inform the application of the frame change.
   SetWindowPos(hWnd,
                 NULL,
                 rcClient.left, rcClient.top,
                 RECTWIDTH(rcClient),RECTHEIGHT(rcClient),
                 SWP_FRAMECHANGED);
 
   fCallDWP = true;
   lRet = 0;
}

下面的图片显示了标准边框(左边)和最新的没有标准的扩展边框(右边)。

 

 

4.在扩展边框中绘制

 

通过去掉标准边框,你会失去应用程序图标和标题的自动绘制。为了将这些添加回你的应用程序,你必须自已绘制他们。为了做这些,首先看看你的客户区发生了哪些变化。

随着标准边框的去除,你的客户区现在包含整个窗体,包括扩展边框。这包含标题按钮绘制的区域。在下面的边与对的比较中,标准边框和自定义扩展边框的客户区都用红色高亮显示。标准边框窗体的客户区(左边)是黑色区域。在扩展边框窗体上(右边),客户区是整个窗体。

因为整个窗体是你的客户区,你可以简单的绘制你想要的东西到扩展边框上。为了给你的应用程序添加标题,只需要在合适的区域绘制文本。下面的图片显示了主题字体绘制在自定义的标题栏上。标题使用DrawThemeTextEx函数绘制。想查看绘制标题的代码,请看:AppendixB: Painting the Caption Title.

注意  当你在自定义边框中绘制的时候,放置UI控件时请小心。因为整个窗体是你的客户区,你必须调整你每个边框的UI控件摆放,如果你不希望他们出现在你的扩展边框上。


5.给你的扩展边框启用点击测试

 

去除标准边框的一方面影响就是失去默认的调整大小和移动功能。为了你的应用程序能够正确的模拟标准窗体行为,你将需要实现逻辑来处理标题按钮点击测试和边框调整大小/移动。

给标题按钮点击测试,DWM提供了DwmDefWindowProc函数。自定义边框情况下,为了正确的点击测试标题按钮,消息必须首先被传递到DwmDefWindowProc处理。如果消息被处理了,DwmDefWindowProc返回TRUE,如果返回FALSE,那么消息没有被处理。如果消息没有被DwmDefWindowProc处理,你的应用程序应该自己处理此消息或传递消息给DefWindowProc.

为了实现边框调整大小和移动功能,你的应用程序必须提供点击测试逻辑和处理边框点击测试消息。边框点击测试消息通过WM_NCHITTEST消息发送给你,尽管你的应用程序创建了一个自定义边框的窗体而不是标准边框窗体。下面的代码演示了当DwmDefWindowProc没有处理消息时我们自己处理WM_NCHITTEST消息。想看所调用的HitTestNCA函数的代码,请看AppendixC: HitTestNCA Function.

C:

  1. // Handle hit testing in the NCA if nothandled by DwmDefWindowProc.   
  2. if ((message == WM_NCHITTEST) &&(lRet == 0))  
  3. {  
  4.    lRet = HitTestNCA(hWnd, wParam, lParam);  
  5.    
  6.    if (lRet != HTNOWHERE)  
  7.     {  
  8.        fCallDWP = false;  
  9.     }  
  10. }  
  11.    
// Handle hit testing in the NCA if nothandled by DwmDefWindowProc.
if ((message == WM_NCHITTEST) &&(lRet == 0))
{
   lRet = HitTestNCA(hWnd, wParam, lParam);
 
   if (lRet != HTNOWHERE)
    {
       fCallDWP = false;
    }
}
 



7.附录A:窗体过程例子


下面的例子演示了一个窗体过程以及它支持用来创建自定义边框应用程序的工作函数。

  1. //   
  2. // Main WinProc.   
  3. //   
  4. LRESULT CALLBACK WndProc(HWND hWnd, UINTmessage, WPARAM wParam, LPARAM lParam)  
  5. {  
  6.    bool fCallDWP = true;  
  7.    BOOL fDwmEnabled = FALSE;  
  8.    LRESULT lRet = 0;  
  9.    HRESULT hr = S_OK;  
  10.    
  11.    // Winproc worker for custom frame issues.   
  12.    hr = DwmIsCompositionEnabled(&fDwmEnabled)  
  13.    if (SUCCEEDED(hr))  
  14.     {  
  15.        lRet = CustomCaptionProc(hWnd,message, wParam, lParam, &fCallDWP);  
  16.     }  
  17.    
  18.    // Winproc worker for the rest of the application.   
  19.    if (fCallDWP)  
  20.     {  
  21.        lRet = AppWinProc(hWnd, message, wParam, lParam);  
  22.     }  
  23.    return lRet;  
  24. }  
  25.    
  26. //   
  27. // Message handler for handling the customcaption messages.   
  28. //   
  29. LRESULT CustomCaptionProc(HWND hWnd, UINTmessage, WPARAM wParam, LPARAM lParam, bool* pfCallDWP)  
  30. {  
  31.    LRESULT lRet = 0;  
  32.    HRESULT hr = S_OK;  
  33.    bool fCallDWP = true// Pass on to DefWindowProc?   
  34.    
  35.     fCallDWP = !DwmDefWindowProc(hWnd, message,wParam, lParam, &lRet);  
  36.    
  37.    // Handle window creation.   
  38.    if (message == WM_CREATE)  
  39.     {  
  40.        RECT rcClient;  
  41.        GetWindowRect(hWnd, &rcClient);  
  42.    
  43.        // Inform application of the frame change.   
  44.         SetWindowPos(hWnd,  
  45.                      NULL,  
  46.                      rcClient.left,rcClient.top,  
  47.                      RECTWIDTH(rcClient),RECTHEIGHT(rcClient),  
  48.                      SWP_FRAMECHANGED);  
  49.    
  50.        fCallDWP = true;  
  51.        lRet = 0;  
  52.     }  
  53.    
  54.    // Handle window activation.   
  55.    if (message == WM_ACTIVATE)  
  56.     {  
  57.        // Extend the frame into the client area.   
  58.        MARGINS margins;  
  59.    
  60.        margins.cxLeftWidth = LEFTEXTENDWIDTH;      // 8   
  61.        margins.cxRightWidth = RIGHTEXTENDWIDTH;   // 8   
  62.        margins.cyBottomHeight = BOTTOMEXTENDWIDTH; // 20   
  63.        margins.cyTopHeight = TOPEXTENDWIDTH;       // 27   
  64.    
  65.        hr = DwmExtendFrameIntoClientArea(hWnd, &margins);  
  66.    
  67.        if (!SUCCEEDED(hr))  
  68.        {  
  69.            // Handle error.   
  70.         }  
  71.    
  72.        fCallDWP = true;  
  73.        lRet = 0;  
  74.     }  
  75.    
  76.    if (message == WM_PAINT)  
  77.     {  
  78.        HDC hdc;  
  79.        {  
  80.            hdc = BeginPaint(hWnd, &ps);  
  81.            PaintCustomCaption(hWnd, hdc);  
  82.            EndPaint(hWnd, &ps);  
  83.        }  
  84.    
  85.        fCallDWP = true;  
  86.        lRet = 0;  
  87.     }  
  88.    
  89.    // Handle the non-client size message.   
  90.    if ((message == WM_NCCALCSIZE) && (wParam == TRUE))  
  91.     {  
  92.        // Calculate new NCCALCSIZE_PARAMS based on custom NCA inset.   
  93.        NCCALCSIZE_PARAMS *pncsp =reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);  
  94.    
  95.        pncsp->rgrc[0].left   =pncsp->rgrc[0].left   + 0;  
  96.        pncsp->rgrc[0].top    =pncsp->rgrc[0].top    + 0;  
  97.        pncsp->rgrc[0].right  =pncsp->rgrc[0].right  - 0;  
  98.        pncsp->rgrc[0].bottom = pncsp->rgrc[0].bottom - 0;  
  99.    
  100.        lRet = 0;  
  101.          
  102.        // No need to pass the message on to the DefWindowProc.   
  103.        fCallDWP = false;  
  104.     }  
  105.    
  106.    // Handle hit testing in the NCA if not handled by DwmDefWindowProc.   
  107.    if ((message == WM_NCHITTEST) && (lRet == 0))  
  108.     {  
  109.        lRet = HitTestNCA(hWnd, wParam, lParam);  
  110.    
  111.        if (lRet != HTNOWHERE)  
  112.        {  
  113.            fCallDWP = false;  
  114.        }  
  115.     }  
  116.    
  117.    *pfCallDWP = fCallDWP;  
  118.    
  119.    return lRet;  
  120. }  
  121.    
  122. //   
  123. // Message handler for the application.   
  124. //   
  125. LRESULT AppWinProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)  
  126. {  
  127.    int wmId, wmEvent;  
  128.    PAINTSTRUCT ps;  
  129.    HDC hdc;  
  130.    HRESULT hr;  
  131.    LRESULT result = 0;  
  132.    
  133.    switch (message)  
  134.     {  
  135.        case WM_CREATE:  
  136.            {}  
  137.            break;  
  138.        case WM_COMMAND:  
  139.            wmId    = LOWORD(wParam);  
  140.            wmEvent = HIWORD(wParam);  
  141.    
  142.            // Parse the menu selections:   
  143.            switch (wmId)  
  144.            {  
  145.                 default:  
  146.                     return DefWindowProc(hWnd,message, wParam, lParam);  
  147.            }  
  148.            break;  
  149.        case WM_PAINT:  
  150.            {  
  151.                 hdc = BeginPaint(hWnd,&ps);  
  152.                 PaintCustomCaption(hWnd, hdc);  
  153.                  
  154.                 // Add any drawing code here...   
  155.      
  156.                 EndPaint(hWnd, &ps);  
  157.            }  
  158.            break;  
  159.        case WM_DESTROY:  
  160.            PostQuitMessage(0);  
  161.            break;  
  162.        default:  
  163.            return DefWindowProc(hWnd, message, wParam, lParam);  
  164.     }  
  165.    return 0;  
  166. }  
  167.    
//
// Main WinProc.
//
LRESULT CALLBACK WndProc(HWND hWnd, UINTmessage, WPARAM wParam, LPARAM lParam)
{
   bool fCallDWP = true;
   BOOL fDwmEnabled = FALSE;
   LRESULT lRet = 0;
   HRESULT hr = S_OK;
 
   // Winproc worker for custom frame issues.
   hr = DwmIsCompositionEnabled(&fDwmEnabled)
   if (SUCCEEDED(hr))
    {
       lRet = CustomCaptionProc(hWnd,message, wParam, lParam, &fCallDWP);
    }
 
   // Winproc worker for the rest of the application.
   if (fCallDWP)
    {
       lRet = AppWinProc(hWnd, message, wParam, lParam);
    }
   return lRet;
}
 
//
// Message handler for handling the customcaption messages.
//
LRESULT CustomCaptionProc(HWND hWnd, UINTmessage, WPARAM wParam, LPARAM lParam, bool* pfCallDWP)
{
   LRESULT lRet = 0;
   HRESULT hr = S_OK;
   bool fCallDWP = true; // Pass on to DefWindowProc?
 
    fCallDWP = !DwmDefWindowProc(hWnd, message,wParam, lParam, &lRet);
 
   // Handle window creation.
   if (message == WM_CREATE)
    {
       RECT rcClient;
       GetWindowRect(hWnd, &rcClient);
 
       // Inform application of the frame change.
        SetWindowPos(hWnd,
                     NULL,
                     rcClient.left,rcClient.top,
                     RECTWIDTH(rcClient),RECTHEIGHT(rcClient),
                     SWP_FRAMECHANGED);
 
       fCallDWP = true;
       lRet = 0;
    }
 
   // Handle window activation.
   if (message == WM_ACTIVATE)
    {
       // Extend the frame into the client area.
       MARGINS margins;
 
       margins.cxLeftWidth = LEFTEXTENDWIDTH;      // 8
       margins.cxRightWidth = RIGHTEXTENDWIDTH;   // 8
       margins.cyBottomHeight = BOTTOMEXTENDWIDTH; // 20
       margins.cyTopHeight = TOPEXTENDWIDTH;       // 27
 
       hr = DwmExtendFrameIntoClientArea(hWnd, &margins);
 
       if (!SUCCEEDED(hr))
       {
           // Handle error.
        }
 
       fCallDWP = true;
       lRet = 0;
    }
 
   if (message == WM_PAINT)
    {
       HDC hdc;
       {
           hdc = BeginPaint(hWnd, &ps);
           PaintCustomCaption(hWnd, hdc);
           EndPaint(hWnd, &ps);
       }
 
       fCallDWP = true;
       lRet = 0;
    }
 
   // Handle the non-client size message.
   if ((message == WM_NCCALCSIZE) && (wParam == TRUE))
    {
       // Calculate new NCCALCSIZE_PARAMS based on custom NCA inset.
       NCCALCSIZE_PARAMS *pncsp =reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);
 
       pncsp->rgrc[0].left   =pncsp->rgrc[0].left   + 0;
       pncsp->rgrc[0].top    =pncsp->rgrc[0].top    + 0;
       pncsp->rgrc[0].right  =pncsp->rgrc[0].right  - 0;
       pncsp->rgrc[0].bottom = pncsp->rgrc[0].bottom - 0;
 
       lRet = 0;
       
       // No need to pass the message on to the DefWindowProc.
       fCallDWP = false;
    }
 
   // Handle hit testing in the NCA if not handled by DwmDefWindowProc.
   if ((message == WM_NCHITTEST) && (lRet == 0))
    {
       lRet = HitTestNCA(hWnd, wParam, lParam);
 
       if (lRet != HTNOWHERE)
       {
           fCallDWP = false;
       }
    }
 
   *pfCallDWP = fCallDWP;
 
   return lRet;
}
 
//
// Message handler for the application.
//
LRESULT AppWinProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
{
   int wmId, wmEvent;
   PAINTSTRUCT ps;
   HDC hdc;
   HRESULT hr;
   LRESULT result = 0;
 
   switch (message)
    {
       case WM_CREATE:
           {}
           break;
       case WM_COMMAND:
           wmId    = LOWORD(wParam);
           wmEvent = HIWORD(wParam);
 
           // Parse the menu selections:
           switch (wmId)
           {
                default:
                    return DefWindowProc(hWnd,message, wParam, lParam);
           }
           break;
       case WM_PAINT:
           {
                hdc = BeginPaint(hWnd,&ps);
                PaintCustomCaption(hWnd, hdc);
               
                // Add any drawing code here...
   
                EndPaint(hWnd, &ps);
           }
           break;
       case WM_DESTROY:
           PostQuitMessage(0);
           break;
       default:
           return DefWindowProc(hWnd, message, wParam, lParam);
    }
   return 0;
}
 


8.附录B:绘制标题栏

 

下面的代码演示了如何绘制一个标题栏到扩展边框上。这个函数在BeginPaintEndPaint调用之间被调用。

  1. // Paint the title on the custom frame.   
  2. void PaintCustomCaption(HWND hWnd, HDC hdc)  
  3. {  
  4.    RECT rcClient;  
  5.    GetClientRect(hWnd, &rcClient);  
  6.    
  7.     HTHEME hTheme = OpenThemeData(NULL,L"CompositedWindow::Window");  
  8.    if (hTheme)  
  9.     {  
  10.        HDC hdcPaint = CreateCompatibleDC(hdc);  
  11.        if (hdcPaint)  
  12.        {  
  13.            int cx = RECTWIDTH(rcClient);  
  14.            int cy = RECTHEIGHT(rcClient);  
  15.    
  16.             // Define the BITMAPINFO structureused to draw text.   
  17.            // Note that biHeight is negative. This is done because   
  18.            // DrawThemeTextEx() needs the bitmap to be in top-to-bottom   
  19.            // order.   
  20.            BITMAPINFO dib = { 0 };  
  21.            dib.bmiHeader.biSize            =sizeof(BITMAPINFOHEADER);  
  22.            dib.bmiHeader.biWidth           =cx;  
  23.            dib.bmiHeader.biHeight          =-cy;  
  24.            dib.bmiHeader.biPlanes          =1;  
  25.            dib.bmiHeader.biBitCount        =BIT_COUNT;  
  26.            dib.bmiHeader.biCompression     =BI_RGB;  
  27.    
  28.            HBITMAP hbm = CreateDIBSection(hdc, &dib, DIB_RGB_COLORS, NULL,NULL, 0);  
  29.            if (hbm)  
  30.            {  
  31.                 HBITMAP hbmOld =(HBITMAP)SelectObject(hdcPaint, hbm);  
  32.    
  33.                 // Setup the theme drawingoptions.   
  34.                 DTTOPTS DttOpts ={sizeof(DTTOPTS)};  
  35.                 DttOpts.dwFlags =DTT_COMPOSITED | DTT_GLOWSIZE;  
  36.                 DttOpts.iGlowSize = 15;  
  37.    
  38.                 // Select a font.   
  39.                 LOGFONT lgFont;  
  40.                 HFONT hFontOld = NULL;  
  41.                 if(SUCCEEDED(GetThemeSysFont(hTheme, TMT_CAPTIONFONT, &lgFont)))  
  42.                 {  
  43.                     HFONT hFont = CreateFontIndirect(&lgFont);  
  44.                     hFontOld = (HFONT)SelectObject(hdcPaint, hFont);  
  45.                 }  
  46.    
  47.                 // Draw the title.   
  48.                 RECT rcPaint = rcClient;  
  49.                 rcPaint.top += 8;  
  50.                 rcPaint.right -= 125;  
  51.                 rcPaint.left += 8;  
  52.                 rcPaint.bottom = 50;  
  53.                 DrawThemeTextEx(hTheme,  
  54.                                 hdcPaint,  
  55.                                 0, 0,  
  56.                                 szTitle,  
  57.                                 -1,  
  58.                                 DT_LEFT |DT_WORD_ELLIPSIS,  
  59.                                 &rcPaint,  
  60.                                 &DttOpts);  
  61.    
  62.                 // Blit text to the frame.   
  63.                 BitBlt(hdc, 0, 0, cx, cy, hdcPaint,0, 0, SRCCOPY);  
  64.    
  65.                 SelectObject(hdcPaint, hbmOld);  
  66.                 if (hFontOld)  
  67.                 {  
  68.                     SelectObject(hdcPaint,hFontOld);  
  69.                 }  
  70.                 DeleteObject(hbm);  
  71.            }  
  72.            DeleteDC(hdcPaint);  
  73.        }  
  74.        CloseThemeData(hTheme);  
  75.     }  
  76. }  
  77.    
// Paint the title on the custom frame.
void PaintCustomCaption(HWND hWnd, HDC hdc)
{
   RECT rcClient;
   GetClientRect(hWnd, &rcClient);
 
    HTHEME hTheme = OpenThemeData(NULL,L"CompositedWindow::Window");
   if (hTheme)
    {
       HDC hdcPaint = CreateCompatibleDC(hdc);
       if (hdcPaint)
       {
           int cx = RECTWIDTH(rcClient);
           int cy = RECTHEIGHT(rcClient);
 
            // Define the BITMAPINFO structureused to draw text.
           // Note that biHeight is negative. This is done because
           // DrawThemeTextEx() needs the bitmap to be in top-to-bottom
           // order.
           BITMAPINFO dib = { 0 };
           dib.bmiHeader.biSize            =sizeof(BITMAPINFOHEADER);
           dib.bmiHeader.biWidth           =cx;
           dib.bmiHeader.biHeight          =-cy;
           dib.bmiHeader.biPlanes          =1;
           dib.bmiHeader.biBitCount        =BIT_COUNT;
           dib.bmiHeader.biCompression     =BI_RGB;
 
           HBITMAP hbm = CreateDIBSection(hdc, &dib, DIB_RGB_COLORS, NULL,NULL, 0);
           if (hbm)
           {
                HBITMAP hbmOld =(HBITMAP)SelectObject(hdcPaint, hbm);
 
                // Setup the theme drawingoptions.
                DTTOPTS DttOpts ={sizeof(DTTOPTS)};
                DttOpts.dwFlags =DTT_COMPOSITED | DTT_GLOWSIZE;
                DttOpts.iGlowSize = 15;
 
                // Select a font.
                LOGFONT lgFont;
                HFONT hFontOld = NULL;
                if(SUCCEEDED(GetThemeSysFont(hTheme, TMT_CAPTIONFONT, &lgFont)))
                {
                    HFONT hFont = CreateFontIndirect(&lgFont);
                    hFontOld = (HFONT)SelectObject(hdcPaint, hFont);
                }
 
                // Draw the title.
                RECT rcPaint = rcClient;
                rcPaint.top += 8;
                rcPaint.right -= 125;
                rcPaint.left += 8;
                rcPaint.bottom = 50;
                DrawThemeTextEx(hTheme,
                                hdcPaint,
                                0, 0,
                                szTitle,
                                -1,
                                DT_LEFT |DT_WORD_ELLIPSIS,
                                &rcPaint,
                                &DttOpts);
 
                // Blit text to the frame.
                BitBlt(hdc, 0, 0, cx, cy, hdcPaint,0, 0, SRCCOPY);
 
                SelectObject(hdcPaint, hbmOld);
                if (hFontOld)
                {
                    SelectObject(hdcPaint,hFontOld);
                }
                DeleteObject(hbm);
           }
           DeleteDC(hdcPaint);
       }
       CloseThemeData(hTheme);
    }
}
 



9.附录C:HitTestNCA函数

 

下面的代码显示了在EnablingHit Testing for the Custom Frame.中使用的HitTestNCA函数。这个函数处理了当DwmDefWindowProc不能处理WM_NCHITTEST时的点击测试逻辑。

  1. // Hit test the frame for resizing andmoving.   
  2. LRESULT HitTestNCA(HWND hWnd, WPARAMwParam, LPARAM lParam)  
  3. {  
  4.    // Get the point coordinates for the hit test.   
  5.    POINT ptMouse = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};  
  6.    
  7.    // Get the window rectangle.   
  8.    RECT rcWindow;  
  9.    GetWindowRect(hWnd, &rcWindow);  
  10.    
  11.    // Get the frame rectangle, adjusted for the style without a caption.   
  12.    RECT rcFrame = { 0 };  
  13.    AdjustWindowRectEx(&rcFrame, WS_OVERLAPPEDWINDOW & ~WS_CAPTION,FALSE, NULL);  
  14.    
  15.    // Determine if the hit test is for resizing. Default middle (1,1).   
  16.    USHORT uRow = 1;  
  17.    USHORT uCol = 1;  
  18.    bool fOnResizeBorder = false;  
  19.    
  20.    // Determine if the point is at the top or bottom of the window.   
  21.    if (ptMouse.y >= rcWindow.top && ptMouse.y < rcWindow.top+ TOPEXTENDWIDTH)  
  22.     {  
  23.        fOnResizeBorder = (ptMouse.y < (rcWindow.top - rcFrame.top));  
  24.        uRow = 0;  
  25.     }  
  26.    else if (ptMouse.y < rcWindow.bottom && ptMouse.y >=rcWindow.bottom - BOTTOMEXTENDWIDTH)  
  27.     {  
  28.        uRow = 2;  
  29.     }  
  30.    
  31.    // Determine if the point is at the left or right of the window.   
  32.    if (ptMouse.x >= rcWindow.left && ptMouse.x <rcWindow.left + LEFTEXTENDWIDTH)  
  33.     {  
  34.        uCol = 0; // left side   
  35.     }  
  36.    else if (ptMouse.x < rcWindow.right && ptMouse.x >= rcWindow.right- RIGHTEXTENDWIDTH)  
  37.     {  
  38.        uCol = 2; // right side   
  39.     }  
  40.    
  41.    // Hit test (HTTOPLEFT, ... HTBOTTOMRIGHT)   
  42.    LRESULT hitTests[3][3] =  
  43.     {  
  44.        { HTTOPLEFT,    fOnResizeBorder ?HTTOP : HTCAPTION,    HTTOPRIGHT },  
  45.        { HTLEFT,       HTNOWHERE,     HTRIGHT },  
  46.        { HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT },  
  47.    };  
  48.    
  49.    return hitTests[uRow][uCol];  
  50. }  
  51.    
  52.    
// Hit test the frame for resizing andmoving.
LRESULT HitTestNCA(HWND hWnd, WPARAMwParam, LPARAM lParam)
{
   // Get the point coordinates for the hit test.
   POINT ptMouse = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
 
   // Get the window rectangle.
   RECT rcWindow;
   GetWindowRect(hWnd, &rcWindow);
 
   // Get the frame rectangle, adjusted for the style without a caption.
   RECT rcFrame = { 0 };
   AdjustWindowRectEx(&rcFrame, WS_OVERLAPPEDWINDOW & ~WS_CAPTION,FALSE, NULL);
 
   // Determine if the hit test is for resizing. Default middle (1,1).
   USHORT uRow = 1;
   USHORT uCol = 1;
   bool fOnResizeBorder = false;
 
   // Determine if the point is at the top or bottom of the window.
   if (ptMouse.y >= rcWindow.top && ptMouse.y < rcWindow.top+ TOPEXTENDWIDTH)
    {
       fOnResizeBorder = (ptMouse.y < (rcWindow.top - rcFrame.top));
       uRow = 0;
    }
   else if (ptMouse.y < rcWindow.bottom && ptMouse.y >=rcWindow.bottom - BOTTOMEXTENDWIDTH)
    {
       uRow = 2;
    }
 
   // Determine if the point is at the left or right of the window.
   if (ptMouse.x >= rcWindow.left && ptMouse.x <rcWindow.left + LEFTEXTENDWIDTH)
    {
       uCol = 0; // left side
    }
   else if (ptMouse.x < rcWindow.right && ptMouse.x >= rcWindow.right- RIGHTEXTENDWIDTH)
    {
       uCol = 2; // right side
    }
 
   // Hit test (HTTOPLEFT, ... HTBOTTOMRIGHT)
   LRESULT hitTests[3][3] =
    {
       { HTTOPLEFT,    fOnResizeBorder ?HTTOP : HTCAPTION,    HTTOPRIGHT },
       { HTLEFT,       HTNOWHERE,     HTRIGHT },
       { HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT },
   };
 
   return hitTests[uRow][uCol];
}
 
 
下面是我用 Delphi 做的例子,可能会与上面的 C 语言版本稍有不同:

 


贴上.dfm和.pas的代码,大家参考一下吧!
大家最好先自己动手,遇到问题再我的代码。

LearnDWMBaseForm.dfm:

  1. object frmBaseDWMLearn: TfrmBaseDWMLearn  
  2.   Left = 0  
  3.   Top = 0  
  4.   Caption = 'Desktop Window Manager'  
  5.   ClientHeight = 290  
  6.   ClientWidth = 554  
  7.   Color = clBlack  
  8.   Font.Charset = DEFAULT_CHARSET  
  9.   Font.Color = clWindowText  
  10.   Font.Height = -11  
  11.   Font.Name = 'Tahoma'  
  12.   Font.Style = []  
  13.   OldCreateOrder = False  
  14.   OnCreate = FormCreate  
  15.   PixelsPerInch = 96  
  16.   TextHeight = 13  
  17. end  
object frmBaseDWMLearn: TfrmBaseDWMLearn
  Left = 0
  Top = 0
  Caption = 'Desktop Window Manager'
  ClientHeight = 290
  ClientWidth = 554
  Color = clBlack
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end

LearnDWMBaseForm.pas:

  1. unit LearnDWMBaseForm;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, Dwmapi, UxTheme, ExtCtrls;  
  8.   
  9. type  
  10.   TfrmBaseDWMLearn = class(TForm)  
  11.     procedure FormCreate(Sender: TObject);  
  12.   private  
  13.     FMarInset: UxTheme.TMargins;  
  14.     procedure DoWMNCCalcSize(var Message: TWMNCCalcSize);  
  15.     procedure WMPAINT(var Message:TMessage);message WM_PAINT;  
  16.     procedure WMNCHITTEST(var Message:TMessage);message WM_NCHITTEST;  
  17.     procedure WMNCCALCSIZE(var Message:TMessage);message WM_NCCALCSIZE;  
  18.     procedure WMNCACTIVATE(var Message:TMessage);message WM_NCACTIVATE;  
  19.     procedure WMDWMCOMPOSITIONCHANGED(var Message:TMessage);message WM_DWMCOMPOSITIONCHANGED;  
  20.     procedure WMDWMNCRENDERINGCHANGED(var Message:TMessage);message WM_DWMNCRENDERINGCHANGED;  
  21.     procedure WMDWMWINDOWMAXIMIZEDCHANGE(var Message:TMessage);message WM_DWMWINDOWMAXIMIZEDCHANGE;  
  22.     { Private declarations }  
  23.   public  
  24.     constructor Create(AOwner:TComponent);override;  
  25.     { Public declarations }  
  26.   end;  
  27.   
  28. var  
  29.   frmBaseDWMLearn: TfrmBaseDWMLearn;  
  30.   
  31. implementation  
  32.   
  33. {$R *.dfm}  
  34.   
  35. { TForm2 }  
  36.   
  37. constructor TfrmBaseDWMLearn.Create(AOwner: TComponent);  
  38. begin  
  39.   FMarInset.cxLeftWidth:=8;  
  40.   FMarInset.cyTopHeight:=27;  
  41.   FMarInset.cxRightWidth:=8;  
  42.   FMarInset.cyBottomHeight:=27;  
  43.   inherited Create(AOwner);  
  44. end;  
  45.   
  46. procedure TfrmBaseDWMLearn.FormCreate(Sender: TObject);  
  47. var  
  48.   rcClient:TRect;  
  49. begin  
  50.   GetWindowRect(Handle,rcClient);  
  51.   SetWindowPos(Handle,0,  
  52.                 rcClient.Left,rcClient.Top,  
  53.                 rcClient.Right-rcClient.Left,  
  54.                 rcClient.Bottom-rcClient.Top,  
  55.                 SWP_FRAMECHANGED);  
  56. end;  
  57.   
  58. procedure TfrmBaseDWMLearn.WMDWMCOMPOSITIONCHANGED(var Message: TMessage);  
  59. begin  
  60.   Inherited;  
  61.   DwmExtendFrameIntoClientArea(Handle,FMarInset);  
  62. end;  
  63.   
  64. procedure TfrmBaseDWMLearn.WMDWMNCRENDERINGCHANGED(var Message: TMessage);  
  65. begin  
  66.   Inherited;  
  67.   DwmExtendFrameIntoClientArea(Handle,FMarInset);  
  68. end;  
  69.   
  70. procedure TfrmBaseDWMLearn.WMDWMWINDOWMAXIMIZEDCHANGE(var Message: TMessage);  
  71. begin  
  72.   Inherited;  
  73. end;  
  74.   
  75. procedure TfrmBaseDWMLearn.WMNCACTIVATE(var Message: TMessage);  
  76. begin  
  77.   Inherited;  
  78.   DwmExtendFrameIntoClientArea(Handle,FMarInset);  
  79. end;  
  80.   
  81. procedure TfrmBaseDWMLearn.WMNCCALCSIZE(var Message: TMessage);  
  82. begin  
  83.   if (Message.WParam=1then  
  84.   begin  
  85.     Inherited;  
  86.     DoWMNCCalcSize(TWMNCCalcSize(Message));  
  87.     Message.Result:=1;  
  88.   end  
  89.   else  
  90.   begin  
  91.     Inherited;  
  92.   end;  
  93. end;  
  94.   
  95. procedure TfrmBaseDWMLearn.DoWMNCCalcSize(var Message: TWMNCCalcSize);  
  96. var  
  97.   NCCalcSizeParams: PNCCalcSizeParams;  
  98.   tmpCaptionY:Integer;  
  99.   tmpFrameY:Integer;  
  100.   tmpFrameX:Integer;  
  101.   tmpTitleHeight:Integer;  
  102. begin  
  103.   NCCalcSizeParams:=Message.CalcSize_Params;  
  104.   tmpFrameY:=GetSystemMetrics(SM_CYFRAME);  
  105.   tmpFrameX:=GetSystemMetrics(SM_CXFRAME);  
  106.   tmpCaptionY:=GetSystemMetrics(SM_CYCAPTION);  
  107.   tmpTitleHeight:=tmpFrameY+tmpCaptionY;  
  108.   //标题栏高度   
  109.   Dec(NCCalcSizeParams.rgrc[0].Top,tmpTitleHeight);  
  110.   //左右下边框   
  111.   Dec(NCCalcSizeParams.rgrc[0].Left,tmpFrameX);  
  112.   Inc(NCCalcSizeParams.rgrc[0].Right,tmpFrameX);  
  113.   Inc(NCCalcSizeParams.rgrc[0].Bottom,tmpFrameY); //这句启用的时候窗体最大化或拖动右边框到屏幕边缘的时候整个窗体会黑掉   
  114. end;  
  115.   
  116.   
  117. procedure TfrmBaseDWMLearn.WMNCHITTEST(var Message: TMessage);  
  118. var  
  119.   dwStyle:Cardinal;  
  120.   rcWindow:TRect;  
  121.   rcFrame:TRect;  
  122.   ptMouse:TPoint;  
  123.   uRow,uCol:Integer;  
  124.   fOnResizeBorder:Boolean;  
  125. const  
  126.     // Hit test (HTTOPLEFT, ... HTBOTTOMRIGHT)   //fOnResizeBorder ? HTTOP :HTCAPTION   
  127.     hitTests:Array [0..2of Array [0..2of Cardinal =  
  128.     (  
  129.         ( HTTOPLEFT,    HTTOP,    HTTOPRIGHT ),  
  130.         ( HTLEFT,       HTNOWHERE,     HTRIGHT ),  
  131.         ( HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT )  
  132.     );  
  133. begin  
  134.   if Not DwmDefWindowProc(Handle,Message.Msg,Message.WParam,Message.LParam,Message.Result) then  
  135.   begin  
  136.     //下面这些是直接从MSDN的C语言例子翻译过来的。。请大家自便   
  137.     Inherited;  
  138.   
  139.     // Get the point coordinates for the hit test.   
  140.     ptMouse.X:=TWMNCHITTEST(Message).XPos;  
  141.     ptMouse.Y:=TWMNCHITTEST(Message).YPos;  
  142.   
  143.     // Get the window rectangle.   
  144.     GetWindowRect(Handle,rcWindow);  
  145.   
  146.     // Get the frame rectangle, adjusted for the style without a caption.   
  147.     rcFrame:=Rect(0,0,0,0);  
  148.     dwStyle:=WS_OVERLAPPEDWINDOW and Not WS_CAPTION;  
  149.     AdjustWindowRectEx(rcFrame, dwStyle, False, 0);  
  150.   
  151.     // Determine if the hit test is for resizing. Default middle (1,1).   
  152.     uRow:=1;  
  153.     uCol:=1;  
  154.     fOnResizeBorder:=False;  
  155.   
  156.     // Determine if the point is at the top or bottom of the window.   
  157.     if (ptMouse.y >= rcWindow.top) and (ptMouse.y < rcWindow.top + FMarInset.cyTopHeight) then  
  158.     begin  
  159.         fOnResizeBorder := (ptMouse.y < (rcWindow.top - rcFrame.top));  
  160.         uRow := 0;  
  161.     end;  
  162.   
  163.     if (ptMouse.y < rcWindow.bottom) and (ptMouse.y >= rcWindow.bottom - FMarInset.cyBottomHeight) then  
  164.     begin  
  165.         uRow := 2;  
  166.     end;  
  167.   
  168.     // Determine if the point is at the left or right of the window.   
  169.     if (ptMouse.x >= rcWindow.left) and (ptMouse.x < rcWindow.left + FMarInset.cxLeftWidth) then  
  170.     begin  
  171.         uCol := 0// left side   
  172.     end;  
  173.   
  174.     if (ptMouse.x < rcWindow.right) and (ptMouse.x >= rcWindow.right - FMarInset.cxRightWidth) then  
  175.     begin  
  176.         uCol := 2// right side   
  177.     end;  
  178.   
  179.   
  180.     Message.Result:=hitTests[uRow][uCol];  
  181.   
  182.     if (uRow=0and (uCol=1and Not fOnResizeBorder then  
  183.     begin  
  184.       Message.Result:=HTCAPTION;  
  185.     end;  
  186.   
  187.   end;  
  188. end;  
  189.   
  190. procedure TfrmBaseDWMLearn.WMPAINT(var Message: TMessage);  
  191. var  
  192.   hThm:HTHEME;  
  193.   hMemDC:HDC;  
  194.   bmpinfo:BITMAPINFO;  
  195.   hBmp:HBITMAP;  
  196.   hBmpOld:HGDIOBJ;  
  197.   tmpdttopts:DTTOPTS;  
  198.   rc:TRect;  
  199.   hr:HRESULT;  
  200.   p:Pointer;  
  201.   hFontOld:HGDIOBJ;  
  202. begin  
  203.   Inherited;  
  204.   //获取主题句柄   
  205.   hThm:=OpenThemeData(GetDesktopWindow(),PWideChar('TextStyle'));  
  206.   
  207.   //创建DIB   
  208.   hMemDC := CreateCompatibleDC(Canvas.Handle);  
  209.   FillChar(bmpinfo,SizeOf(bmpinfo),0);  
  210.   bmpinfo.bmiHeader.biSize := sizeof(bmpinfo.bmiHeader);  
  211.   bmpinfo.bmiHeader.biBitCount := 32;  
  212.   bmpinfo.bmiHeader.biCompression := BI_RGB;  
  213.   bmpinfo.bmiHeader.biPlanes := 1;  
  214.   bmpinfo.bmiHeader.biWidth := Width;  
  215.   bmpinfo.bmiHeader.biHeight := -FMarInset.cyTopHeight;  
  216.   hBmp := CreateDIBSection(hMemDC, &bmpinfo, DIB_RGB_COLORS, p, 00);  
  217.   
  218.   hBmpOld := SelectObject(hMemDC, hBmp);  
  219.   hFontOld:=SelectObject(hMemDC,Font.Handle);  
  220.   
  221.   //绘制选项   
  222.   FillChar(tmpdttopts,SizeOf(tmpdttopts),0);  
  223.   tmpdttopts.dwSize := sizeof(DTTOPTS);  
  224.   tmpdttopts.dwFlags := DTT_GLOWSIZE or DTT_COMPOSITED or DTT_TEXTCOLOR;  
  225.   
  226.   //发光的范围大小   
  227.   tmpdttopts.iGlowSize := 6;  
  228.   tmpdttopts.crText:=ColorToRGB(Font.Color);  
  229.   
  230.   //绘制文本   
  231.   rc := Rect(10,0,Width,FMarInset.cyTopHeight);  
  232.   hr := DrawThemeTextEx(hThm,hMemDC, TEXT_BODYTITLE, 0, Caption, -1, DT_LEFT or DT_VCENTER or DT_SINGLELINE , rc, tmpdttopts);  
  233.   
  234.   //绘制缓存   
  235.   if (FAILED(hr)) then Exit;  
  236.   
  237.   BitBlt(Canvas.Handle, 100, Width,FMarInset.cyTopHeight, hMemDC, 00, SRCCOPY);  
  238.   
  239.   SelectObject(hMemDC, hFontOld);  
  240.   SelectObject(hMemDC, hBmpOld);  
  241.   DeleteObject(hBmp);  
  242.   DeleteDC(hMemDC);  
  243.   CloseThemeData(hThm);  
  244.   
  245. end;  
  246.   
  247.   
  248. end.  
unit LearnDWMBaseForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Dwmapi, UxTheme, ExtCtrls;

type
  TfrmBaseDWMLearn = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    FMarInset: UxTheme.TMargins;
    procedure DoWMNCCalcSize(var Message: TWMNCCalcSize);
    procedure WMPAINT(var Message:TMessage);message WM_PAINT;
    procedure WMNCHITTEST(var Message:TMessage);message WM_NCHITTEST;
    procedure WMNCCALCSIZE(var Message:TMessage);message WM_NCCALCSIZE;
    procedure WMNCACTIVATE(var Message:TMessage);message WM_NCACTIVATE;
    procedure WMDWMCOMPOSITIONCHANGED(var Message:TMessage);message WM_DWMCOMPOSITIONCHANGED;
    procedure WMDWMNCRENDERINGCHANGED(var Message:TMessage);message WM_DWMNCRENDERINGCHANGED;
    procedure WMDWMWINDOWMAXIMIZEDCHANGE(var Message:TMessage);message WM_DWMWINDOWMAXIMIZEDCHANGE;
    { Private declarations }
  public
    constructor Create(AOwner:TComponent);override;
    { Public declarations }
  end;

var
  frmBaseDWMLearn: TfrmBaseDWMLearn;

implementation

{$R *.dfm}

{ TForm2 }

constructor TfrmBaseDWMLearn.Create(AOwner: TComponent);
begin
  FMarInset.cxLeftWidth:=8;
  FMarInset.cyTopHeight:=27;
  FMarInset.cxRightWidth:=8;
  FMarInset.cyBottomHeight:=27;
  inherited Create(AOwner);
end;

procedure TfrmBaseDWMLearn.FormCreate(Sender: TObject);
var
  rcClient:TRect;
begin
  GetWindowRect(Handle,rcClient);
  SetWindowPos(Handle,0,
                rcClient.Left,rcClient.Top,
                rcClient.Right-rcClient.Left,
                rcClient.Bottom-rcClient.Top,
                SWP_FRAMECHANGED);
end;

procedure TfrmBaseDWMLearn.WMDWMCOMPOSITIONCHANGED(var Message: TMessage);
begin
  Inherited;
  DwmExtendFrameIntoClientArea(Handle,FMarInset);
end;

procedure TfrmBaseDWMLearn.WMDWMNCRENDERINGCHANGED(var Message: TMessage);
begin
  Inherited;
  DwmExtendFrameIntoClientArea(Handle,FMarInset);
end;

procedure TfrmBaseDWMLearn.WMDWMWINDOWMAXIMIZEDCHANGE(var Message: TMessage);
begin
  Inherited;
end;

procedure TfrmBaseDWMLearn.WMNCACTIVATE(var Message: TMessage);
begin
  Inherited;
  DwmExtendFrameIntoClientArea(Handle,FMarInset);
end;

procedure TfrmBaseDWMLearn.WMNCCALCSIZE(var Message: TMessage);
begin
  if (Message.WParam=1) then
  begin
    Inherited;
    DoWMNCCalcSize(TWMNCCalcSize(Message));
    Message.Result:=1;
  end
  else
  begin
    Inherited;
  end;
end;

procedure TfrmBaseDWMLearn.DoWMNCCalcSize(var Message: TWMNCCalcSize);
var
  NCCalcSizeParams: PNCCalcSizeParams;
  tmpCaptionY:Integer;
  tmpFrameY:Integer;
  tmpFrameX:Integer;
  tmpTitleHeight:Integer;
begin
  NCCalcSizeParams:=Message.CalcSize_Params;
  tmpFrameY:=GetSystemMetrics(SM_CYFRAME);
  tmpFrameX:=GetSystemMetrics(SM_CXFRAME);
  tmpCaptionY:=GetSystemMetrics(SM_CYCAPTION);
  tmpTitleHeight:=tmpFrameY+tmpCaptionY;
  //标题栏高度
  Dec(NCCalcSizeParams.rgrc[0].Top,tmpTitleHeight);
  //左右下边框
  Dec(NCCalcSizeParams.rgrc[0].Left,tmpFrameX);
  Inc(NCCalcSizeParams.rgrc[0].Right,tmpFrameX);
  Inc(NCCalcSizeParams.rgrc[0].Bottom,tmpFrameY); //这句启用的时候窗体最大化或拖动右边框到屏幕边缘的时候整个窗体会黑掉
end;


procedure TfrmBaseDWMLearn.WMNCHITTEST(var Message: TMessage);
var
  dwStyle:Cardinal;
  rcWindow:TRect;
  rcFrame:TRect;
  ptMouse:TPoint;
  uRow,uCol:Integer;
  fOnResizeBorder:Boolean;
const
    // Hit test (HTTOPLEFT, ... HTBOTTOMRIGHT)   //fOnResizeBorder ? HTTOP :HTCAPTION
    hitTests:Array [0..2] of Array [0..2] of Cardinal =
    (
        ( HTTOPLEFT,    HTTOP,    HTTOPRIGHT ),
        ( HTLEFT,       HTNOWHERE,     HTRIGHT ),
        ( HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT )
    );
begin
  if Not DwmDefWindowProc(Handle,Message.Msg,Message.WParam,Message.LParam,Message.Result) then
  begin
    //下面这些是直接从MSDN的C语言例子翻译过来的。。请大家自便
    Inherited;

    // Get the point coordinates for the hit test.
    ptMouse.X:=TWMNCHITTEST(Message).XPos;
    ptMouse.Y:=TWMNCHITTEST(Message).YPos;

    // Get the window rectangle.
    GetWindowRect(Handle,rcWindow);

    // Get the frame rectangle, adjusted for the style without a caption.
    rcFrame:=Rect(0,0,0,0);
    dwStyle:=WS_OVERLAPPEDWINDOW and Not WS_CAPTION;
    AdjustWindowRectEx(rcFrame, dwStyle, False, 0);

    // Determine if the hit test is for resizing. Default middle (1,1).
    uRow:=1;
    uCol:=1;
    fOnResizeBorder:=False;

    // Determine if the point is at the top or bottom of the window.
    if (ptMouse.y >= rcWindow.top) and (ptMouse.y < rcWindow.top + FMarInset.cyTopHeight) then
    begin
        fOnResizeBorder := (ptMouse.y < (rcWindow.top - rcFrame.top));
        uRow := 0;
    end;

    if (ptMouse.y < rcWindow.bottom) and (ptMouse.y >= rcWindow.bottom - FMarInset.cyBottomHeight) then
    begin
        uRow := 2;
    end;

    // Determine if the point is at the left or right of the window.
    if (ptMouse.x >= rcWindow.left) and (ptMouse.x < rcWindow.left + FMarInset.cxLeftWidth) then
    begin
        uCol := 0; // left side
    end;

    if (ptMouse.x < rcWindow.right) and (ptMouse.x >= rcWindow.right - FMarInset.cxRightWidth) then
    begin
        uCol := 2; // right side
    end;


    Message.Result:=hitTests[uRow][uCol];

    if (uRow=0) and (uCol=1) and Not fOnResizeBorder then
    begin
      Message.Result:=HTCAPTION;
    end;

  end;
end;

procedure TfrmBaseDWMLearn.WMPAINT(var Message: TMessage);
var
  hThm:HTHEME;
  hMemDC:HDC;
  bmpinfo:BITMAPINFO;
  hBmp:HBITMAP;
  hBmpOld:HGDIOBJ;
  tmpdttopts:DTTOPTS;
  rc:TRect;
  hr:HRESULT;
  p:Pointer;
  hFontOld:HGDIOBJ;
begin
  Inherited;
  //获取主题句柄
  hThm:=OpenThemeData(GetDesktopWindow(),PWideChar('TextStyle'));

  //创建DIB
  hMemDC := CreateCompatibleDC(Canvas.Handle);
  FillChar(bmpinfo,SizeOf(bmpinfo),0);
  bmpinfo.bmiHeader.biSize := sizeof(bmpinfo.bmiHeader);
  bmpinfo.bmiHeader.biBitCount := 32;
  bmpinfo.bmiHeader.biCompression := BI_RGB;
  bmpinfo.bmiHeader.biPlanes := 1;
  bmpinfo.bmiHeader.biWidth := Width;
  bmpinfo.bmiHeader.biHeight := -FMarInset.cyTopHeight;
  hBmp := CreateDIBSection(hMemDC, &bmpinfo, DIB_RGB_COLORS, p, 0, 0);

  hBmpOld := SelectObject(hMemDC, hBmp);
  hFontOld:=SelectObject(hMemDC,Font.Handle);

  //绘制选项
  FillChar(tmpdttopts,SizeOf(tmpdttopts),0);
  tmpdttopts.dwSize := sizeof(DTTOPTS);
  tmpdttopts.dwFlags := DTT_GLOWSIZE or DTT_COMPOSITED or DTT_TEXTCOLOR;

  //发光的范围大小
  tmpdttopts.iGlowSize := 6;
  tmpdttopts.crText:=ColorToRGB(Font.Color);

  //绘制文本
  rc := Rect(10,0,Width,FMarInset.cyTopHeight);
  hr := DrawThemeTextEx(hThm,hMemDC, TEXT_BODYTITLE, 0, Caption, -1, DT_LEFT or DT_VCENTER or DT_SINGLELINE , rc, tmpdttopts);

  //绘制缓存
  if (FAILED(hr)) then Exit;

  BitBlt(Canvas.Handle, 10, 0, Width,FMarInset.cyTopHeight, hMemDC, 0, 0, SRCCOPY);

  SelectObject(hMemDC, hFontOld);
  SelectObject(hMemDC, hBmpOld);
  DeleteObject(hBmp);
  DeleteDC(hMemDC);
  CloseThemeData(hThm);

end;


end.


DWM的学习基本就到这里了,接下来会写一篇关于实践的文章,我会讲解一下在开启模糊合成下窗体上进行绘制以及控件相关等方面的内容。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值