关于窗体的鼠标穿透……
2008-07-01 09:52
204 查看
在做一个图像处理程序,做了一个蒙板,想来想去,用个窗体透明,然后让鼠标穿透最简单…………
于是乎…………上网查找…………得到资料若干:
大多数提到了一个函数:SetWindowLong。
于是乎“F1”之,得到如下:(英文太烂…………没看懂…………)
Note This function has been superseded by the SetWindowLongPtr function. To write code that is compatible with both 32-bit and 64-bit versions of Microsoft Windows, use the SetWindowLongPtr function.
Syntax
Parameters
hWnd
[in]
Handle to the window and, indirectly, the class to which the window belongs.
Windows 95/98/Me: The SetWindowLong function may fail if the window specified by the hWnd parameter does not belong to the same process as the calling thread.
nIndex
[in] Specifies the zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer. To set any other value, specify one of the following values.
GWL_EXSTYLE
Sets a new extended window style. For more information, see CreateWindowEx.
GWL_STYLE
Sets a new window style.
GWL_WNDPROC
Sets a new address for the window procedure.
Windows NT/2000/XP: You cannot change this attribute if the window does not belong to the same process as the calling thread.
GWL_HINSTANCE
Sets a new application instance handle.
GWL_ID
Sets a new identifier of the window.
GWL_USERDATA
Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.
The following values are also available when the hWnd parameter identifies a dialog box.
DWL_DLGPROC
Sets the new address of the dialog box procedure.
DWL_MSGRESULT
Sets the return value of a message processed in the dialog box procedure.
DWL_USER
Sets new extra information that is private to the application, such as handles or pointers.
dwNewLong
[in] Specifies the replacement value.
Return Value
If the function succeeds, the return value is the previous value of the specified 32-bit integer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
If the previous value of the specified 32-bit integer is zero, and the function succeeds, the return value is zero, but the function does not clear the last error information. This makes it difficult to determine success or failure. To deal with this, you should clear the last error information by calling SetLastError(0) before calling SetWindowLong. Then, function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.
英文巨烂
…………没看懂
…………不过至少知道该function有3个参数:hWnd:应该是窗体句柄,nIndex :这个好像就是设置窗体的(一堆值
不到干吗的)。最后的一个…………dwNewLong 翻了下是“指定代替者”?不过看看网上的好像没人用这个…………于是乎,曰:“放”………………
但查找中找到了网友ahjoe(强哥)的东西:
//做成控件了,支持透明度,支持鼠标穿透。
unit TransForm; {DragonPC 2001.2.21 }
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms ;
type
TTranForm = class(TComponent)
private
FAlphaValue: integer ;
FTransMouse: Boolean;
FParentFormHandle: HWND ;
procedure SetFAlphaValue(Alpha:integer);
procedure SetTransMouse(value: Boolean);
protected
procedure UpdateDisplay;
public
constructor Create(AOwner: TComponent); override;
published
property AlphaValue: integer read FAlphaValue write SetFAlphaValue ;
property TransMouse: Boolean read FTransMouse write SetTransMouse;
end;
procedure Register;
function SetLayeredWindowAttributes(Handle: HWND; COLORKEY: COLORREF;
Alpha: BYTE; Flags: DWORD): Boolean; stdcall; external 'USER32.DLL';
implementation
procedure Register;
begin
RegisterComponents('Standard', [TTranForm]);
end;
procedure TTranForm.SetFAlphaValue(Alpha: integer);
begin
if (Alpha >= 0) and (Alpha < 256) then begin
FAlphaValue := Alpha ;
UpdateDisplay() ;
end;
end;
procedure TTranForm.UpdateDisplay;
begin
if (csDesigning in ComponentState) then Exit ;
SetLayeredWindowAttributes(FParentFormHandle, 0, FAlphaValue, 2);
end;
constructor TTranForm.Create(AOwner: TComponent);
begin
inherited;
if (csDesigning in ComponentState) then
Exit;
FAlphaValue := 255 ;
FParentFormHandle := TForm(AOwner).Handle ;
SetWindowLong(FParentFormHandle, GWL_EXSTYLE,
GetWindowLong(FParentFormHandle, GWL_EXSTYLE) or $80000);
end;
procedure TTranForm.SetTransMouse(value: Boolean);
begin
if FTransMouse <> value then
begin
FTransMouse := value;
if value then
SetWindowLong(FParentFormHandle, GWL_EXSTYLE,
GetWindowLong(FParentFormHandle, GWL_EXSTYLE) or WS_EX_TRANSPARENT)
else
SetWindowLong(FParentFormHandle, GWL_EXSTYLE,
GetWindowLong(FParentFormHandle, GWL_EXSTYLE) and not WS_EX_TRANSPARENT);
end;
end;
end.
本着“拿来主意”的精神,直接用了(免责!!我可是有说是谁的东西!)…………
tranForm := TTranForm.Create(Self);
TranForm.AlphaValue := 150;
TranForm.TransMouse := False;
这样就好了…………
要是要穿透只要TranForm.TransMouse := true;
不过问题是,这个好像不能将窗体设置到空中里…………会失效…………
于是乎…………上网查找…………得到资料若干:
大多数提到了一个函数:SetWindowLong。
于是乎“F1”之,得到如下:(英文太烂…………没看懂…………)
SetWindowLong Function
The SetWindowLong function changes an attribute of the specified window. The function also sets the 32-bit (long) value at the specified offset into the extra window memory.Note This function has been superseded by the SetWindowLongPtr function. To write code that is compatible with both 32-bit and 64-bit versions of Microsoft Windows, use the SetWindowLongPtr function.
Syntax
LONG SetWindowLong( HWND hWnd, int nIndex, LONG dwNewLong );
Parameters
hWnd
[in]
Handle to the window and, indirectly, the class to which the window belongs.
Windows 95/98/Me: The SetWindowLong function may fail if the window specified by the hWnd parameter does not belong to the same process as the calling thread.
nIndex
[in] Specifies the zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer. To set any other value, specify one of the following values.
GWL_EXSTYLE
Sets a new extended window style. For more information, see CreateWindowEx.
GWL_STYLE
Sets a new window style.
GWL_WNDPROC
Sets a new address for the window procedure.
Windows NT/2000/XP: You cannot change this attribute if the window does not belong to the same process as the calling thread.
GWL_HINSTANCE
Sets a new application instance handle.
GWL_ID
Sets a new identifier of the window.
GWL_USERDATA
Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.
The following values are also available when the hWnd parameter identifies a dialog box.
DWL_DLGPROC
Sets the new address of the dialog box procedure.
DWL_MSGRESULT
Sets the return value of a message processed in the dialog box procedure.
DWL_USER
Sets new extra information that is private to the application, such as handles or pointers.
dwNewLong
[in] Specifies the replacement value.
Return Value
If the function succeeds, the return value is the previous value of the specified 32-bit integer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
If the previous value of the specified 32-bit integer is zero, and the function succeeds, the return value is zero, but the function does not clear the last error information. This makes it difficult to determine success or failure. To deal with this, you should clear the last error information by calling SetLastError(0) before calling SetWindowLong. Then, function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.
英文巨烂
…………没看懂
…………不过至少知道该function有3个参数:hWnd:应该是窗体句柄,nIndex :这个好像就是设置窗体的(一堆值
不到干吗的)。最后的一个…………dwNewLong 翻了下是“指定代替者”?不过看看网上的好像没人用这个…………于是乎,曰:“放”………………
但查找中找到了网友ahjoe(强哥)的东西:
//做成控件了,支持透明度,支持鼠标穿透。
unit TransForm; {DragonPC 2001.2.21 }
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms ;
type
TTranForm = class(TComponent)
private
FAlphaValue: integer ;
FTransMouse: Boolean;
FParentFormHandle: HWND ;
procedure SetFAlphaValue(Alpha:integer);
procedure SetTransMouse(value: Boolean);
protected
procedure UpdateDisplay;
public
constructor Create(AOwner: TComponent); override;
published
property AlphaValue: integer read FAlphaValue write SetFAlphaValue ;
property TransMouse: Boolean read FTransMouse write SetTransMouse;
end;
procedure Register;
function SetLayeredWindowAttributes(Handle: HWND; COLORKEY: COLORREF;
Alpha: BYTE; Flags: DWORD): Boolean; stdcall; external 'USER32.DLL';
implementation
procedure Register;
begin
RegisterComponents('Standard', [TTranForm]);
end;
procedure TTranForm.SetFAlphaValue(Alpha: integer);
begin
if (Alpha >= 0) and (Alpha < 256) then begin
FAlphaValue := Alpha ;
UpdateDisplay() ;
end;
end;
procedure TTranForm.UpdateDisplay;
begin
if (csDesigning in ComponentState) then Exit ;
SetLayeredWindowAttributes(FParentFormHandle, 0, FAlphaValue, 2);
end;
constructor TTranForm.Create(AOwner: TComponent);
begin
inherited;
if (csDesigning in ComponentState) then
Exit;
FAlphaValue := 255 ;
FParentFormHandle := TForm(AOwner).Handle ;
SetWindowLong(FParentFormHandle, GWL_EXSTYLE,
GetWindowLong(FParentFormHandle, GWL_EXSTYLE) or $80000);
end;
procedure TTranForm.SetTransMouse(value: Boolean);
begin
if FTransMouse <> value then
begin
FTransMouse := value;
if value then
SetWindowLong(FParentFormHandle, GWL_EXSTYLE,
GetWindowLong(FParentFormHandle, GWL_EXSTYLE) or WS_EX_TRANSPARENT)
else
SetWindowLong(FParentFormHandle, GWL_EXSTYLE,
GetWindowLong(FParentFormHandle, GWL_EXSTYLE) and not WS_EX_TRANSPARENT);
end;
end;
end.
本着“拿来主意”的精神,直接用了(免责!!我可是有说是谁的东西!)…………
tranForm := TTranForm.Create(Self);
TranForm.AlphaValue := 150;
TranForm.TransMouse := False;
这样就好了…………
要是要穿透只要TranForm.TransMouse := true;
不过问题是,这个好像不能将窗体设置到空中里…………会失效…………
相关文章推荐
- 制作鼠标穿透窗体
- C#制作鼠标可以穿透的窗体
- 制作鼠标穿透窗体
- C# 鼠标穿透窗体功能
- [随笔]关于如何实现鼠标穿透窗口和窗口半透明
- C# 鼠标穿透窗体与恢复
- C# 鼠标穿透窗体功能的实现方法
- 怎么实现“鼠标穿透”,即鼠标对窗体失去作用,对着它点右键要出现WINDOWS的桌面右菜单
- 关于动态监测鼠标移入移出窗体问题的分析
- 用C#写依附于桌面的透明带鼠标穿透窗体的总结。
- 怎么实现“鼠标穿透”,即鼠标对窗体失去作用,对着它点右键要出现WINDOWS的桌面右菜单
- C#制作鼠标可以穿透的窗体
- Qt 不规则窗体 – 鼠标点击穿透(Linux也可以,有对x11的配置的方法)
- Winform实现鼠标可穿透的窗体镂空效果
- C#窗体设计------鼠标穿透窗体
- 实现“鼠标穿透窗体”功能
- 实现鼠标穿透窗体
- C#-窗体鼠标穿透
- C# Winform 窗体美化(五、鼠标穿透)
- c# 鼠标穿透窗体 张宇轩