|
|
发表于 2006-6-27 15:46:00
|
显示全部楼层
你试试我的。呵呵,其实很简单的
/*********************************************************
// 代码说明 DX窗口基本框架
//
//编号:0001
//日期:15:29 2006-6-27
//QQ:85258604
//网名:£ 草下飞 £
//E-MAIL:nbxiong2002@yahoo.com.cn
//BLOG:nbxiong.blog.com.cn
*********************************************************/
#include "stdafx.h"
#include "resource.h"
#include <ddraw.h>
#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
BOOL bActive = FALSE; // 用于判断程序是否运行的变量
LPDIRECTDRAWSURFACE7 ppsur;
LPDIRECTDRAWSURFACE7 pbbur;
LPDIRECTDRAWSURFACE7 popla;
LPDIRECTDRAWSURFACE7 popla2;
DDSURFACEDESC2 desc;
DDSCAPS2 caps;
// 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);
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_D0001, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_D0001);
// Main message loop:
while(1) // 程序的循环
{
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) // 不要特别注意的
{
if(!GetMessage(&msg, NULL, 0, 0 ))
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(bActive) // 就是上面的“程序是否激活”的变量判断
{
// ShowThePic(); 显示人物图片的函数,上节的内容,就是整合在这儿的!
}
else WaitMessage(); // 没有消息时,就等消息:)
}
/*
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_D0001);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;//(LPCSTR)IDC_D0001;
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_POPUP,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
////hWnd = CreateWindowEx(WS_EX_TOPMOST, szWindowClass, szTitle, WS_POPUP, 112, 84, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
LPDIRECTDRAW7 pDD7;
HRESULT result;
result=DirectDrawCreateEx(NULL,(void **)&pDD7,IID_IDirectDraw7,NULL);
if (result!=DD_OK)
{
MessageBox(NULL,"建立DX对象失败!","程序初始化",MB_OK);
return FALSE;
}
result=pDD7->SetCooperativeLevel(hWnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);
if (result!=DD_OK)
{
MessageBox(NULL,"设定程序协调级失败!","程序初始化",MB_OK);
return FALSE;
}
result=pDD7->SetDisplayMode(1024,768,32,0,DDSDM_STANDARDVGAMODE);
if (result!=DD_OK)
{
MessageBox(NULL,"设定显示模式失败!","程序初始化",MB_OK);
return FALSE;
}
memset(&desc,0,sizeof(desc));
desc.dwSize=sizeof(desc);
desc.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
desc.dwBackBufferCount=1;
desc.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
result=pDD7->CreateSurface(&desc,&ppsur,NULL);
if(result!=DD_OK)
{
MessageBox(NULL,"建立主缓冲区失败!","程序初始化",MB_OK);
return FALSE;
}
caps.dwCaps=DDSCAPS_BACKBUFFER;
result=ppsur->GetAttachedSurface(&caps,&pbbur);
if(result!=DD_OK)
{
MessageBox(NULL,"连接后缓冲区失败!","程序初始化",MB_OK);
return FALSE;
}
memset(&desc,0,sizeof(desc));
desc.dwSize=sizeof(desc);
desc.dwFlags=DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH;
desc.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
desc.dwWidth=1024;
desc.dwHeight=768;
result=pDD7->CreateSurface(&desc,&popla,NULL);
if(result!=DD_OK)
{
MessageBox(NULL,"建立幕后内存区失败!","程序初始化",MB_OK);
return FALSE;
}
result=pDD7->CreateSurface(&desc,&popla2,NULL);
if(result!=DD_OK)
{
MessageBox(NULL,"建立幕后内存区二失败!","程序初始化",MB_OK);
return FALSE;
}
HDC hdc,hdc1;
HBITMAP bitmap,rwmap;
hdc=CreateCompatibleDC(NULL);
bitmap=(HBITMAP)LoadImage(NULL,"x.bmp",IMAGE_BITMAP,1024,768,LR_LOADFROMFILE);
if(bitmap==NULL)
{
MessageBox(NULL,"读取位图失败!","程序初始化",MB_OK);
return FALSE;
}
rwmap=(HBITMAP)LoadImage(NULL,"z.bmp",IMAGE_BITMAP,320,185,LR_LOADFROMFILE);
if(rwmap==NULL)
{
MessageBox(NULL,"读取人物位图失败!","程序初始化",MB_OK);
return FALSE;
}
SelectObject(hdc,bitmap);
popla->GetSurfaceDesc(&desc);
result=popla->GetDC(&hdc1);
BitBlt(hdc1,0,0,desc.dwWidth,desc.dwHeight,hdc,0,0,SRCCOPY);
SelectObject(hdc,rwmap);
BitBlt(hdc1,250,150,160,185,hdc,160,0,SRCAND);
BitBlt(hdc1,250,150,160,185,hdc,0,0,SRCPAINT);
popla->ReleaseDC(hdc1);
bitmap=(HBITMAP)LoadImage(NULL,"x2.bmp",IMAGE_BITMAP,1024,768,LR_LOADFROMFILE);
if(bitmap==NULL)
{
MessageBox(NULL,"读取位图失败!","程序初始化",MB_OK);
return FALSE;
}
BITMAP bm;
GetObject(rwmap,sizeof(BITMAP),&bm);
unsigned char *px=new unsigned char[bm.bmHeight*bm.bmWidthBytes];
GetBitmapBits(rwmap,bm.bmHeight*bm.bmWidthBytes,px);
for (int y=0;y<bm.bmHeight;y++)
{
for (int x=0;x<bm.bmWidthBytes;x++)
{
px[x*3+y*bm.bmHeight]*=0.5;
px[x*3+1+y*bm.bmHeight]*=0.5;
px[x*3+2+y*bm.bmHeight]*=0.5;
}
}
GetObject(bitmap,sizeof(BITMAP),&bm);
unsigned char *px2=new unsigned char[bm.bmHeight*bm.bmWidthBytes];
GetBitmapBits(bitmap,bm.bmHeight*bm.bmWidthBytes,px2);
int i=0;
for (y=20;y<348;y++)
{
for (int x=150;x<447;x++)
{
px2[x*3+y*bm.bmHeight]=px2[x*3+y*bm.bmHeight]*0.5+px;
px2[x*3+1+y*bm.bmHeight]=px2[x*3+1+y*bm.bmHeight]*0.5+px[i+1];
px2[x*3+2+y*bm.bmHeight]=px2[x*3+2+y*bm.bmHeight]*0.5+px[i+2];
i+=3;
}
}
SetBitmapBits(bitmap,bm.bmHeight*bm.bmWidthBytes,px2);
SelectObject(hdc,bitmap);
popla2->GetSurfaceDesc(&desc);
result=popla2->GetDC(&hdc1);
BitBlt(hdc1,0,0,desc.dwWidth,desc.dwHeight,hdc,0,0,SRCCOPY);
/* SelectObject(hdc,rwmap);
BitBlt(hdc1,250,150,160,185,hdc,160,0,SRCAND);*/
popla2->ReleaseDC(hdc1);
return TRUE;
}
//
// 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;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
RECT rt;
GetClientRect(hWnd,&rt);
rt.top=0;
rt.left=0;
rt.bottom=768;
rt.right=1024;
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:
// pbbur->BltFast(0,0,popla,&rt,DDBLTFAST_WAIT);
// ppsur->Flip(NULL,DDFLIP_WAIT);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_ACTIVATE:
bActive = TRUE;
break;
case WM_SETCURSOR:
SetCursor(NULL);
return TRUE;
case WM_LBUTTONDOWN:
pbbur->BltFast(0,0,popla,&rt,DDBLTFAST_WAIT);
ppsur->Flip(NULL,DDFLIP_WAIT);
return TRUE;
case WM_RBUTTONDOWN:
pbbur->BltFast(0,0,popla2,&rt,DDBLTFAST_WAIT);
ppsur->Flip(NULL,DDFLIP_WAIT);
return TRUE;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE: // 处理按下 ESC 键的反应
PostMessage(hWnd, WM_CLOSE, 0, 0); // 发送一个关闭窗口的命令
break;
}
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;
}
/*********************************************************
//
■■■■■■■■■■■■■■■■■■■■■■■■
这个世界只有两种人,懂二进制的和不懂二进制的。
■■■■■■■■■■■■■■■■■■■■■■■■
//
*********************************************************/ |
|