程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> VC++ >> 創建DirectX環境

創建DirectX環境

編輯:VC++

一、首先創建一個"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;

  1. 上一頁:
  2. 下一頁:
欄目導航
Copyright © 程式師世界 All Rights Reserved