⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 roomstatus.~cpp

📁 全面剖析酒店管理的内容
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "RoomStatus.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfmRoomStatus *fmRoomStatus;
//---------------------------------------------------------------------------
__fastcall TfmRoomStatus::TfmRoomStatus(TComponent* Owner)
    : TForm(Owner)
{
    // 默认不可以编辑房态
    m_SelectRoom = 0;
    m_RoomCode = "";
    m_RoomType = "";
}
//---------------------------------------------------------------------------
// 刷新状态图
void __fastcall TfmRoomStatus::btUpdateStatusClick(TObject *Sender)
{
    // 清空
    for(int i=0; i<100; i++)
        for(int j=0; j<100; j++)
            szCellCode[i][i] = "";

    TQuery* pQueryFloor = new TQuery(NULL);
    TQuery* pQueryRoom = new TQuery(NULL);
    pQueryFloor->DatabaseName = "db";
    pQueryRoom->DatabaseName = "db";
    pQueryFloor->SQL->Add("select distinct 楼层编号 from 客房信息");
    pQueryFloor->Open();
    int nRow = 0;
    // 循环楼层,显示不同楼层的客房
    while(!pQueryFloor->Eof)
    {
        nRow ++;
        StringGrid1->RowCount = nRow + 1;
        AnsiString szFloor;
        szFloor = pQueryFloor->FieldByName("楼层编号")->AsString;
        StringGrid1->Cells[0][nRow] = szFloor + "/F";
        // 对应改楼层所有房间的状态,填入到stringgrid的cell中
        // 循环客房,显示同一楼层所有客房
        pQueryRoom->SQL->Clear();
        AnsiString sql;
        sql = "select 客房编号,状态 from 客房信息 where 楼层编号 = '";
        sql += szFloor + "'";
        // 客房类型
        if(m_RoomType.Length()>0)
        {
            sql += " and 类型编号='" + m_RoomType + "'";
        }
        sql += " order by 客房编号";
        pQueryRoom->SQL->Add(sql);
        pQueryRoom->Open();
        int nCol = 0;     // 总列数
        while(!pQueryRoom->Eof)
        {
            nCol++;
            if(StringGrid1->ColCount < nCol + 1)
                StringGrid1->ColCount = nCol + 1;
            // 设置第一行(列标题)
            if(StringGrid1->Cells[nCol][0].IsEmpty())
                StringGrid1->Cells[nCol][0] = IntToStr(nCol);
            // 记录房间编号
            szCellCode[nCol][nRow] = pQueryRoom->FieldByName("客房编号")->AsString;;
            // 设置客房的状态
            StringGrid1->Cells[nCol][nRow] =
                pQueryRoom->FieldByName("状态")->AsString;
            // 下一客房
            pQueryRoom->Next();
        }
        // 对于预定和入住的选择,应该清楚不是该客房类型的客房
        for(int i=nCol+1; i< StringGrid1->ColCount; i++)
        {
            StringGrid1->Cells[i][nRow] = "";
            szCellCode[i][nRow] = "";
        }
        //-------------------------
        // 下一楼层
        pQueryFloor->Next();
    }
    if(Visible) StringGrid1->SetFocus();
    // 释放空间
    delete pQueryFloor;
    delete pQueryRoom;
}
//---------------------------------------------------------------------------
void __fastcall TfmRoomStatus::FormShow(TObject *Sender)
{
    // 刷新状态图
    btUpdateStatusClick(NULL);
}
//---------------------------------------------------------------------------
// 绘制状态图
// 自己绘制StringGrid,以图片形式代替文本输出
void __fastcall TfmRoomStatus::StringGrid1DrawCell(TObject *Sender,
      int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
    // 第一行,列标题,演示如何将文本对中输出
    if(ARow == 0)
    {
        // 将原来的区域填充
        StringGrid1->Canvas->FillRect(Rect);
        // 重新输出文本,居中对齐
        AnsiString szCon = StringGrid1->Cells[ACol][ARow];  //显示的内容
        // 先获取输出文本宽度
        int nTextLen = StringGrid1->Canvas->TextWidth(szCon);
        StringGrid1->Canvas->TextRect(Rect, Rect.Left+Rect.Width()/2-nTextLen/2,
            Rect.Top+8, szCon );
    }
    // -------------------------
    // 显示状态图形
    TImage* pImage = NULL;
    // 判断用什么图片显示
    if(StringGrid1->Cells[ACol][ARow] == "空房")
        pImage = imEmpty;
    else if(StringGrid1->Cells[ACol][ARow] == "住房")
        pImage = imNoEmpty;
    else if(StringGrid1->Cells[ACol][ARow] == "自用")
        pImage = imSelf;
    else if(StringGrid1->Cells[ACol][ARow] == "维修")
        pImage = imMaintain;
    else if(StringGrid1->Cells[ACol][ARow] == "将到")
        pImage = imArrive;
    else if(StringGrid1->Cells[ACol][ARow] == "将离")
        pImage = imLeave;

    if(pImage)   // 存在图片,替换文字输出
    {
        // 将原来的区域填充
        StringGrid1->Canvas->FillRect(Rect);
        // 输出图片
        StringGrid1->Canvas->Draw(Rect.Left+(Rect.Width()-pImage->Width)/2,
            Rect.Top+1, pImage->Picture->Graphic);
        // 在图片上再输出客房号
        AnsiString szCon = szCellCode[ACol][ARow];  //显示的内容
        // 获取输出文本宽度
        int nTextLen = StringGrid1->Canvas->TextWidth(szCon);
        // 将文本输出设置为透明模式
        // 保留原来的模式
        TBrushStyle style = StringGrid1->Canvas->Brush->Style;
        StringGrid1->Canvas->Brush->Style = bsClear;
        // 设置字体字号,小字号
        // 保留原来字号
        int size = StringGrid1->Canvas->Font->Height;
        StringGrid1->Canvas->Font->Height = -1;
        // 客房编号输出
        StringGrid1->Canvas->TextOut(Rect.Left+Rect.Width()/2-nTextLen/2,
            Rect.Top+22, szCon );
        // 恢复原来的模式
        StringGrid1->Canvas->Brush->Style = style;
        // 恢复原来字号
        StringGrid1->Canvas->Font->Height = size;
    }
}
//---------------------------------------------------------------------------
// 选择Cell时,更新选择的房态
void __fastcall TfmRoomStatus::StringGrid1SelectCell(TObject *Sender,
      int ACol, int ARow, bool &CanSelect)
{
    if(StringGrid1->Cells[ACol][ARow] == "空房")
        rbEmpty->Checked = true;
    else if(StringGrid1->Cells[ACol][ARow] == "住房")
        rbNoEmpty->Checked = true;
    else if(StringGrid1->Cells[ACol][ARow] == "自用")
        rbSelf->Checked = true;
    else if(StringGrid1->Cells[ACol][ARow] == "维修")
        rbMaintain->Checked = true;
    else if(StringGrid1->Cells[ACol][ARow] == "将到")
        rbArrive->Checked = true;
    else if(StringGrid1->Cells[ACol][ARow] == "将离")
        rbLeave->Checked = true;
    m_nCurCol = ACol;
    m_nCurRow = ARow;
}
//---------------------------------------------------------------------------
// 更改选择客房的房态
void __fastcall TfmRoomStatus::btSetStatusClick(TObject *Sender)
{
    // 没有选择客房
    if(szCellCode[m_nCurCol][m_nCurRow].IsEmpty())
    {
        Application->MessageBox("没有选择客房","提示",MB_OK);
        return;
    }
    TQuery* pQueryRoom = new TQuery(NULL);
    pQueryRoom->DatabaseName = "db";
    AnsiString szStatus;
    if(rbEmpty->Checked)
         szStatus = "空房";
    else if(rbNoEmpty->Checked)
         szStatus = "住房";
    else if(rbSelf->Checked)
         szStatus = "自用";
    else if(rbMaintain->Checked)
         szStatus = "维修";
    else if(rbArrive->Checked)
         szStatus = "将到";
    else if(rbLeave->Checked)
         szStatus = "将离";
    AnsiString sql = "update 客房信息 set 状态='";
    sql += szStatus + "' where 客房编号='";
    sql += szCellCode[m_nCurCol][m_nCurRow] + "'";
    int nCol, nRow;
    nCol = m_nCurCol;
    nRow = m_nCurRow;
    pQueryRoom->SQL->Add(sql);
    // 更新
    pQueryRoom->ExecSQL();
    // 重新刷新房态图
    btUpdateStatusClick(NULL);
    // 重新定位选择的cell
    StringGrid1->Col = nCol;
    StringGrid1->Row = nRow;
    StringGrid1->SetFocus();
    delete pQueryRoom;
}
//---------------------------------------------------------------------------
void __fastcall TfmRoomStatus::StringGrid1DblClick(TObject *Sender)
{
    // 双击选择一个客房,返回客房编号
    if(m_SelectRoom == 0 && szCellCode[m_nCurCol][m_nCurRow].Length()>0)
    {
        //只能选择空房!!
        if(StringGrid1->Cells[m_nCurCol][m_nCurRow] != "空房")
        {
            Application->MessageBox("选择的客房不是空房!","提示",MB_OK);
            return;
        }
        m_RoomCode = szCellCode[m_nCurCol][m_nCurRow];
        // 选择ok,关闭窗体
        Close();
    }
}
//---------------------------------------------------------------------------
void __fastcall TfmRoomStatus::FormClose(TObject *Sender,
      TCloseAction &Action)
{
    // 删除窗体并回收空间
    Action = caFree;    
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -