我尝试使用SetWindowRgn
,但我做不到。
可以这样做(前2个角是圆角的,窗户有阴影)就像这张照片一样?
答案 0 :(得分:18)
以下是如何使用阴影设置窗口区域的代码示例:
(注意:表格BorderStyle
假定为bsNone
,而不是重新调整大小
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure CreateFlatRoundRgn;
protected
procedure CreateParams(var Params: TCreateParams); override;
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure ExcludeRectRgn(var Rgn: HRGN; LeftRect, TopRect, RightRect, BottomRect: Integer);
var
RgnEx: HRGN;
begin
RgnEx := CreateRectRgn(LeftRect, TopRect, RightRect, BottomRect);
CombineRgn(Rgn, Rgn, RgnEx, RGN_OR);
DeleteObject(RgnEx);
end;
procedure TForm1.CreateFlatRoundRgn;
const
CORNER_SIZE = 6;
var
Rgn: HRGN;
begin
with BoundsRect do
begin
Rgn := CreateRoundRectRgn(0, 0, Right - Left + 1, Bottom - Top + 1, CORNER_SIZE, CORNER_SIZE);
// exclude left-bottom corner
ExcludeRectRgn(Rgn, 0, Bottom - Top - CORNER_SIZE div 2, CORNER_SIZE div 2, Bottom - Top + 1);
// exclude right-bottom corner
ExcludeRectRgn(Rgn, Right - Left - CORNER_SIZE div 2, Bottom - Top - CORNER_SIZE div 2, Right - Left , Bottom - Top);
end;
// the operating system owns the region, delete the Rgn only SetWindowRgn fails
if SetWindowRgn(Handle, Rgn, True) = 0 then
DeleteObject(Rgn);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
BorderStyle := bsNone;
CreateFlatRoundRgn;
end;
procedure TForm1.CreateParams(var Params: TCreateParams);
const
CS_DROPSHADOW = $00020000;
begin
inherited CreateParams(Params);
with Params do
begin
Style := WS_POPUP;
WindowClass.Style := WindowClass.Style or CS_DROPSHADOW;
end;
end;
绘制自定义阴影的另一种方法是设置窗口WS_EX_LAYERED
并使用UpdateLayeredWindow
以下是very good example如何完成(源代码使用C ++,但很容易理解)
对于更复杂的形状,您可以在表单上使用PNG
图片并Alpha Blend。
修改强>
调整WS_POPUP
窗口的大小是痛苦的世界......
您有几个选择:
WM_Syscommand
$F008
调整大小(在链接上方)或$F012
到move the Window。WS_EX_STATICEDGE
and WS_SIZEBOX
个样式。 注意您需要在重新调整窗口区域时重新创建窗口区域(例如OnResize
事件)。
答案 1 :(得分:-1)
将dwm用于无边界Windows应用程序。示例代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
private
{ Private declarations }
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure WndProc(var Message: TMessage); override;
public
{ Public declarations }
end;
TMARGINS = record
leftWidth: integer;
rightWidth: integer;
topHeight: integer;
bottomHeight: integer;
end;
LPCVOID = Pointer;
function DwmExtendFrameIntoClientArea(hWnd: HWND; const pMarInset: TMARGINS): HRESULT;
stdcall; external 'dwmapi.dll';
function DwmSetWindowAttribute(hWnd: HWND; dwAttribute: DWORD; pvAttribute: LPCVOID;
cbAttribute: DWORD): HRESULT; stdcall; external 'dwmapi.dll';
function DwmIsCompositionEnabled(out pfEnabled: BOOL): HRESULT; stdcall; external 'dwmapi.dll';
const
CS_DROPSHADOW = $00020000;
HTCLIENT = $1;
var
Form1: TForm1;
m_aeroEnabled: boolean;
implementation
{$R *.DFM}
{ TForm1 }
function CheckAeroEnabled(): boolean;
var
Enabled: longbool;
begin
if (Win32MajorVersion >= 6) then
begin
Enabled := False;
DwmIsCompositionEnabled(Enabled);
result := Enabled;
end
else
result := False;
end;
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited;
m_aeroEnabled := CheckAeroEnabled();
if (not m_aeroEnabled) then
Params.WindowClass.style := Params.WindowClass.style + CS_DROPSHADOW;
end;
procedure TForm1.WndProc(var Message: TMessage);
var
margins: TMARGINS;
v: integer;
begin
case (Message.Msg) of
WM_NCPAINT: if (m_aeroEnabled) then
begin
v := 2;
DwmSetWindowAttribute(Self.Handle, 2, @v, 4);
margins.bottomHeight := 1;
margins.leftWidth := 0;
margins.rightWidth := 0;
margins.topHeight := 0;
DwmExtendFrameIntoClientArea(Self.Handle, margins);
end;
end;
inherited;
//To allow move form without Caption.
if (Message.Msg = WM_NCHITTEST) and (Message.result = HTCLIENT) then
Message.result := HTCAPTION;
end;
end.