📄 menubar.cpp
字号:
/*******************************************************************************
模 块: 菜单条.
功 能: 能用光标移动选择菜单项并挂接 SubMenu.
程序员: 雷中南.
版 本: v1.1
时 间: 1999-05-10
*******************************************************************************/
#include <graphics.h>
#include <string.h>
#include "menubar.h"
#include "han.h"
#include "def.h"
//菜单横条所在的位置.
struct RECT MBRect={0,0,639,46};
//构造函数.
MenuBar::MenuBar(char *submenu[],int number)
:Object(MBRect)
{
//记录菜单条上的名称.
for(int i=0; i<number; i++)
subMenu[i]=submenu[i];
SetBkColor(7);
//设置汉字颜色.
han.Color = 0;
//子菜单数量.
SubMenuNumber = number;
//子菜单焦点所在位置.
SubMenuFocus = 0;
}
//显示子菜单上的文字.
void
MenuBar::ItemDraw()
{
int Left=8;
han.Color = 0;
//按标准距离输出文本.
for (int i=0; i<SubMenuNumber; i++)
{
han.Out(Left, Rect.Height/2-8, subMenu[i]);
Left = Left + 8 + strlen(subMenu[i]) * 8;
}
}
//运行菜单条.
void
MenuBar::DoIt()
{
int Key;
FocusDraw();
for(;;)
{
//获取键盘.
Key = GetKey();
switch (Key)
{
//如果按左箭头.
case KEY_LEFT:
//跳到前一个菜单项.
PrevItem();
PutEvent(MN_PREVSUBMENU);
return;
//如果按右箭头.
case KEY_RIGHT:
//跳到下一个菜单项.
NextItem();
PutEvent(MN_NEXTSUBMENU);
return;
//按下回车键.
case KEY_RETURN:
PutEvent(MN_SUBCALL);
return;
//按下ESC键.
case KEY_ESC:
PutEvent(EV_QUIT);
return;
}
}
}
//跳到下一个菜单项.
void
MenuBar::NextItem()
{
//如果有子菜单:
if(SubMenuNumber>0)
{
//如果当前子菜单为最后一项.
if(SubMenuFocus < SubMenuNumber - 1)
{
//覆盖焦点.
FocusRestore();
//将当前子菜单改为下一项.
SubMenuFocus++;
//画焦点.
FocusDraw();
}
else
{
//覆盖焦点.
FocusRestore();
//将当前子菜单改为第一项.
SubMenuFocus = 0;
//画焦点.
FocusDraw();
}
}
}
//跳到前一个菜单项.
void
MenuBar::PrevItem()
{
//如果有子菜单.
if(SubMenuNumber>0)
{
//如果当前子菜单不是第一项的话:
if(SubMenuFocus>0)
{
//覆盖焦点.
FocusRestore();
//将当前子菜单改为前一项.
SubMenuFocus--;
//画焦点.
FocusDraw();
}
//如果当前子菜单是第一项的话:
else
{
//覆盖焦点.
FocusRestore();
//将当前子菜单改为最后一项.
SubMenuFocus = SubMenuNumber-1;
//画焦点.
FocusDraw();
}
}
}
//计算子菜单的菜单框的位置.
struct RECT
MenuBar::CalRect(char *submenu[], int number)
{
struct RECT rect;
int temp,BlockLeft=8,size=0;
//根据菜单条的标题位置确定方框的左上角.
for(temp=0; temp<SubMenuFocus; temp++)
{
BlockLeft=BlockLeft+strlen(subMenu[temp])*8+8;
}
rect.Left=BlockLeft;
rect.Top=Rect.Height;
//根据子菜单每一项的标题宽度确定方框的右下角.
for(temp=0; temp<number; temp++)
{
if(size<strlen(submenu[temp])) size=strlen(submenu[temp]);
}
rect.Width=24+size*8;
rect.Height=2 + number*24+4;
//返回计算的结果.
return rect;
}
//恢复菜单项.
//当焦点消失时调用它可恢复得到焦点前的情况.
void
MenuBar::FocusRestore()
{
int temp,BlockLeft=8;
//计算焦点的位置.
for(temp=0; temp<SubMenuFocus; temp++)
{
BlockLeft=BlockLeft+strlen(subMenu[temp])*8+8;
}
//恢复背景.
setfillstyle(SOLID_FILL,BackColor);
bar(BlockLeft-4,
Rect.Height/2-10,
BlockLeft+strlen(subMenu[SubMenuFocus])*8+4,
Rect.Height/2+10);
//恢复菜单标题.
han.Color = 0;
han.Out(BlockLeft, Rect.Height/2-8, subMenu[SubMenuFocus]);
}
//画焦点.
void
MenuBar::FocusDraw()
{
int temp,BlockLeft=8;
//根据每一项的长度,计算当前焦点所在的屏幕位置.
for(temp=0; temp<SubMenuFocus; temp++)
{
BlockLeft=BlockLeft+strlen(subMenu[temp])*8+8;
}
//画焦点方块.
setfillstyle(SOLID_FILL,1);
bar(BlockLeft-4,
Rect.Height/2-10,
BlockLeft+strlen(subMenu[SubMenuFocus])*8+4,
Rect.Height/2+10);
//画3d外围线.
setcolor(15);
line(BlockLeft-4, Rect.Height/2-10,BlockLeft+strlen(subMenu[SubMenuFocus])*8+4,Rect.Height/2-10);
line(BlockLeft-4, Rect.Height/2-10,BlockLeft-4,Rect.Height/2+10);
setcolor(8);
line(BlockLeft-4,Rect.Height/2+10,BlockLeft+strlen(subMenu[SubMenuFocus])*8+4,Rect.Height/2+10);
line(BlockLeft+strlen(subMenu[SubMenuFocus])*8+4,Rect.Height/2-10,BlockLeft+strlen(subMenu[SubMenuFocus])*8+4,Rect.Height/2+10);
//输出高亮文本.
temp=han.Color;
han.Color=15;
han.Out(BlockLeft, Rect.Height/2-8, subMenu[SubMenuFocus]);
han.Color=temp;
}
//画菜单条.
void
MenuBar::Draw()
{
//画基类对象.
Object::Draw();
//画3d边框线.
setcolor(15);
line(0, 0, Rect.Width, 0);
line(0, 0, 0, Rect.Height);
line(1, Rect.Height-1, Rect.Width-1, Rect.Height-1);
line(Rect.Width-1, 1, Rect.Width-1, Rect.Height-1);
setcolor(8);
line(1, 1, Rect.Width-2, 1);
line(1, 1, 1, Rect.Height-2);
line(0, Rect.Height, Rect.Width, Rect.Height);
line(Rect.Width, 0, Rect.Width, Rect.Height);
//显示菜单标题.
ItemDraw();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -