一、首先创建一个"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)添加头文件 #includestatic 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; |
正在阅读:创建DirectX环境创建DirectX环境
2005-06-06 14:57
出处:
责任编辑:moningfeng