请听我慢慢道来...
窗体使用了Alpha透明. 使用的控件代码如下unit TransForm; {DragonPC 2001.2.21 }interfaceuses
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';implementationprocedure 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.
窗体使用了Alpha透明. 使用的控件代码如下unit TransForm; {DragonPC 2001.2.21 }interfaceuses
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';implementationprocedure 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.
dwin: HWND;
adc: HDC;
begin
dwin := GetDesktopWindow();
adc := GetDC(dwin);
try
capImage.Width := Screen.Width;
capImage.Height := Screen.Height;
// BitBlt(ScreenBmp.Canvas.Handle, 0, 0, SCNBMPWIDTH, SCNBMPHEIGHT,
// adc, 0, 0, SRCCOPY);
if not BitBlt(capImage.Canvas.Handle, 0, 0, capImage.Width, capImage.Height,
adc, 0, 0, SRCCOPY) then
begin
capImage.Height := 800 * capImage.Height div capImage.Width;
capImage.Width := 800;
capImage.Canvas.FillRect(capImage.Canvas.ClipRect);
capImage.Canvas.TextOut(80, 40, faces[41]);
end;
finally
ReleaseDC(dwin, adc);
end;
以上抓屏代码,抓不到使用了上面的透明控件的窗体。抓出来的图,ALPHA透明的窗体消失了。
但是按 PrintScreen, 再到画笔里粘贴,却可以抓到上述窗体。
我也想抓到PrintScreen的效果,我该怎么写代码?
如果用BitBlt函数,最后一个参数用一个MSDN没有提到过的值好像可以。我现在查不到资料了,硬盘2周前刚修了。
PrintWindow应该是最简单的。
var
dwin: HWND;
adc: HDC;
capImage : Tbitmap;
begin
capimage := Tbitmap.Create;
dwin := GetDesktopWindow();
adc := CreateDC('DISPLAY',nil,nil,nil);
//adc := GetWindowDC (0);
try
capImage.Width := Screen.Width;
capImage.Height := Screen.Height;
// BitBlt(ScreenBmp.Canvas.Handle, 0, 0, SCNBMPWIDTH, SCNBMPHEIGHT,
// adc, 0, 0, SRCCOPY);
if not bitblt(capImage.Canvas.Handle, 0, 0, capImage.Width, capImage.Height,
adc, 0, 0,SRCCOPY or $40000000 ) then
begin
capImage.Height := 800 * capImage.Height div capImage.Width;
capImage.Width := 800;
capImage.Canvas.FillRect(capImage.Canvas.ClipRect);
capImage.Canvas.TextOut(10, 10,'33');
end;
Image1.Picture.Bitmap.Assign(capImage);
//Panel1.Align := alLeft;
Panel1.Caption := 'ttt';
finally
DeleteDC(adc);
//ReleaseDC(0, adc);
end;end;
主要是就是: SRCCOPY or $40000000 DELPHI沒有封裝這個參數,暈倒。