启用VCL样式时如何制作透明表单?
我使用以下代码使表单透明,但是当应用程序启用了 VCL 样式时,表单将使用 VCL 样式的背景颜色进行绘制,而不是透明。
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure CreateParams(var Params:TCreateParams); override;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := WS_EX_TRANSPARENT or WS_EX_TOPMOST;
//Params.ExStyle := Params.ExStyle or WS_EX_LAYERED;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Brush.Style:=bsClear;
BorderStyle:=bsNone;
//SetLayeredWindowAttributes(Handle, 0, 230, $00000002);
end;
仅供参考,如果 vcl 样式设置为 Windows
,则代码可以正常工作。
是否存在另一种使表单透明来解决此问题的方法?
I'm using the following code to make a form transparent, but when the application has a VCL style enabled the form is paint with the background color of the VCL style instead of be transparent.
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure CreateParams(var Params:TCreateParams); override;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := WS_EX_TRANSPARENT or WS_EX_TOPMOST;
//Params.ExStyle := Params.ExStyle or WS_EX_LAYERED;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Brush.Style:=bsClear;
BorderStyle:=bsNone;
//SetLayeredWindowAttributes(Handle, 0, 230, $00000002);
end;
FYI The code works fine if the the vcl style is set to Windows
.
Exist another way to make a form transparent to workaround this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说这似乎是一个错误。 VCL 样式使用样式钩子来拦截绘制方法以及与这些操作相关的 Windows 消息,因此在这种情况下,您必须将注意力集中在
PaintBackground
方法上。 a href="http://docwiki.embarcadero.com/VCL/en/Forms.TFormStyleHook">TFormStyleHook
类位于Vcl.Forms
中,从这里你创建一个新的样式钩子类(源自TFormStyleHook),重写PaintBackground
方法,修复代码,最后在使用它之前调用RegisterStyleHook方法来注册New < em>样式钩子。检查这篇文章修复 TPageControl 和 TTabControl 组件中的 VCL 样式错误
查看示例。更新
检查此示例
新样式 Hook 类
It seems like a bug to me. The VCL Styles use Style hooks to intercept the paint methods and the Windows messages related to these operations, So in this case you must focus your atention in the
PaintBackground
method of theTFormStyleHook
class located in theVcl.Forms
, from here you create a new style hook class (which descends from TFormStyleHook), override thePaintBackground
method, fix the code and finally before to use it call the RegisterStyleHook method to register the New style hook. check this articleFixing a VCL Style bug in the TPageControl and TTabControl components
to see an example.UPDATE
Check this sample
The New Style Hook Class
另外,您是否尝试过使用
TransparentColor
和TranparentColorValue
属性而不是在CreateParams()
中操作窗口样式?On a separate note, have you tried using the
TransparentColor
andTranparentColorValue
properties instead of manipulating the window styles inCreateParams()
?我使用 OverridePaintNC := False 来防止在 NC 区域上绘制样式。还有 OverrideEraseBkgnd 。也许这有帮助。
I use OverridePaintNC := False to prevent draw Styles on NC area. And there is OverrideEraseBkgnd too. Maybe this help.