寻:DirectX8编程中文资料及下DirectX下抓图方法!(100分)

H

homejun

Unregistered / Unconfirmed
GUEST, unregistred user!
寻:DirectX8编程中文资料及下DirectX下抓图方法!
 
中文资料:
http://www.gpgame.net/
抓图方法:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <D3DX8.h>
#include <D3dx8tex.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HRESULT InitDx8(HWND hwnd);
void ExitDx8();
void ErrorOut();
HRESULT TakeScreenShot(IDirect3DDevice8* device, char* file_name);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
LPDIRECT3D8 g_pD3D = NULL; // 用来创建D3D设备
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // D3D设备

HRESULT InitDx8(HWND hwnd){
D3DDISPLAYMODE d3ddm;
D3DPRESENT_PARAMETERS d3dpp;

if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
return E_FAIL;


if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &amp;d3ddm ) ) )
return E_FAIL;


ZeroMemory(&amp;d3dpp,sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;

if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&amp;d3dpp,
&amp;g_pd3dDevice))){
return E_FAIL;
}

return S_OK;
}

void ExitDx8(){
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();

if( g_pD3D != NULL)
g_pD3D->Release();
}

HRESULT TakeScreenShot(IDirect3DDevice8* device, char* file_name){
HRESULT hr;
// get display dimensions
// this will be the dimensions of the front buffer
D3DDISPLAYMODE mode;
if (FAILED(hr=device->GetDisplayMode(&amp;mode)))
return hr;

IDirect3DSurface8* frontbuf; // this is our pointer to the memory location containing our copy of the
// front buffer
// now we create the image that our screen shot will be copied into
// NOTE: Surface format of the front buffer is D3DFMT_A8R8G8B8 when it is returned
device->CreateImageSurface(mode.Width,mode.Height, D3DFMT_A8R8G8B8, &amp;frontbuf);
// now we copy the front buffer into our surface
hr = device->GetFrontBuffer(frontbuf);
//error checking
if(hr != D3D_OK)
{
// do error handling etc...
frontbuf->Release(); // release the surface so there is no memory leak
return hr;
}
// now write our screen shot to a bitmap file
// the last 2 params are NULL because we want the entire front buffer and no palette
hr=D3DXSaveSurfaceToFile(file_name, D3DXIFF_BMP, frontbuf, NULL, NULL);
// release the surface so there is no memory leak
frontbuf->Release();
return hr;
}

int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&amp;wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
500, /* The programs width */
400, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);


// Initialize Direct3D
if( SUCCEEDED( InitDx8( hwnd ) ) )
{
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
UpdateWindow( hwnd );
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&amp;messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&amp;messages);
/* Send message to WindowProcedure */
DispatchMessage(&amp;messages);
}
}

ExitDx8();
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndButton = 0;

switch (message) /* handle the messages */
{
case WM_CREATE:
/* Now create the button */
hwndButton = CreateWindow (
"button",/* Builtin button class */
"截图",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
200, 150, 100,50,
hwnd,/* Parent is this window. */
(HMENU) 1,/* Control ID: 1 */
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
break;

case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_COMMAND:
/* Check the control ID, notification code and
* control handle to see if this is a button click
* message from our child button. */
if (LOWORD(wParam) == 1 &amp;&amp;
HIWORD(wParam) == BN_CLICKED &amp;&amp;
(HWND) lParam == hwndButton)
{
/* Our button was clicked. TakeScreenShot */
TakeScreenShot(g_pd3dDevice,"1.bmp");
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}

不过效果不好,还是不能截一些播放器的图
 
这个站点在翻译DX8的开发指南。
http://cgd.pages.com.cn/dxtocn/index.html
 
多人接受答案了。
 
顶部