📄 box.cpp
字号:
/*******************************************************************************
模 块: 常用对话框.
功 能: Msgbox, QuestionBox, LoginBox, InputBox, PasswordBox
程序员: 雷中南.
版 本: v1.1
时 间: 1999-05-10
*******************************************************************************/
#include <graphics.h>
#include <string.h>
#include "def.h"
#include "box.h"
#include "han.h"
#include "inpscase.h"
#include "inptcase.h"
/****************************************************************************
消息窗。
****************************************************************************/
//构造函数.输入参数为:消息.
MsgBox::MsgBox(char *buf)
: Window(CreateRect(100,180,440,120))//产生消息窗.
{
//构造确定按钮.
btOK = new Button(CreateRect(Rect.Left + 180, Rect.Top + 80, 80, 30), "确 定", CMD_YES);
//将按钮插入窗口.
Insert(btOK);
//传递消息.
Msg = buf;
//显示消息窗口.
Show();
}
//析构函数.
MsgBox::~MsgBox()
{
//删除按钮.
delete btOK;
}
//运行窗口.
void
MsgBox::DoIt()
{
//定义事件.
unsigned int Event;
//窗口运行的循环.
for(;;)
{
//先运行窗口本身.
Window::DoIt();
//获取窗口所产生的事件.
GetEvent(Event);
switch(Event)
{
//如果是确定按钮事件.
case CMD_YES:
//返回.
return;
//如果是退出事件.
case EV_QUIT:
//返回.
return;
}
}
}
//重画窗口.
//虽然按下面的函数可以自动分割三行文本,但存在一个问题,如果换行位置在一个
//汉字的中间,结果将变得非常不好.
void
MsgBox::Draw()
{
//top消息第一行顶端的坐标, n是行数.
int top, n;
//临时字符串.
char *tmsg;
//复制一行字符的临时字符串.
char temp[51];
//重画消息窗口.
Window::Draw();
//输出消息到窗口.
han.Color = 0;
//临时字符串.
tmsg = Msg;
//行数.以50个字符为一行.
n = (int)((strlen(tmsg) / 50.0) + 0.99);
//计算顶端坐标.
top=(int)(80.0 - n*22.0)/2;
//循环写文本.
for(int i=0; i<n; i++)
{
if(strlen(tmsg) >= 50)
{
//如果消息长度大于50.
//拷贝前50个字符.
strncpy(temp, tmsg, 50);
temp[50] = NULL;
}
else
{
//如果消息长度小于50.
//拷贝全部字符串.
strncpy(temp, tmsg, strlen(tmsg));
temp[strlen(tmsg)]=NULL;
}
//输出temp中的内容.
han.Out(20, i*22.0 + top, temp);
//如果字符串长度大于50个,字符串指针往后播50.
if(strlen(tmsg) >= 50)
{
tmsg += 50;
}
}
}
/****************************************************************************
问题窗。
****************************************************************************/
//构造函数.输入参数为:消息,按钮文本1,按钮文本2.
QuestionBox::QuestionBox(char *buf, char *bt1, char *bt2)
:Window(CreateRect(100,180,440,120))//产生问题窗.
{
//构造确定按钮.
btOK = new Button(CreateRect(Rect.Left + 110, Rect.Top + 80, 80, 30), bt1, CMD_YES);
//将按钮插入窗口.
Insert(btOK);
btNO = new Button(CreateRect(Rect.Left + 250, Rect.Top + 80, 80, 30), bt2, CMD_NO);
//将按钮插入窗口.
Insert(btNO);
//消息文本.
Msg = buf;
//显示窗口.
Show();
}
//析构函数.
QuestionBox::~QuestionBox()
{
delete btOK;
delete btNO;
}
//运行窗口.
void
QuestionBox::DoIt()
{
//定义事件.
unsigned int Event;
//窗口运行的循环.
for(;;)
{
//先运行窗口本身.
Window::DoIt();
//获取窗口所产生的事件.
GetEvent(Event);
switch(Event)
{
//如果是确定按钮事件.
case CMD_YES:
PutEvent(CMD_YES);
//返回.
return;
//如果是取消按钮事件.
case CMD_NO:
PutEvent(CMD_NO);
//返回.
return;
//如果是退出事件.
case EV_QUIT:
PutEvent(EV_QUIT);
//返回.
return;
}
}
}
//重画窗口.
//虽然按下面的函数可以自动分割三行文本,但存在一个问题,如果换行位置在一个
//汉字的中间,结果将变得非常不好.
void
QuestionBox::Draw()
{
//top消息第一行顶端的坐标, n是行数.
int top, n;
//临时字符串.
char *tmsg;
//复制一行字符的临时字符串.
char temp[51];
//重画消息窗口.
Window::Draw();
//输出消息到窗口.
han.Color = 0;
//临时字符串.
tmsg = Msg;
//行数.以50个字符为一行.
n = (int)((strlen(tmsg) / 50.0) + 0.99);
//计算顶端坐标.
top=(int)(80.0 - n*22.0)/2;
//循环写文本.
for(int i=0; i<n; i++)
{
if(strlen(tmsg) >= 50)
{
//如果消息长度大于50.
//拷贝前50个字符.
strncpy(temp, tmsg, 50);
temp[50] = NULL;
}
else
{
//如果消息长度小于50.
//拷贝全部字符串.
strncpy(temp, tmsg, strlen(tmsg));
temp[strlen(tmsg)]=NULL;
}
//输出temp中的内容.
han.Out(20, i*22.0 + top, temp);
//如果字符串长度大于50个,字符串指针往后播50.
if(strlen(tmsg) >= 50)
{
tmsg += 50;
}
}
}
/****************************************************************************
登录窗。
****************************************************************************/
//构造函数.
LoginBox::LoginBox()
:Window(CreateRect(200,190,240,120))//产生登录窗.
{
//用户框.
User = new Edit(CreateRect(Rect.Left + 110, Rect.Top + 20, 100, 20));
Insert(User);
//口令框.
Password = new Edit(CreateRect(Rect.Left + 110, Rect.Top + 50, 100, 20));
//设置文本框为密码方式.
Password->IsPassword = L_OK;
Insert(Password);
//产生确定按钮
btOK = new Button(CreateRect(Rect.Left + 50, Rect.Top + 85, 60, 25), "确定", CMD_YES);
//将按钮插入窗口.
Insert(btOK);
//产生取消按钮.
btNO = new Button(CreateRect(Rect.Left + 140, Rect.Top + 85, 60, 25), "取消", CMD_NO);
//将按钮插入窗口.
Insert(btNO);
//显示窗口.
Show();
}
//析构函数.
LoginBox::~LoginBox()
{
delete User;
delete Password;
delete btOK;
delete btNO;
}
//运行窗口.
void
LoginBox::DoIt()
{
//定义事件.
unsigned int Event;
//窗口运行的循环.
for(;;)
{
//先运行窗口本身.
Window::DoIt();
//获取窗口所产生的事件.
GetEvent(Event);
switch(Event)
{
//如果是确定按钮事件.
case CMD_YES:
PutEvent(CMD_YES);
//返回.
return;
//如果是取消按钮事件.
case CMD_NO:
User->SetText("");
Password->SetText("");
break;
case EV_QUIT:
PutEvent(EV_QUIT);
//返回.
return;
}
}
}
//获取用户名.
char *
LoginBox::GetUser()
{
return User->Text();
}
//获取口令.
char *
LoginBox::GetPassword()
{
return Password->Text();
}
//重画窗口.
void
LoginBox::Draw()
{
Window::Draw();
han.Color=0;
han.Out(30, 23, "用户名:");
han.Out(30, 53, "口 令:");
}
/****************************************************************************
文本输入窗。
****************************************************************************/
//构造函数.
InputBox::InputBox(char *buf)
:Window(CreateRect(150, 190, 340, 120))//信息输入窗口外框.
{
//消息文本.
Msg = buf;
//输入文本框.
InputLine = new Edit(CreateRect(Rect.Left + 30, Rect.Top + 50, 280, 20));
//将文本框插入窗口.
Insert(InputLine);
//确定按钮.
btOK = new Button(CreateRect(Rect.Left + 40, Rect.Top + 80, 80, 25), "确 定", CMD_YES);
//将按钮插入窗口.
Insert(btOK);
//取消按钮.
btNO = new Button(CreateRect(Rect.Left + 220, Rect.Top + 80, 80, 25), "取 消", CMD_NO);
//将按钮插入窗口.
Insert(btNO);
//显示窗口.
Show();
}
//析构函数.
InputBox::~InputBox()
{
delete InputLine;
delete btOK;
delete btNO;
}
//设置文本.
void
InputBox::SetText(char *buf)
{
//设置文本框中的文本.
InputLine->SetText(buf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -