自己才学编程没多久,还没有深入,我比较不熟悉Delphi和DirectX,麻烦懂的朋友帮忙把下面这段代码转成VB或者VC的都行。
也好学习下不同环境下的截图功,谢谢大家了
interface{$DEFINE D3D9}uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons,
{$IFDEF D3D9}
// D3DX9, // use D3D to save surface
Direct3D9
{$ELSE}
// D3DX8, // use D3D to save surface
Direct3D8
{$ENDIF};
...
procedure TForm1.BitBtn1Click(Sender: TObject);
// Capture screen through D3D.
var
BitsPerPixel: Byte;
{$IFDEF D3D9}
pD3D: IDirect3D9;
pSurface: IDirect3DSurface9;
g_pD3DDevice: IDirect3DDevice9;
{$ELSE}
pD3D: IDirect3D8;
pSurface: IDirect3DSurface8;
g_pD3DDevice: IDirect3DDevice8;
{$ENDIF}
D3DPP: TD3DPresentParameters;
ARect: TRect;
LockedRect: TD3DLockedRect;
BMP: TBitmap;
i, p: Integer;
begin
BitsPerPixel := GetDeviceCaps(Canvas.Handle, BITSPIXEL);
FillChar(d3dpp, SizeOf(d3dpp), 0);
D3DPP.Windowed := True;
D3DPP.Flags := D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
D3DPP.SwapEffect := D3DSWAPEFFECT_DISCARD;
D3DPP.BackBufferWidth := Screen.Width;
D3DPP.BackBufferHeight := Screen.Height;
D3DPP.BackBufferFormat := D3DFMT_X8R8G8B8;
{$IFDEF D3D9}
pD3D := Direct3DCreate9(D3D_SDK_VERSION);
pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, @D3DPP, g_pD3DDevice);
g_pD3DDevice.CreateOffscreenPlainSurface(Screen.Width, Screen.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, pSurface, nil);
g_pD3DDevice.GetFrontBufferData(0, pSurface);
{$ELSE}
pD3D := Direct3DCreate8(D3D_SDK_VERSION);
pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, GetDesktopWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DPP, g_pD3DDevice);
g_pD3DDevice.CreateImageSurface(Screen.Width, Screen.Height, D3DFMT_A8R8G8B8, pSurface);
g_pD3DDevice.GetFrontBuffer(pSurface);
{$ENDIF}
// use D3D to save surface. Notes: D3DX%ab.dll is required!
// D3DXSaveSurfaceToFile('Desktop.bmp', D3DXIFF_BMP, pSurface, nil, nil);
// use Bitmap to save surface
ARect := Screen.DesktopRect;
pSurface.LockRect(LockedRect, @ARect, D3DLOCK_NO_DIRTY_UPDATE or D3DLOCK_NOSYSLOCK or D3DLOCK_READONLY);
BMP := TBitmap.Create;
BMP.Width := Screen.Width;
BMP.Height := Screen.Height;
case BitsPerPixel of
8: BMP.PixelFormat := pf8bit;
16: BMP.PixelFormat := pf16bit;
24: BMP.PixelFormat := pf24bit;
32: BMP.PixelFormat := pf32bit;
end;
p := Cardinal(LockedRect.pBits);
for i := 0 to Screen.Height - 1 do
begin
CopyMemory(BMP.ScanLine[i], Ptr(p), Screen.Width * BitsPerPixel div 8);
p := p + LockedRect.Pitch;
end;
BMP.SaveToFile('Desktop.bmp');
BMP.Free;
pSurface.UnlockRect;
end;
也好学习下不同环境下的截图功,谢谢大家了
interface{$DEFINE D3D9}uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons,
{$IFDEF D3D9}
// D3DX9, // use D3D to save surface
Direct3D9
{$ELSE}
// D3DX8, // use D3D to save surface
Direct3D8
{$ENDIF};
...
procedure TForm1.BitBtn1Click(Sender: TObject);
// Capture screen through D3D.
var
BitsPerPixel: Byte;
{$IFDEF D3D9}
pD3D: IDirect3D9;
pSurface: IDirect3DSurface9;
g_pD3DDevice: IDirect3DDevice9;
{$ELSE}
pD3D: IDirect3D8;
pSurface: IDirect3DSurface8;
g_pD3DDevice: IDirect3DDevice8;
{$ENDIF}
D3DPP: TD3DPresentParameters;
ARect: TRect;
LockedRect: TD3DLockedRect;
BMP: TBitmap;
i, p: Integer;
begin
BitsPerPixel := GetDeviceCaps(Canvas.Handle, BITSPIXEL);
FillChar(d3dpp, SizeOf(d3dpp), 0);
D3DPP.Windowed := True;
D3DPP.Flags := D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
D3DPP.SwapEffect := D3DSWAPEFFECT_DISCARD;
D3DPP.BackBufferWidth := Screen.Width;
D3DPP.BackBufferHeight := Screen.Height;
D3DPP.BackBufferFormat := D3DFMT_X8R8G8B8;
{$IFDEF D3D9}
pD3D := Direct3DCreate9(D3D_SDK_VERSION);
pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, @D3DPP, g_pD3DDevice);
g_pD3DDevice.CreateOffscreenPlainSurface(Screen.Width, Screen.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, pSurface, nil);
g_pD3DDevice.GetFrontBufferData(0, pSurface);
{$ELSE}
pD3D := Direct3DCreate8(D3D_SDK_VERSION);
pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, GetDesktopWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DPP, g_pD3DDevice);
g_pD3DDevice.CreateImageSurface(Screen.Width, Screen.Height, D3DFMT_A8R8G8B8, pSurface);
g_pD3DDevice.GetFrontBuffer(pSurface);
{$ENDIF}
// use D3D to save surface. Notes: D3DX%ab.dll is required!
// D3DXSaveSurfaceToFile('Desktop.bmp', D3DXIFF_BMP, pSurface, nil, nil);
// use Bitmap to save surface
ARect := Screen.DesktopRect;
pSurface.LockRect(LockedRect, @ARect, D3DLOCK_NO_DIRTY_UPDATE or D3DLOCK_NOSYSLOCK or D3DLOCK_READONLY);
BMP := TBitmap.Create;
BMP.Width := Screen.Width;
BMP.Height := Screen.Height;
case BitsPerPixel of
8: BMP.PixelFormat := pf8bit;
16: BMP.PixelFormat := pf16bit;
24: BMP.PixelFormat := pf24bit;
32: BMP.PixelFormat := pf32bit;
end;
p := Cardinal(LockedRect.pBits);
for i := 0 to Screen.Height - 1 do
begin
CopyMemory(BMP.ScanLine[i], Ptr(p), Screen.Width * BitsPerPixel div 8);
p := p + LockedRect.Pitch;
end;
BMP.SaveToFile('Desktop.bmp');
BMP.Free;
pSurface.UnlockRect;
end;
就是你说的 BMP: TBitmap;我不会把握难到了,vc,vb里面都没这个东西。其他的勉强还行