#include <afxwin.h>
#include "windows.h"
#include <iostream>
using namespace std;
#include <ddraw.h>
#pragma comment(lib, "ddraw.lib")
#pragma comment(lib, "Dxguid.lib")
LRESULT CALLBACK WndProc( HWND hWnd, // handle to window
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
HWND hwnd;
void InitWindows(HINSTANCE hInstance)
{
TCHAR lpszClassName[] = TEXT("kuan");
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = ::LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = ::LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = lpszClassName;
RegisterClass(&wc);
// WS_OVERLAPPEDWINDOW 可以改变窗口大小
// WS_OVERLAPPED 不可以改变窗口大小
// WS_POPUP 全屏
hwnd = CreateWindow(lpszClassName,
TEXT("fangyukuan"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,0,0,800,600,
NULL,
NULL,
hInstance,
NULL);
/*
MSG msg;
while(TRUE)
{
if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
*/
}
DDPIXELFORMAT g_ddpfOverlayFormats = {sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('Y','U','Y','2'),0,0,0,0,0};
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
InitWindows( hInstance);
LPDIRECTDRAW pDD = NULL;
HRESULT hr;
// Create a DirectDraw object using the enumerated GUID
if( FAILED( hr = DirectDrawCreate( NULL, &pDD, NULL ) ) )//IID_IDirectDraw7,
{
MessageBox(NULL,"DirectDraw Create Error","Wrong!", MB_OK);
return 0;
}
LPDIRECTDRAW7 lpDDraw7;
if(FAILED(pDD->QueryInterface(IID_IDirectDraw7,(LPVOID*)&lpDDraw7)))
{
MessageBox(NULL,"IID_IDirectDraw7 Create Error","Wrong!", MB_OK);
return 0;
}
if(FAILED(lpDDraw7->SetCooperativeLevel(hwnd,DDSCL_NORMAL /* DDSCL_FULLSCREEN|DDSCL_ALLOWMODEX|DDSCL_EXCLUSIVE|DDSCL_ALLOWREBOOT*/)))
{
MessageBox(NULL,"SetCooperativeLevel Create Error","Wrong!", MB_OK);
return 0;
}
/*
HRESULT EnumDisplayModes(
DWORD dwFlags,
LPDDSURFACEDESC lpDDSurfaceDesc,
LPVOID lpContext,
LPDDENUMMODESCALLBACK lpEnumModesCallback
);
if(FAILED(hr = lpDDraw7->SetDisplayMode(800,600,8,0,0)))
{
if(FAILED(hr = lpDDraw7->SetDisplayMode(800,600,16,0,0)));
{
if(FAILED(hr = lpDDraw7->SetDisplayMode(800,600,32,0,0)));
{
MessageBox(NULL,"SetDisplayMode Create Error","Wrong!", MB_OK);
return 0;
}
}
}
*/
DDSURFACEDESC2 ddsd;
LPDIRECTDRAWSURFACE7 surface;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if(FAILED(hr = lpDDraw7->CreateSurface(&ddsd, &surface,NULL) ) )// DDERR_INCOMPATIBLEPRIMARY
{
if(hr == DDENUMRET_CANCEL)
{
MessageBox(NULL,"CreateSurface Create Error","Wrong!", MB_OK);
return hr;
}
}
DDCAPS ddcap;
ZeroMemory(&ddcap, sizeof(ddcap));
ddcap.dwSize = sizeof(ddcap);
if(FAILED(hr = lpDDraw7->GetCaps(&ddcap,NULL) ) )// DDERR_INCOMPATIBLEPRIMARY
{
MessageBox(NULL,"GetCaps Create Error","Wrong!", MB_OK);
return hr;
}
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
ddsd.dwWidth = 800;
ddsd.dwHeight = 600;
ddsd.ddpfPixelFormat = g_ddpfOverlayFormats;
LPDIRECTDRAWSURFACE7 backsurface;
if(FAILED(hr = lpDDraw7->CreateSurface(&ddsd, &backsurface,NULL) ) )// DDERR_INCOMPATIBLEPRIMARY
{
if(hr == DDENUMRET_CANCEL)
{
MessageBox(NULL,"CreateSurface Create Error","Wrong!", MB_OK);
return hr;
}
}
LPDIRECTDRAWCLIPPER FAR lplpDDClipper;
if(FAILED(hr = lpDDraw7->CreateClipper(0, &lplpDDClipper,NULL) ) )// DDERR_INCOMPATIBLEPRIMARY
{
MessageBox(NULL,"CreateClipper Create Error","Wrong!", MB_OK);
return hr;
}
lplpDDClipper->SetHWnd(0, hwnd);
if(FAILED(hr = surface->SetClipper(lplpDDClipper)))
{
MessageBox(NULL,"SetClipper Create Error","Wrong!", MB_OK);
return hr;
}
// CClientDC dc(this);
RECT rect;
GetWindowRect(hwnd, &rect);
HDC dc;
surface->GetDC(&dc);
BOOL b =TextOut(dc, 100,100,"hello",5);
// dc->TextOut("heool");
// CClientDC dc(this);
HDC mdc;
//CDC只有空构造函数,也就是说无法通过拷贝函数来构造一个
//与dc相同的mdc,即mdc=dc非法。
//CreateCompatibleDC建立一个适合指定pDC的内存设备上下文
mdc = CreateCompatibleDC(NULL);
///HDC hdc;
CBitmap bitmap;
//CBitmap继承自CGdiObject
//其成员变量m_hObject是一个句柄
//A HANDLE containing the HBITMAP, HPALETTE, HRGN, HBRUSH, HPEN, or HFONT
//attached to this object.
//可以简单记为bitmap拥有HBITMAP类型句柄m_hObject,而该句柄需要使用
//Windows API的LoadImage获得
bitmap.m_hObject=(HBITMAP)::LoadImage(NULL,"logo.bmp",IMAGE_BITMAP,500,400,LR_LOADFROMFILE);
if(bitmap.m_hObject == NULL)
{
MessageBox(NULL,"LoadImage Create Error","Wrong!", MB_OK);
return hr;
}
SelectObject(mdc,bitmap);
// CRect rect;
//取得客户区域大小
// GetClientRect(&rect);
//BitBlt()将位图贴到要显示的DC
hr= BitBlt(dc,0,0,rect.right,rect.bottom,mdc,0,0,SRCCOPY);
if(FAILED(hr))
{
MessageBox(NULL,"BitBlt Create Error","Wrong!", MB_OK);
return hr;
}
// StretchDIBits(dc,100,100,400,400,0,0,400,400,);
//最后要记得在析构函数中释放mdc
// delete mdc;
MSG msg;
while(TRUE)
{
if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
// b =ShowWindow(hwnd, SW_SHOW);
int i;
//cin>>i;
// Sleep(5000);
}
LRESULT CALLBACK WndProc( HWND hWnd,UINT Msg,WPARAM wParam,
LPARAM lParam )
{
switch(Msg)
{
case WM_LBUTTONDOWN:
{
::MessageBeep(MB_ICONHAND);
}
break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hWnd,Msg,wParam,lParam);
}
return 0;
}
bool ShowImage()
{
return true;
}