?? menu.cpp
字號:
// ███████████
// ██╭----------╮██
// █╭╯ Menu.cpp ╰╮█
// █| 菜單類文件 |█
// █╰--------------╯█
// ███████████
// Welcome to http://witsun.myrice.com
#include "stdafx.h"
#include "Menu.h"
#include "DirectDraw.h"
#include <mmsystem.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CDirectDraw* DirectDraw;
extern CMenu* Menu;
extern void Cleanup();
extern LPDIRECTDRAWSURFACE7 CreateBlankSurface(int nWidth, int nHeight);//創(chuàng)建空白頁
extern bool LeftButton;
extern bool MenuMoveable;
extern int nowx,nowy,TileID;
extern void ChangeTile();
extern HWND hWnd;
extern int Menu0,Menu1; //the Tile in Menu's point
extern void TileMenu1();
extern bool MapMove; //地圖中的元素是否呆經(jīng)過?
/////////////////////////////////////////////////////////////////
CMenu::CMenu()
{
MenuNum=0;
}
CMenu::~CMenu()
{
MenuNode* next;
while(!IsEmpty())
{
next=top->link;
for (int i=0;i<top->ButtonID;i++)
SafeRelease(top->ButtonSurface[i]);
SafeRelease(top->MenuSurface);
SafeDelete(top);
top= next;
MenuNum--;
}
}
/////////////////////////////////////////增加菜單
CMenu& CMenu::CreateMenu(const LPCTSTR menuname,
int x,int y,bool havebutton)
{
MenuNum++;
MenuNode* p=new MenuNode;
p->ButtonID=0;
p->MenuName =menuname;
p->MenuX=x;
p->MenuY=y;
p->HaveButton=havebutton;
int width,height;
char* tempname = new char[strlen(menuname)+10];
strcpy(tempname,"pic\\");
strcat(tempname,menuname);
strcat(tempname,".bmp");
p->MenuSurface=BMPToSurface(tempname,width,height);
delete tempname;
p->MenuWidth=width;
p->MenuHeight=height;
p->link =top;
top=p;
destRect.top=top->MenuY;
destRect.left=top->MenuX;
destRect.right=top->MenuX+top->MenuWidth;
destRect.bottom=top->MenuY+top->MenuHeight;
return* this;
}
////////////////////////////////////////刪除頂棧的菜單
CMenu& CMenu::DelMenu()
{
if (!IsEmpty())
{
MenuNode* p=top;
top= top->link;
for (int i=0;i<p->ButtonID;i++)
SafeRelease(p->ButtonSurface[i]);
SafeRelease(p->MenuSurface);
delete p;
MenuNum--;
}
return* this;
}
/////////////////////////////////////////顯示窗口
void CMenu::Show()
{
if (!IsEmpty())
/* {
DirectDraw->CleanSurface();
return;
}
*/
DirectDraw->lpDDSBack->Blt(&destRect,top->MenuSurface,NULL,DDBLT_WAIT,NULL);
}
///////////////////////////////////創(chuàng)建按鈕
void CMenu::CreateButton(const LPCTSTR name,int x,int y)
{
if(top->ButtonID<ButtonMax)
{
top->ButtonName[top->ButtonID]=name;
top->ButtonX[top->ButtonID]=x;
top->ButtonY[top->ButtonID]=y;
int width,height;
char* tempname = new char[strlen(top->MenuName)+strlen(name)+11];
strcpy(tempname,"pic\\");
strcat(tempname,top->MenuName);
strcat(tempname,"_");
strcat(tempname,name);
strcat(tempname,".bmp");
top->ButtonSurface[top->ButtonID]=BMPToSurface(tempname,
width,height);
delete tempname;
top->ButtonWidth[top->ButtonID]=width;
top->ButtonHeight[top->ButtonID]=height;
// DirectDraw->DDSetColorKey(top->ButtonSurface[top->ButtonID],CLR_INVALID);
top->MenuSurface->BltFast(x,y,
top->ButtonSurface[top->ButtonID],
NULL,
DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);
top->ButtonID++;
}
}
///////////////////////////////////創(chuàng)建Tile
void CMenu::CreateTile(LPDIRECTDRAWSURFACE7 face)
{
static int ID=0;
if(top->ButtonID<ButtonMax)
{
top->ButtonName[top->ButtonID]=(LPCTSTR)ID;
top->ButtonX[top->ButtonID]=0;
top->ButtonY[top->ButtonID]=0;
top->ButtonWidth[top->ButtonID]=40;
top->ButtonHeight[top->ButtonID]=40;
top->ButtonSurface[top->ButtonID]=CreateBlankSurface(40, 40);
top->ButtonSurface[top->ButtonID]->BltFast(0,0,
face,
NULL,
DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);
top->ButtonID++;
ID++;
}
}
///////////////////////////////////////////////////加載圖片的函數(shù)
LPDIRECTDRAWSURFACE7 CMenu::BMPToSurface(LPCTSTR file_name,
int& width,int& height)
{
HDC hdc;
HBITMAP bit;
LPDIRECTDRAWSURFACE7 surf;
// load the interface bitmap
bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,
LR_DEFAULTSIZE|LR_LOADFROMFILE);
if (!bit)
// failed to load, return failure to caller
return NULL;
// get bitmap dimensions
BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
int surf_width=bitmap.bmWidth;
int surf_height=bitmap.bmHeight;
width=bitmap.bmWidth;
height=bitmap.bmHeight;
// create surface
HRESULT ddrval;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = surf_width;
ddsd.dwHeight = surf_height;
// attempt to create surface
ddrval=DirectDraw->lpDD->CreateSurface(&ddsd,&surf,NULL);
// created ok?
if (ddrval!=DD_OK) {
// no, release the bitmap and return failure to caller
DeleteObject(bit);
return NULL;
} else {
// yes, get a DC for the surface
surf->GetDC(&hdc);
// generate a compatible DC
HDC bit_dc=CreateCompatibleDC(hdc);
// blit the interface to the surface
SelectObject(bit_dc,bit);
BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);
// release the DCs
surf->ReleaseDC(hdc);
DeleteDC(bit_dc);
}
// clear bitmap
DeleteObject(bit);
// return pointer to caller
return surf;
}
//////////////////////////////////////////////////鼠標(biāo)按下處理
void CMenu::MouseDown()
{
POINT point;
GetCursorPos(&point);
if (!IsEmpty())//是否有菜單?
{
if (top->HaveButton)//是否有按鈕?
{
for (int i=0;i<top->ButtonID;i++)//鼠標(biāo)是否在有在按鈕上?
{
if (point.x>top->MenuX+top->ButtonX[i] &&
point.x<top->MenuX+top->ButtonX[i]+top->ButtonWidth[i] &&
point.y>top->MenuY+top->ButtonY[i] &&
point.y<top->MenuY+top->ButtonY[i]+top->ButtonHeight[i])
{
if ((strcmp(top->MenuName,"TileMenu0")==0 ||strcmp(top->MenuName,"TileMenu1")==0) && i>3)//選擇Tile
{
TileID=i;
return;
}
LPCTSTR A=top->MenuName;
LPCTSTR B=top->ButtonName[i];
char* c = new char[strlen(A)+strlen(B)+2];
strcpy(c,A);
strcat(c,"_");
strcat(c,B);
ButtonClick(c);
delete c;
return;
}
}
}
else//菜單如果沒有按鈕
{
NotButtonClick(top->MenuName);
return;
}
if(point.x<top->MenuX ||
point.x>top->MenuX+top->MenuWidth ||
point.y<top->MenuY ||
point.y>top->MenuY+top->MenuHeight)//鼠標(biāo)是否在菜單外面?
ChangeTile();//change Tile.
}
else
ChangeTile();
}
//////////////////////////////////////////////////鼠標(biāo)移動(dòng)處理
void CMenu::MouseMove()
{
if (!IsEmpty() && top->HaveButton)//是否有菜單并有按鈕?
{
POINT point;
GetCursorPos(&point);
for (int i=0;i<top->ButtonID;i++)
{
if (point.x>top->MenuX+top->ButtonX[i] &&
point.x<top->MenuX+top->ButtonX[i]+top->ButtonWidth[i] &&
point.y>top->MenuY+top->ButtonY[i] &&
point.y<top->MenuY+top->ButtonY[i]+top->ButtonHeight[i])//鼠標(biāo)在按鈕或Tile上時(shí)
{
if ((strcmp(top->MenuName,"TileMenu0")==0 ||strcmp(top->MenuName,"TileMenu1")==0) && i>3)//如杲在Tile上就反回
return;
LPCTSTR A=top->MenuName;
LPCTSTR B=top->ButtonName[i];
char* c = new char[strlen(A)+strlen(B)+5];
strcpy(c,A);
strcat(c,"_");
strcat(c,B);
strcat(c,"_On");
ButtonOn(i,c);
delete c;
return;
}
else//如果不在按鈕或Tile上
{
if ((strcmp(top->MenuName,"TileMenu0")!=0 || strcmp(top->MenuName,"TileMenu1")!=0) && i<4)//是否不是Tile
{
DirectDraw->DDSetColorKey(top->ButtonSurface[i],CLR_INVALID);
top->MenuSurface->BltFast(top->ButtonX[i],top->ButtonY[i],
top->ButtonSurface[i],
NULL,
DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);
}
}
}
if (point.x>top->MenuX &&
point.x<top->MenuX+top->MenuWidth &&
point.y>top->MenuY &&
point.y<top->MenuY+top->MenuHeight &&
LeftButton)
{
nowx=point.x;
nowy=point.y;
MenuMoveable=true;
return;
}
}
}
void CMenu::ButtonOn(int buttonid,LPCTSTR buttonname)
{
int width,height;
LPDIRECTDRAWSURFACE7 tempsurface;
char* tempname = new char[strlen(buttonname)+10];
strcpy(tempname,"pic\\");
strcat(tempname,buttonname);
strcat(tempname,".bmp");
tempsurface=BMPToSurface(tempname,
width,height);
delete tempname;
// DirectDraw->DDSetColorKey(tempsurface,CLR_INVALID);
top->MenuSurface->BltFast(top->ButtonX[buttonid],top->ButtonY[buttonid],
tempsurface,
NULL,
DDBLTFAST_WAIT/*|DDBLTFAST_SRCCOLORKEY*/);
}
// ████████████████████████████
// ██╭--------------------------------------------╮██
// █╭╯ 以 下 為 鼠 標(biāo) 事 件 區(qū) ╰╮█
// █| |█
// █| |█
// █| ★鼠 標(biāo) 單 擊 與 鼠 標(biāo) 移 動(dòng)★ |█
// █╰------------------------------------------------╯█
// ████████████████████████████
//////////////////////////////////////////////////鼠標(biāo)單擊事情
void ButtonClick(LPCTSTR name)
{
if (strcmp(name,"TileMenu1_End")==0)
{
Menu->DelMenu();
}
else if (strcmp(name,"TileMenu1_Move")==0)
{
MapMove=!MapMove;
}
else if (strcmp(name,"TileMenu1_Up")==0)
{
Menu1-=30;
if (Menu1<1)
Menu1=0;
Menu->DelMenu();
TileMenu1();
}
else if (strcmp(name,"TileMenu1_Down")==0)
{
Menu1+=30;
if (Menu1>540+18)
Menu1=600-48;
Menu->DelMenu();
TileMenu1();
}
else if (strcmp(name,"Exit_Yes")==0)
{
DestroyWindow(hWnd);
}
else if (strcmp(name,"Exit_No")==0)
{
Menu->DelMenu();
}
else if (strcmp(name,"TileMenu0_End")==0)
{
Menu->DelMenu();
}
else if (strcmp(name,"TileMenu0_Move")==0)
{
MapMove=!MapMove;
}
else if (strcmp(name,"")==0)
{
}
}
void NotButtonClick(LPCTSTR name)
{
if (strcmp(name,"About")==0)
{
Menu->DelMenu();
return;
}
else if (strcmp(name,"")==0)
{
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -