防止透明表单点击 [英] Prevent Click-thru on transparent form
问题描述
我想在窗体中绘制一个函数,而窗体背景是可见的。创建一个TFOM,将TransparentColor设置为True,将TransParentColorValue设置为clWhite。只需放一个TImage,就可以在位图上绘制一个功能,将其分配给图像,效果很好。有一个问题我不能点击表单了。任何单击表单的标题和客户端都会导致基础应用程序中的点击。这是因为将TransparentColor设置为True。如何防止这种点击通?
I want to plot a function in a form without the forms background being visible. Created a TForm with TransparentColor set to True and TransParentColorValue set to clWhite. Just put a TImage on it, draw a function on a bitmap, assign it to the image, works great. There is one problem. I cannot click the form anymore. Any click on the form's caption and clientarea lead to a click in the underlying application. This is caused by setting TransparentColor to True. How can I prevent this "clicking-thru"?
更新
我尝试过Sertac的建议,并取得了一些不错的成果。我准备位图如下:
Update I tried Sertac's suggestions and I got some remarkable results. I prepare the bitmap as follows:
Bitmap.Canvas.Brush.Color := clFuchsia;
Bitmap.Canvas.FillRect (Rect (0, 0, Bitmap.Width, Bitmap.Height));
当颜色设置为clWhite和Forms TransparentColorValue时,表单完全点击启用
When Color is set to clWhite and the Forms TransparentColorValue as well, the form is completely click-thru enabled.
都设置为clBlack,表单可以移动,但边框无法调整大小,系统按钮不起作用
Both set to clBlack, the form can be moved, but borders can't be resized and system buttons don't work
两个都设置为clFuchsia它的行为是一个普通的窗体。那么它现在可以工作,但也许有人得到解释?
Both set to clFuchsia it behaves as a normal Form. Well, it works now but maybe someone got an explanation?
推荐答案
当使用某些特定的颜色时,API似乎很奇怪。白是一个明显的一个,黑色有点少问题。我尝试过黄色,灰色,按钮脸,紫红色等。他们看起来很好。
It would seem the API acts weird when some specific colors is used. White is an obvious one, black is a little less problematic. I've tried with yellow, gray, button face, fuchsia etc.. and they look fine.
如果有人想要复制问题,而不使用Delphi的透明 。'属性,这里是一个小代码片段:
If anyone wants to duplicate the problem without using Delphi's 'Transparent...' properties, here's a little snippet:
procedure TForm1.Button1Click(Sender: TObject);
var
Color: DWORD;
DC: HDC;
begin
Color := $00FFFFFF;
SetWindowLong(Handle, GWL_EXSTYLE,
GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED );
SetLayeredWindowAttributes(Handle, Color, 255, LWA_COLORKEY);
DC := GetWindowDC(Handle);
SetDCBrushColor(DC, Color);
FillRect(DC, Rect(10, 10, 100, 80), GetStockObject(DC_BRUSH));
ReleaseDC(Handle, DC);
end;
注意:如果有人提供一个确定的答案,我很乐意删除这个答案说明。
Note: I'd gladly remove this answer if anyone provides an answer with a definitive explanation.
这篇关于防止透明表单点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!