您的位置:首页 > VC 创建DirectX环境
站内搜索

创建DirectX环境

一、首先创建一个"Hello Word!" Win32 Application程序


二、删除WM_PAINT消息中的以下内容
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);

三、修改函数

ATOM MyRegisterClass(HINSTANCE hInstance)
{

WNDCLASS wc;

//wc.cbSize = sizeof(WNDCLASS);

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = szWindowClass;

return RegisterClass (&wc );
}



四、修改函数

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindowEx(WS_EX_TOPMOST,szWindowClass, szTitle, WS_POPUP,
0, 0,GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}



五、定义DirectDraw

a) 定义全局变量

LPDIRECTDRAW lpDD;
LPDIRECTDRAWSURFACE lpDDSPrimary;
LPDIRECTDRAWSURFACE lpDDSBack;



b)修改函数

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindowEx(WS_EX_TOPMOST,szWindowClass, szTitle, WS_POPUP,
0, 0,GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;
HDC hdc;
char buf[256];
ddrval=DirectDrawCreate(NULL,&lpDD,NULL);
if (ddrval==DD_OK)
{
ddrval=lpDD->SetCooperativeLevel (hWnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN);
if (ddrval==DD_OK)
{
ddrval=lpDD->SetDisplayMode (640,480,8);
if (ddrval==DD_OK)
{
ddsd.dwSize =sizeof(ddsd);
ddsd.dwFlags =DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps =DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount =1;
ddrval=lpDD->CreateSurface (&ddsd,&lpDDSPrimary,NULL);
if (ddrval==DD_OK)
{
ddscaps.dwCaps =DDSCAPS_BACKBUFFER;
ddrval=lpDDSPrimary->GetAttachedSurface (&ddscaps,&lpDDSBack);
if(ddrval==DD_OK)
{
return TRUE;
}
}
}
}
}
DestroyDirectDrawObjects();
DestroyWindow(hWnd);
return FALSE;
}



c)添加头文件
#include

d)在project|setting|link添加ddraw.lib

e)添加函数声明及函数体

static void DestroyDirectDrawObjects(void);
static void DestroyDirectDrawObjects(void)
{
if(lpDD!=NULL)
{
lpDD->RestoreDisplayMode ();
if(lpDDSPrimary!=NULL)

{
lpDDSPrimary->Release ();
lpDDSPrimary=NULL;
}
lpDD->Release ();
lpDD=NULL;
}
}

六、添加

case WM_KEYDOWN:
switch(wParam)
{

case VK_ESCAPE:
case VK_F12:
DestroyDirectDrawObjects();
PostMessage(hWnd,WM_CLOSE,0,0);
break;
}
break;

  • 上一篇:MFC:thunk技术实现窗口类的封装
  • 下一篇:在一个程序中打开其它应用程序