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