#include <windows.h> #include <windowsx.h> #include <ddraw.h> #include <atlstr.h> LPDIRECTDRAW lpDD = NULL;//direct draw object LPDIRECTDRAWSURFACE lpDDSPrimary = NULL;//direct draw main page char* lpszMsg1 = "Hello, andylin! I love my baby so much!"; char* lpszMsg2 = "press esc exit"; LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); BOOL InitWindow(HINSTANCE hInst, int nCmdShow); BOOL InitDDraw(); void FreeDDraw(); void PrintMsg(HWND hWnd = NULL, LPCTSTR lpszInfo = NULL, LPCTSTR lpszTitle = NULL, int nStyle = MB_OK) { CString strInfo = lpszInfo; CString strTitle = lpszTitle; if (NULL == lpszInfo) { strInfo = "test info"; } if (NULL == lpszTitle) { strTitle = "my direct test"; } MessageBox(hWnd, strInfo, strTitle, nStyle); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg = {0}; if (!InitWindow(hInstance, nCmdShow)) { PrintMsg(NULL, "InitWindow Failed!"); return -1; } HWND hWnd = GetActiveWindow(); //init direct draw if (!InitDDraw()) { PrintMsg(hWnd, "init DirectDraw failed!"); DestroyWindow(hWnd); return -1; } while (::GetMessage(&msg, NULL, 0, 0)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } return (int)msg.wParam; } BOOL InitWindow( HINSTANCE hInstance, int nCmdShow ) { HWND hWnd = NULL; WNDCLASS ws = {0}; char szClassName[] = _T("test dx"); //int window class ws.cbClsExtra = 0; ws.cbWndExtra = 0; ws.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH); ws.hCursor = ::LoadCursor(NULL, IDC_ARROW); ws.hIcon = ::LoadIcon(NULL, IDI_APPLICATION); ws.hInstance = hInstance; ws.lpfnWndProc = WinProc; ws.lpszClassName = szClassName; ws.lpszMenuName = NULL; ws.style = CS_HREDRAW | CS_VREDRAW; //register window class ::RegisterClass(&ws); hWnd = ::CreateWindowEx(0, szClassName, _T("test for diectx"), WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, 0); if (!hWnd) { return FALSE; } //show and update ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_KEYDOWN: { switch (wParam) { case VK_ESCAPE: ::PostMessage(hwnd, WM_CLOSE, 0, 0); break; } } break; case WM_DESTROY: { FreeDDraw(); PostQuitMessage(0); } break; default: break; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } BOOL InitDDraw() { DDSURFACEDESC ddsd = {0}; //page descripe HDC hdc = NULL; if (DD_OK != ::DirectDrawCreate(NULL, &lpDD, NULL)) { return FALSE; } //取得独占和全屏模式 if (DD_OK != lpDD->SetCooperativeLevel(GetActiveWindow(), DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)) { return FALSE; } //set show mode if (DD_OK != lpDD->SetDisplayMode(640, 480, 8)) { return FALSE; } //set main page ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; //create main page object if (DD_OK != lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL)) { return FALSE; } //print text if (DD_OK != lpDDSPrimary->GetDC(&hdc)) { return FALSE; } SetBkColor(hdc, RGB(0, 0, 255)); SetTextColor(hdc, RGB(255, 255, 0)); TextOut(hdc, 0, 20, lpszMsg1, lstrlen(lpszMsg1)); TextOut(hdc, 0, 40, lpszMsg2, lstrlen(lpszMsg2)); lpDDSPrimary->ReleaseDC(hdc); return TRUE; } void FreeDDraw() { if (NULL != lpDDSPrimary) { lpDDSPrimary->Release(); lpDDSPrimary = NULL; } if (NULL != lpDD) { lpDD->Release(); lpDD = NULL; } }