MediaItem.h
#ifndef MEDIAITEM_H
#define MEDIAITEM_H
#include <string>
using namespace std;
//enum Rating { UNRATED, GENERAL, ADULT, CHILDREN }; // 评级
enum MediaType { BOOK, VIDEO, PICTURE }; // 物品类型
// 基类:物品
class MediaItem {
public:
wchar_t* id; // 编号
wchar_t* title; // 标题
wchar_t* author; // 作者
wchar_t* rating; // 评级
MediaItem(wchar_t* id, wchar_t* title, wchar_t* author, wchar_t* rating)
: id(id), title(title), author(author), rating(rating) {
}
virtual ~MediaItem() {}
virtual void display() const = 0; // 纯虚函数,显示物品信息
virtual wstring info() const = 0;
};
#endif
Book.h
#ifndef BOOK_H
#define BOOK_H
#include "MediaItem.h"
#include <string>
#include <fstream>
using namespace std;
// 图书类,继承自 MediaItem
class Book : public MediaItem {
public:
wchar_t* publisher; // 出版社
wchar_t* isbn; // ISBN号
wchar_t* pages; // 页数
Book(wchar_t* id, wchar_t* title, wchar_t* author, wchar_t* rating,
wchar_t* publisher, wchar_t* isbn, wchar_t* pages)
: MediaItem(id, title, author, rating), publisher(publisher), isbn(isbn), pages(pages) {
}
void display() const override {
//ofstream outFile("C:\\Users\\ROG\\source\\repos\\sub_for_cpp\\sub_for_cpp\\record.txt");
cout << "图书编号: " << id << ", 标题: " << title << ", 作者: " << author
<< ", 评级: " << rating << ", 出版社: " << publisher
<< ", ISBN: " << isbn << ", 页数: " << (pages) << endl;
//outFile.close();
}
wstring info() const override {
wstring inf = L"";
inf += L"图书编号: ";
inf += id;
inf += L", 标题: ";
inf += title;
inf += L", 作者: ";
inf += author;
inf += L", 评级: ";
inf += rating;
inf += L", 出版社: ";
inf += publisher;
inf += L", ISBN: ";
inf += isbn;
inf += L", 页数: ";
inf += pages;
return inf;
}
};
#endif
Picture.h
#ifndef PICTURE_H
#define PICTURE_H
#include "MediaItem.h"
#include <string>
// 图画类,继承自 MediaItem
class Picture : public MediaItem {
public:
wchar_t* nationality; // 出品国籍
wchar_t* width; // 作品宽度(厘米)
wchar_t* height; // 作品高度(厘米)
Picture(wchar_t* id, wchar_t* title, wchar_t* author, wchar_t* rating,
wchar_t* nationality, wchar_t* width, wchar_t* height)
: MediaItem(id, title, author, rating), nationality(nationality), width(width), height(height) {
}
void display() const override {
cout << "图画编号: " << id << ", 标题: " << title << ", 作者: " << author
<< ", 评级: " << rating << ", 出品国籍: " << nationality
<< ", 宽度: " << width << "厘米, 高度: " << height << "厘米" << endl;
}
wstring info() const override {
wstring inf = L"";
inf += L"图画编号: ";
inf += id;
inf += L", 标题: ";
inf += title;
inf += L", 作者: ";
inf += author;
inf += L", 评级: ";
inf += rating;
inf += L", 出品国籍: ";
inf += nationality;
inf += L", 宽度: ";
inf += width;
inf += L"厘米, 高度: ";
inf += height;
inf +=L"厘米";
return inf;
}
};
#endif
Video.h
#ifndef VIDEO_H
#define VIDEO_H
#include <string>
#include "MediaItem.h"
// 视频光盘类,继承自 MediaItem
class Video : public MediaItem {
public:
wchar_t* producer; // 出品者
wchar_t* year; // 出品年份
wchar_t* duration; // 视频时长(分钟)
Video(wchar_t* id, wchar_t* title, wchar_t* author, wchar_t* rating,
wchar_t* producer, wchar_t* year, wchar_t* duration)
: MediaItem(id, title, author, rating), producer(producer), year(year), duration(duration) {
}
void display() const override {
cout << "视频光盘编号: " << id << ", 标题: " << title << ", 作者: " << author
<< ", 评级: " << rating << ", 出品者: " << producer
<< ", 出品年份: " << year << ", 时长: " << duration << "分钟" << endl;
}
wstring info() const override {
wstring inf = L"";
inf += L"视频光盘编号: ";
inf += id;
inf += L", 标题: ";
inf += title;
inf += L", 作者: ";
inf += author;
inf += L", 评级: ";
inf += rating;
inf += L", 出品者: ";
inf += producer;
inf += L", 出品年份: ";
inf += year;
inf += L", 时长: ";
inf += duration;
inf += L"分钟";
return inf;
}
};
#endif
MediaLibrary.h
#ifndef MEDIALIBRARY_H
#define MEDIALIBRARY_H
#include <vector>
#include <algorithm>
#include <iostream>
#include "Book.h"
#include "Video.h"
#include "Picture.h"
using namespace std;
class MediaLibrary {
private:
vector<MediaItem*> items; // 存储物品的容器
size_t max_capacity = 100; // 最大容量
public:
vector<MediaItem*> show() {
return items;
}
// 添加物品
bool addItem(MediaItem* item) {
if (items.size() >= max_capacity) {
cout << "物品库已满,无法添加新物品!" << endl;
return false;
}
// 检查编号是否重复
for (auto& existing_item : items) {
if (existing_item->id == item->id) {
cout << "编号重复,添加失败!" << endl;
return false;
}
}
items.push_back(item);
cout << "物品添加成功!" << endl;
return true;
}
// 根据编号查询物品
MediaItem* findById(const wchar_t* id) {
for (auto& item : items) {
if (!wcscmp(item->id,id)) {
return item;
}
}
return nullptr;
}
//根据标题查找
MediaItem* findByName(const wchar_t* name) {
for (auto& item : items) {
if (!wcscmp(item->title, name))
return item;
}
return nullptr;
}
//根据类别查找
vector<MediaItem*> findBytype(const wchar_t* num) {
vector<MediaItem*> rec;
rec.clear();
for (auto& item : items) {
if (!wcscmp(item->rating, num))
rec.push_back(item);
}
return rec;
}
// 显示所有物品
void displayAllItems() const {
if (items.empty()) {
cout << "物品库为空!" << endl;
return;
}
for (const auto& item : items) {
item->display();
}
}
// 删除物品
bool deleteItemById(const wchar_t* id) {
auto it = find_if(items.begin(), items.end(), [&id](MediaItem* item) { return !wcscmp(item->id, id); });
if (it != items.end()) {
delete* it; // 删除物品对象
items.erase(it); // 从库中移除
cout << "物品删除成功!" << endl;
return true;
}
cout << "物品编号不存在!" << endl;
return false;
}
// 统计信息
void statistics() const {
cout << "物品总数: " << items.size() << endl;
int book_count = 0, video_count = 0, picture_count = 0;
for (const auto& item : items) {
if (dynamic_cast<Book*>(item)) ++book_count;
else if (dynamic_cast<Video*>(item)) ++video_count;
else if (dynamic_cast<Picture*>(item)) ++picture_count;
}
cout << "图书数量: " << book_count << endl;
cout << "视频光盘数量: " << video_count << endl;
cout << "图画数量: " << picture_count << endl;
}
// 清空所有物品
void clear() {
for (auto& item : items) {
delete item;
}
items.clear();
}
~MediaLibrary() {
clear();
}
};
#endif
Input.hpp
class EasyTextBox
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
wchar_t* text = NULL; // 控件内容
size_t maxlen = 100; // 文本框最大内容长度
public:
void Create(int x1, int y1, int x2, int y2, int max)
{
maxlen = max;
text = new wchar_t[maxlen];
text[0] = 0;
left = x1, top = y1, right = x2, bottom = y2;
// 绘制用户界面
Show();
}
~EasyTextBox()
{
if (text != NULL)
delete[] text;
}
wchar_t* Text()
{
return text;
}
bool Check(int x, int y)
{
return (left <= x && x <= right && top <= y && y <= bottom);
}
// 绘制界面
void Show()
{
// 备份环境值
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
setlinecolor(LIGHTGRAY); // 设置画线颜色
setbkcolor(BLACK); // 设置背景颜色
setfillcolor(WHITE); // 设置填充颜色
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + 5, text);
// 恢复环境值
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void OnMessage()
{
// 备份环境值
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
setlinecolor(GREEN); // 设置画线颜色
setbkcolor(BLACK); // 设置背景颜色
setfillcolor(BLACK); // 设置填充颜色
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + 5, text);
int width = textwidth(text); // 字符串总宽度
int counter = 0; // 光标闪烁计数器
bool binput = true; // 是否输入中
ExMessage msg;
while (binput)
{
while (binput && peekmessage(&msg, EX_MOUSE | EX_CHAR, false)) // 获取消息,但不从消息队列拿出
{
if (msg.message == WM_LBUTTONDOWN)
{
// 如果鼠标点击文本框外面,结束文本输入
if (msg.x < left || msg.x > right || msg.y < top || msg.y > bottom)
{
binput = false;
break;
}
}
else if (msg.message == WM_CHAR)
{
size_t len = wcslen(text);
switch (msg.ch)
{
case '\b': // 用户按退格键,删掉一个字符
if (len > 0)
{
text[len - 1] = 0;
width = textwidth(text);
counter = 0;
clearrectangle(left + 10 + width, top + 1, right - 1, bottom - 1);
}
break;
case '\r': // 用户按回车键,结束文本输入
case '\n':
binput = false;
break;
default: // 用户按其它键,接受文本输入
if (len < maxlen - 1)
{
text[len++] = msg.ch;
text[len] = 0;
clearrectangle(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); // 清除画的光标
width = textwidth(text); // 重新计算文本框宽度
counter = 0;
outtextxy(left + 10, top + 5, text); // 输出新的字符串
}
}
}
peekmessage(NULL, EX_MOUSE | EX_CHAR); // 从消息队列抛弃刚刚处理过的一个消息
}
// 绘制光标(光标闪烁周期为 20ms * 32)
counter = (counter + 1) % 32;
if (counter < 16)
line(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); // 画光标
else
clearrectangle(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); // 擦光标
// 延时 20ms
Sleep(20);
}
clearrectangle(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); // 擦光标
// 恢复环境值
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
Show();
}
};
// 实现按钮控件
class EasyButton
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
wchar_t* text = NULL; // 控件内容
void (*userfunc)() = NULL; // 控件消息
public:
void Create(int x1, int y1, int x2, int y2, const wchar_t* title, void (*func)())
{
text = new wchar_t[wcslen(title) + 1];
wcscpy_s(text, wcslen(title) + 1, title);
left = x1, top = y1, right = x2, bottom = y2;
userfunc = func;
// 绘制用户界面
Show();
}
~EasyButton()
{
if (text != NULL)
delete[] text;
}
bool Check(int x, int y)
{
return (left <= x && x <= right && top <= y && y <= bottom);
}
// 绘制界面
void Show()
{
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
setlinecolor(BLACK); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
setfillcolor(WHITE); // 设置填充颜色
fillrectangle(left, top, right, bottom);
outtextxy(left + (right - left - textwidth(text) + 1) / 2, top + (bottom - top - textheight(text) + 1) / 2, text);
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void OnMessage()
{
if (userfunc != NULL)
userfunc();
}
};
main.cpp
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#include <graphics.h>
#include <iostream>
#include <string>
#include <conio.h>
#include <windows.h>
#include <string.h>
#include <locale>
#include "MediaLibrary.h"
#include "Book.h"
#include "Video.h"
#include "Picture.h"
#include "Input.hpp"
#include <fstream>
using namespace std;
bool flag = 0;
EasyTextBox id_;
EasyTextBox author_;
EasyTextBox publisher_;
EasyTextBox isbn_;
EasyTextBox title_;
EasyTextBox pages_;
EasyTextBox rate_;
EasyButton btnOK_;
EasyTextBox okk;
void drawMenu();
//绘制光标
void drawCursor(int x, int y, bool show_) {
if (show_) {
setcolor(WHITE); // 光标颜色
line(x, y, x, y + 20); // 绘制光标
}
else {
setcolor(BLACK); // 背景颜色,用于擦除光标
line(x, y, x, y + 20); // 擦除光标
setcolor(WHITE);
}
}
// 按钮绘制函数
void drawButton(int x, int y, int width, int height, const wchar_t* text) {
setfillcolor(LIGHTBLUE);
solidrectangle(x, y, x + width, y + height); // 绘制按钮
settextcolor(WHITE);
outtextxy(x + (width - textwidth(text)) / 2, y + (height - textheight(text)) / 2, text); // 按钮文字居中
}
// 检测按钮点击
bool isButtonClicked(int x, int y, int width, int height, int mouseX, int mouseY) {
return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
}
void ok()
{
//okk.Create(700, 500, 800, 600, 10);
outtextxy(700, 500, L"ok");
}
void okkk()
{
okk.Create(700, 500, 800, 600, 10);
//outtextxy(700, 500, L"ok");
}
// 添加图书
Book* getBookInfo() {
cleardevice();
const wchar_t *con = L"OK";
ok();
okkk();
outtextxy(50, 50, L"请输入图书信息:");
outtextxy(50, 100, L"编号:");
id_.Create(150, 100, 400, 140, 10);
outtextxy(50, 150, L"标题:");
title_.Create(150, 150, 400, 190, 10);
outtextxy(50, 200, L"作者:");
author_.Create(150, 200, 400, 240, 10);
outtextxy(50, 250, L"出版社:");
publisher_.Create(150, 250, 400, 290, 10);
outtextxy(50, 300, L"ISBN:");
isbn_.Create(150, 300, 400, 340, 10);
outtextxy(50, 350, L"页数:");
pages_.Create(150, 350, 400, 390, 10);
outtextxy(50, 400, L"评级:1.未定义 2.一般 3.成人 4.儿童");
rate_.Create(150, 400, 400, 440, 10);
ok();
ExMessage msg;
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
// 判断控件
if (id_.Check(msg.x, msg.y)) id_.OnMessage();
if (author_.Check(msg.x, msg.y)) author_.OnMessage();
if (title_.Check(msg.x, msg.y)) title_.OnMessage();
if (publisher_.Check(msg.x, msg.y)) publisher_.OnMessage();
if (isbn_.Check(msg.x, msg.y)) isbn_.OnMessage();
if (pages_.Check(msg.x, msg.y)) pages_.OnMessage();
if (rate_.Check(msg.x, msg.y)) rate_.OnMessage();
if (okk.Check(msg.x, msg.y)) break;
}
}
wchar_t* title = title_.Text();
wchar_t* author = author_.Text();
wchar_t* pages__ = pages_.Text();
wchar_t* pages = pages_.Text();
wchar_t* id = id_.Text();
wchar_t* publisher = publisher_.Text();
wchar_t* isbn = isbn_.Text();
wchar_t* rate = rate_.Text();
return new Book(id, title, author, rate, publisher, isbn, pages);
}
const wchar_t* con = L"OK";
// 添加视频光盘
Video* getVideoInfo() {
cleardevice();
btnOK_.Create(400, 600, 420, 630, con, NULL);
outtextxy(50, 50, L"请输入视频光盘信息:");
outtextxy(50, 100, L"编号:");
id_.Create(150, 100, 400, 140, 10);
outtextxy(50, 150, L"标题:");
title_.Create(150, 150, 400, 190, 10);
outtextxy(50, 200, L"作者:");
author_.Create(150, 200, 400, 240, 10);
outtextxy(50, 250, L"出品方:");
publisher_.Create(150, 250, 400, 290, 10);
outtextxy(50, 300, L"出品年份:");
isbn_.Create(150, 300, 400, 340, 10);
outtextxy(50, 350, L"时长(分钟):");
pages_.Create(150, 350, 400, 390, 10);
outtextxy(50, 400, L"评级:1.未定义 2.一般 3.成人 4.儿童");
rate_.Create(150, 400, 400, 440, 10);
ok();
okkk();
ExMessage msg;
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
// 判断控件
if (id_.Check(msg.x, msg.y)) id_.OnMessage();
if (author_.Check(msg.x, msg.y)) author_.OnMessage();
if (title_.Check(msg.x, msg.y)) title_.OnMessage();
if (publisher_.Check(msg.x, msg.y)) publisher_.OnMessage();
if (isbn_.Check(msg.x, msg.y)) isbn_.OnMessage();
if (pages_.Check(msg.x, msg.y)) pages_.OnMessage();
if (rate_.Check(msg.x, msg.y)) rate_.OnMessage();
if (okk.Check(msg.x, msg.y)) break;
//if (backback(m.x)) break;
}
}
wchar_t* title = title_.Text();
wchar_t* author = author_.Text();
wchar_t* pages__ = pages_.Text();
wchar_t* pages = pages_.Text();
wchar_t* id = id_.Text();
wchar_t* publisher = publisher_.Text();
wchar_t* isbn = isbn_.Text();
wchar_t* rate = rate_.Text();
return new Video(id, title, author, rate, publisher, isbn, pages);
}
// 添加图画
Picture* getPictureInfo() {
cleardevice();
const wchar_t* con = L"OK";
btnOK_.Create(400, 600, 420, 630, con, NULL);
outtextxy(50, 50, L"请输入图画信息:");
outtextxy(50, 100, L"编号:");
id_.Create(150, 100, 400, 140, 10);
outtextxy(50, 150, L"标题:");
title_.Create(150, 150, 400, 190, 10);
outtextxy(50, 200, L"作者:");
author_.Create(150, 200, 400, 240, 10);
outtextxy(50, 250, L"国籍:");
publisher_.Create(150, 250, 400, 290, 10);
outtextxy(50, 300, L"宽度(cm):");
isbn_.Create(150, 300, 400, 340, 10);
outtextxy(50, 350, L"长度(cm):");
pages_.Create(150, 350, 400, 390, 10);
outtextxy(50, 400, L"评级:1.未定义 2.一般 3.成人 4.儿童");
rate_.Create(150, 400, 400, 440, 10);
okk.Create(700, 500, 800, 600, 10);
outtextxy(700, 500, L"ok");
ExMessage msg;
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
// 判断控件
if (id_.Check(msg.x, msg.y)) id_.OnMessage();
if (author_.Check(msg.x, msg.y)) author_.OnMessage();
if (title_.Check(msg.x, msg.y)) title_.OnMessage();
if (publisher_.Check(msg.x, msg.y)) publisher_.OnMessage();
if (isbn_.Check(msg.x, msg.y)) isbn_.OnMessage();
if (pages_.Check(msg.x, msg.y)) pages_.OnMessage();
if (rate_.Check(msg.x, msg.y)) rate_.OnMessage();
if (okk.Check(msg.x, msg.y)) break;
if (btnOK_.Check(msg.x, msg.y)) break;
}
}
wchar_t* title = title_.Text();
wchar_t* author = author_.Text();
wchar_t* pages__ = pages_.Text();
wchar_t* pages = pages_.Text();
wchar_t* id = id_.Text();
wchar_t* publisher = publisher_.Text();
wchar_t* isbn = isbn_.Text();
wchar_t* rate = rate_.Text();
return new Picture(id, title, author, rate, publisher, isbn, pages);
}
bool turn_to_main() {
ok();
okkk();
ExMessage msg;
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
if (okk.Check(msg.x, msg.y)) return true;
}
}
}
// 添加物品功能界面
void addItem(MediaLibrary& library) {
cleardevice();
outtextxy(50, 50, L"请选择物品类别:");
drawButton(50, 100, 200, 40, L"1. 图书");
drawButton(50, 160, 200, 40, L"2. 视频光盘");
drawButton(50, 220, 200, 40, L"3. 图画");
while (true) {
if (MouseHit()) {
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN) {
int mouseX = msg.x;
int mouseY = msg.y;
if (isButtonClicked(50, 100, 200, 40, mouseX, mouseY)) { // 添加图书
Book* newBook = getBookInfo();
library.addItem(newBook);
break;
}
else if (isButtonClicked(50, 160, 200, 40, mouseX, mouseY)) { // 添加视频光盘
Video* newVideo = getVideoInfo();
library.addItem(newVideo);
break;
}
else if (isButtonClicked(50, 220, 200, 40, mouseX, mouseY)) { // 添加图画
Picture* newPicture = getPictureInfo();
library.addItem(newPicture);
break;
}
}
}
}
}
void selectItem(MediaLibrary& library) {
cleardevice();
outtextxy(50, 50, L"请选择查询方式:");
outtextxy(50, 100, L"1. 按编号查询");
outtextxy(50, 150, L"2. 按标题查询");
outtextxy(50, 200, L"3. 按类别查询");
while (true) {
if (MouseHit()) {
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN) {
int mouseX = msg.x;
int mouseY = msg.y;
if (isButtonClicked(50, 100, 200, 40, mouseX, mouseY)) { // 按编号查询
cleardevice();
outtextxy(50, 100, L"编号:");
id_.Create(150, 100, 400, 140, 10);
ExMessage msg;
btnOK_.Create(400, 600, 420, 630, con, NULL);
ok();
okkk();
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
// 判断控件
if (id_.Check(msg.x, msg.y)) id_.OnMessage();
if (okk.Check(msg.x, msg.y)) break;
//if (btnOK_.Check(msg.x, msg.y)) break;
}
}
wchar_t* id = id_.Text();
MediaItem* item = library.findById(id);
if (item) {
wstring filecontent;
char p[100] = {};
filecontent = item->info();
int len = filecontent.size();
*(p + len) = '\0';
outtextxy(50, 200, filecontent.c_str());
}
else outtextxy(50, 200, L"该编号不存在!");
if (turn_to_main()) break;
if (getchar())
break;
}
else if (isButtonClicked(50, 150, 200, 40, mouseX, mouseY)) { // 按标题查询
cleardevice();
outtextxy(50, 100, L"标题:");
title_.Create(150, 100, 400, 140, 10);
ExMessage msg;
ok();
okkk();
btnOK_.Create(400, 600, 420, 630, con, NULL);
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
// 判断控件
if (title_.Check(msg.x, msg.y)) title_.OnMessage();
if (btnOK_.Check(msg.x, msg.y)) break;
if (okk.Check(msg.x, msg.y)) break;
//if (turn_to_main()) break;
}
}
wchar_t* title = title_.Text();
MediaItem* item = library.findByName(title);
if (item) {
wstring filecontent;
char p[100] = {};
filecontent = item->info();
int len = filecontent.size();
//filecontent.copy(p, len, 0);
//*(p + len) = '\0';
outtextxy(50, 200, filecontent.c_str());
}
else outtextxy(50, 200, L"该标题不存在!");
if (turn_to_main()) break;
if (getchar())
break;
}
else if (isButtonClicked(50, 200, 200, 40, mouseX, mouseY)) { // 按类别查询
cleardevice();
outtextxy(50, 100, L"类别:");
rate_.Create(150, 100, 400, 140, 10);
ok();
okkk();
ExMessage msg;
btnOK_.Create(400, 600, 420, 630, con, NULL);
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
// 判断控件
if (rate_.Check(msg.x, msg.y)) rate_.OnMessage();
if (btnOK_.Check(msg.x, msg.y)) break;
if (okk.Check(msg.x, msg.y)) break;
}
}
wchar_t* rate = rate_.Text();
vector<MediaItem*> item = library.findBytype(rate);
int cnt = 200;
if (!item.empty()) {
for (auto& i : item) {
wstring filecontent;
char p[100] = {};
filecontent = i->info();
int len = filecontent.size();
//filecontent.copy(p, len, 0);
//*(p + len) = '\0';
outtextxy(50, cnt, filecontent.c_str());
cnt += 30;
}
}
else outtextxy(50, 200, L"该类别不存在!");
if (turn_to_main()) break;
if (getchar())
break;
}
}
}
}
}
void show_info(MediaLibrary& library) {
vector<MediaItem*> elem = library.show();
int cnt = 100;
cleardevice();
while (true)
{
for (auto& i : elem) {
wstring filecontent;
char p[100] = {};
filecontent = i->info();
int len = filecontent.size();
//filecontent.copy(p, len, 0);
//*(p + len) = '\0';
outtextxy(50, cnt, filecontent.c_str());
cnt += 30;
}
if (elem.empty())
outtextxy(100, 200, L"目录内没有内容");
if (turn_to_main()) break;
if (getchar())
break;
}
}
void dele(MediaLibrary& library) {
cleardevice();
ok();
okkk();
while (true)
{
outtextxy(50, 100,L"编号:");
id_.Create(150, 100, 400, 140, 10);
wchar_t* id = id_.Text();
ExMessage msg;
btnOK_.Create(400, 600, 420, 630, con, NULL);
while (true)
{
msg = getmessage(EX_MOUSE); // 获取消息输入
if (msg.message == WM_LBUTTONDOWN)
{
// 判断控件
if (id_.Check(msg.x, msg.y)) id_.OnMessage();
if (okk.Check(msg.x, msg.y)) break;
}
}
MediaItem* item = library.findById(id);
if (item)
{
library.deleteItemById(id);
outtextxy(50, 200, L"删除成功");
}
else
outtextxy(50, 200, L"找不到此物品");
if (turn_to_main()) break;
if (getchar())
break;
}
}
void count(MediaLibrary& library) {
cleardevice();
outtextxy(50, 50, L"请选择物品类别:");
drawButton(50, 100, 200, 40, L"1. 图书");
drawButton(50, 160, 200, 40, L"2. 视频光盘");
drawButton(50, 220, 200, 40, L"3. 图画");
while (true) {
vector<MediaItem*> item1 = library.show();
vector<MediaItem*> record;
record.clear();
if (MouseHit()) {
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN) {
int mouseX = msg.x;
int mouseY = msg.y;
if (isButtonClicked(50, 100, 200, 40, mouseX, mouseY)) { // 图书
for (auto& i : item1) {
if (dynamic_cast<Book*>(i))
record.push_back(i);
}
}
else if (isButtonClicked(50, 160, 200, 40, mouseX, mouseY)) { // 视频光盘
for (auto& i : item1) {
if (dynamic_cast<Video*>(i))
record.push_back(i);
}
}
else if (isButtonClicked(50, 220, 200, 40, mouseX, mouseY)) { // 图画
for (auto& i : item1) {
if (dynamic_cast<Picture*>(i))
record.push_back(i);
}
}
cleardevice();
int tt = 100;
for (auto& i : record) {
wstring filecontent;
char p[100] = {};
filecontent = i->info();
int len = filecontent.size();
//filecontent.copy(p, len, 0);
*(p + len) = '\0';
outtextxy(50, tt, filecontent.c_str());
tt += 30;
}
if (record.size() == 0)
outtextxy(100, 200, L"没有此类别");
if (turn_to_main()) break;
if (getchar())
break;
}
}
}
}
// 主菜单界面绘制
void drawMenu() {
cleardevice();
settextcolor(WHITE);
outtextxy(50, 20, L"请选择操作:");
drawButton(50, 60, 200, 40, L"1. 添加物品");
drawButton(50, 120, 200, 40, L"2. 查询物品");
drawButton(50, 180, 200, 40, L"3. 显示所有物品");
drawButton(50, 240, 200, 40, L"4. 删除物品");
drawButton(50, 300, 200, 40, L"5. 统计信息");
drawButton(50, 360, 200, 40, L"6. 退出");
}
// 主程序
int main() {
initgraph(800, 600); // 初始化图形窗口
MediaLibrary library; // 创建物品库
wchar_t s0[][100] = { L"101",L"102",L"103" };
wchar_t s1[][100] = { L"小王子",L"百年孤独",L"解忧杂货店" };
wchar_t s2[][100] = { L"埃克苏佩里",L"加西亚·马尔克斯",L"东野圭吾" };
wchar_t s3[][100] = { L"儿童",L"成人",L"一般" };
wchar_t s4[][100] = { L"哈尔滨出版社",L"人民文学出版社",L"南海出版公司" };
wchar_t s5[][100] = { L"978-7-20107-764-2",L"978-7-02010-420-9",L"978-7-5442-6919-9" };
wchar_t s6[][100] = { L"192",L"360",L"293" };
for (int i = 0; i < 3; i++) {
library.addItem(new Book(s0[i], s1[i], s2[i], s3[i], s4[i], s5[i], s6[i]));
}
//library.addItem(book1);
while (true) {
drawMenu(); // 绘制主菜单
while (true) {
if (MouseHit()) { // 检测鼠标事件
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN) { // 鼠标左键点击
int mouseX = msg.x;
int mouseY = msg.y;
if (isButtonClicked(50, 60, 200, 40, mouseX, mouseY)) { // 添加物品
addItem(library);
break;
}
else if (isButtonClicked(50, 120, 200, 40, mouseX, mouseY)) { //查询物品
selectItem(library);
break;
}
else if (isButtonClicked(50, 180, 200, 40, mouseX, mouseY)) { //显示所有物品
show_info(library);
break;
}
else if (isButtonClicked(50, 240, 200, 40, mouseX, mouseY)) { //删除
dele(library);
break;
}
else if (isButtonClicked(50, 300, 200, 40, mouseX, mouseY)) { //统计
count(library);
break;
}
else if (isButtonClicked(50, 360, 200, 40, mouseX, mouseY)) { // 退出程序
closegraph();
return 0;
}
}
}
}
}
closegraph(); // 关闭图形窗口
return 0;
}