您的位置:首页 > 其它

关于窗体的鼠标穿透……

2008-07-01 09:52 204 查看
在做一个图像处理程序,做了一个蒙板,想来想去,用个窗体透明,然后让鼠标穿透最简单…………

于是乎…………上网查找…………得到资料若干:

大多数提到了一个函数: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;

不过问题是,这个好像不能将窗体设置到空中里…………会失效…………
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: