19,464
社区成员
发帖
与我相关
我的任务
分享
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TESTDX, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTDX);
/**
* 创建一个 DirectDraw 对象
*/
LPDIRECTDRAW _lpDD = NULL;
HRESULT ddrval = DirectDrawCreate(NULL, &_lpDD, NULL);
if (FAILED(ddrval))
{
MessageBox(NULL, "Failed to DirectDrawCreate", NULL, MB_OK);
return FALSE;
}
LPDIRECTDRAW lpDDraw = NULL;
if(FAILED(_lpDD->QueryInterface(IID_IDirectDraw, (LPVOID *)&lpDDraw)))
{
MessageBox(NULL,TEXT("DirectDraw QueryInterface error!"), TEXT("Wrong!"), MB_OK);
return(0);
}
/**
* 设置执行模式
*/
if (FAILED(lpDDraw->SetCooperativeLevel(g_hwnd, DDSCL_NORMAL )))
{
MessageBox(NULL,TEXT("DirectDraw SetCooperativeLevel error!"), TEXT("Wrong!"), MB_OK);
return(0);
}
/**
* 创建一个表层
*/
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE lpDDSPrimary;
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS ;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_VISIBLE | DDSCAPS_OVERLAY;
ddsd.dwBackBufferCount = 1;
ddrval = lpDDraw->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
if (FAILED(ddrval))
{
MessageBox(NULL, "Failed to CreateSurface", NULL, MB_OK);
return FALSE;
}
LPDIRECTDRAWCLIPPER clipper;
if(lpDDraw->CreateClipper(0, &clipper, NULL) != DD_OK)
{
MessageBox(NULL, "Failed to GreateClipper", NULL, MB_OK);
return FALSE;
}
if( lpDDSPrimary->SetClipper(clipper) != DD_OK )
{
MessageBox(NULL, "Failed to SetClipper", NULL, MB_OK);
return FALSE;
}
clipper->SetHWnd(0, g_hwnd);
HDC hdc;
if(lpDDSPrimary->GetDC(&hdc) != DD_OK)
{
MessageBox(NULL, "Failed to GetDC", NULL, MB_OK);
return FALSE;
}
RECT rcWin;
GetWindowRect(g_hwnd,&rcWin);
SetViewportOrgEx(hdc,rcWin.left,rcWin.top,NULL);
SetViewportExtEx(hdc, rcWin.right - rcWin.left, rcWin.bottom - rcWin.top, NULL);
SetBkColor(hdc, RGB(0,0,255));
SetTextColor( hdc,RGB(255,255,0 ) );
TextOut( hdc, 0, 0, "test", lstrlen("test"));
lpDDSPrimary->ReleaseDC(hdc);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// testDX.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <DDRAW.H>
#pragma comment(lib, "ddraw.lib")
#pragma comment(lib, "dxguid.lib")
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
HWND g_hwnd = NULL;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
static DDPIXELFORMAT g_ddpfOverlayFormats[] = {
{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0xFF00, 0x0FF0, 0x00FF, 0},
{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0xF800, 0x07e0, 0x001F, 0},
{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x7C00, 0x03e0, 0x001F, 0},
{sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y','U','V',0),0,0,0,0,0},
{sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y','U','Y','V'),0,0,0,0,0},
{sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U','Y','V','Y'),0,0,0,0,0},
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TESTDX, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTDX);
/**
* 创建一个 DirectDraw 对象
*/
LPDIRECTDRAW _lpDD = NULL;
HRESULT ddrval = DirectDrawCreate(NULL, &_lpDD, NULL);
if (FAILED(ddrval))
{
MessageBox(NULL, "Failed to DirectDrawCreate", NULL, MB_OK);
return FALSE;
}
LPDIRECTDRAW lpDDraw = NULL;
if(FAILED(_lpDD->QueryInterface(IID_IDirectDraw, (LPVOID *)&lpDDraw)))
{
MessageBox(NULL,TEXT("DirectDraw QueryInterface error!"), TEXT("Wrong!"), MB_OK);
return(0);
}
/**
* 设置执行模式
*/
if (FAILED(lpDDraw->SetCooperativeLevel(g_hwnd, DDSCL_NORMAL )))
{
MessageBox(NULL,TEXT("DirectDraw SetCooperativeLevel error!"), TEXT("Wrong!"), MB_OK);
return(0);
}
// 判断是否支持 Overlay
DDCAPS ddcaps;
// Get driver capabilities to determine overlay support.
ZeroMemory( &ddcaps, sizeof(ddcaps) );
ddcaps.dwSize = sizeof(ddcaps);
_lpDD->GetCaps( &ddcaps, NULL );
// Does the driver support overlays in the current mode?
// The DirectDraw emulation layer does not support overlays
// so overlay related APIs will fail without hardware support.
if( ddcaps.dwCaps & DDCAPS_OVERLAY )
{
// Make sure it supports stretching (scaling)
if ( ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH )
{}
else
{
MessageBox(NULL, "Don't suppor OVERLAY", NULL, MB_OK);
return FALSE;
}
}
else
{
MessageBox(NULL, "Don't suppor OVERLAYe", NULL, MB_OK);
return FALSE;
}
/**
* 创建一个表层
*/
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE lpDDSPrimary;
/*
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS ;
ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
ddsd.dwBackBufferCount = 1;
*/
memset (&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
ddsd.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
ddsd.dwHeight = 100;
ddsd.dwWidth = 100;
ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
ddsd.ddpfPixelFormat.dwFourCC = MAKEFOURCC('Y','V','1','2');
ddsd.ddpfPixelFormat.dwYUVBitCount = 12;
ddrval = lpDDraw->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
if (FAILED(ddrval))
{
MessageBox(NULL, "Failed to CreateSurface", NULL, MB_OK);
return FALSE;
}
LPDIRECTDRAWCLIPPER clipper;
if(lpDDraw->CreateClipper(0, &clipper, NULL) != DD_OK)
{
MessageBox(NULL, "Failed to GreateClipper", NULL, MB_OK);
return FALSE;
}
if( lpDDSPrimary->SetClipper(clipper) != DD_OK )
{
MessageBox(NULL, "Failed to SetClipper", NULL, MB_OK);
return FALSE;
}
clipper->SetHWnd(0, g_hwnd);
HDC hdc;
HRESULT hr = lpDDSPrimary->GetDC(&hdc);
if( hr != DD_OK)
{
MessageBox(NULL, "Failed to GetDC", NULL, MB_OK);
return FALSE;
}
RECT rcWin;
GetWindowRect(g_hwnd,&rcWin);
SetViewportOrgEx(hdc,rcWin.left,rcWin.top,NULL);
SetViewportExtEx(hdc, rcWin.right - rcWin.left, rcWin.bottom - rcWin.top, NULL);
SetBkColor(hdc, RGB(0,0,255));
SetTextColor( hdc,RGB(255,255,0 ) );
TextOut( hdc, 0, 0, "test", lstrlen("test"));
lpDDSPrimary->ReleaseDC(hdc);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TESTDX);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_TESTDX;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
g_hwnd = hWnd;
return TRUE;
}
BOOL HasOverlaySupport()
{
DDCAPS g_ddcaps;
// Get driver capabilities to determine overlay support.
ZeroMemory( &g_ddcaps, sizeof(g_ddcaps) );
g_ddcaps.dwSize = sizeof(g_ddcaps);
g_pDD->GetCaps( &g_ddcaps, NULL );
// Does the driver support overlays in the current mode?
// The DirectDraw emulation layer does not support overlays
// so overlay related APIs will fail without hardware support.
if( g_ddcaps.dwCaps & DDCAPS_OVERLAY )
{
// Make sure it supports stretching (scaling)
if ( g_ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH )
return TRUE;
else
return FALSE;
}
else
{
return FALSE;
}
}