C++控制台界面库_实例6: 电话簿

代码如下:

// PhoneBook.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//

#include
#include “console.h”
#pragma comment(lib,“console.lib”)
#include

int circulation = 1;
int interface_num = 0;

void MainInterface(); // 0
void Add(); // 1
void Delete(); // 2
void Find(); // 3
void Modify(); // 4

char phone_book[] = “dianhuabu.txt”;

struct Dhb
{
char name[10], telephone[20];
};

extern COORD mouse;

int main()
{
Screen screen;
screen.ShowOrHideCursor(0);

Event event;
event.AddMouseMode();

while (circulation)
{
	screen.Clear_screen();

	switch (interface_num)
	{
	case 0:
		MainInterface();
		break;
	case 1:
		Add();
		break;
	case 2:
		Delete();
		break;
	case 3:
		Find();
		break;
	case 4:
		Modify();
		break;
	}
}

event.RestoreMode();
//screen.Pause();
screen.Close_handle();
return 0;

}

void MainInterface()
{
Screen screen;
screen.SetSize(70, 29);
screen.SetTitle(“电话簿”);

if (_access(phone_book, 0) == -1)
{
	FILE* dhb;
	fopen_s(&dhb, phone_book, "a");
	fclose(dhb);
}

screen.text_color_num = 6;
screen.DisplayText(30, 2, "电话簿管理");

Listbox listbox = Listbox(24, 5, 20, 8);
listbox.Attribute(3, 13, 13, true, false, 0);
char item[][100] = { "        添加","        删除","        查找","        修改" };
int item_num = 4;
listbox.AddArrayto_SinglePageListbox(item, item_num);

Form form;
form.Attribute(listbox.listbox_textcolor_num, listbox.listbox_textBgcolor_num);
form.DrawSeparatorBar_H(11, listbox.cx0 - 2, listbox.cx0 + listbox.wide + 1);

Button EXIT_button = Button(listbox.cx0, 12, "      退出程序      ");
EXIT_button.button_color_num = 3;
EXIT_button.button_Bgcolor_num = 13;
EXIT_button.Display_button();

Event event;

int index = 0;

while (1)
{
	event.ReadEvent();
	if (event.Mouse_Event())
	{
		if (EXIT_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			circulation = 0;
			return;
		}

		if (EXIT_button.MousePosition_At_button())
		{
			EXIT_button.PaintText_button(2, 13);
		}
		else
		{
			EXIT_button.PaintText_button(3, 13);
		}

		if (listbox.MousePosition_At_listbox())
		{
			listbox.index_textbox.PaintText_textbox(3, 13);
			event.Get_mouse_position();
			listbox.index_textbox.cy0 = mouse.Y;
			listbox.index_textbox.PaintText_textbox(1, 6);

			if (event.Left_Button_Pressed())
			{
				index = listbox.SinglePageItemIndex();
				interface_num = index + 1;
				return;
			}
		}
		else	listbox.index_textbox.PaintText_textbox(3, 13);
	}

	if (event.Key_Event() && event.Key_Down())
	{
		char ch = event.Get_ch();
		WORD vk = event.Get_vk();

		if (ch == 'q' || vk == VK_ESCAPE)
		{
			circulation = 0;
			return;
		}
	}
}

}

void Add()
{
Screen screen;
screen.SetTitle(“添加”);
screen.Paint_screen(14);

Button RETURN_button = Button(30, 17, "返回");
RETURN_button.Attribute(4, 14);
RETURN_button.Display_button();

screen.TextAttribute(0, 14);
screen.DisplayText(10, 6, "姓名");
screen.DisplayText(10, 8, "电话");

Textbox Name_textbox = Textbox(16, 6, 20);
Name_textbox.Attribute(2, 1, 1);
Textbox Telephone_textbox = Textbox(16, 8, 20);
Telephone_textbox.Attribute(2, 1, 1);

char* name = NULL, * telephone = NULL;

Event event;

Button OK_button = Button(15, 17, "添加");
OK_button.Attribute(4, 14);
OK_button.Display_button();

while (1)
{
	event.ReadEvent();
	if (event.Mouse_Event())
	{
		if (RETURN_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			interface_num = 0;
			return;
		}

		if (Name_textbox.MousePosition_At_textbox()
			&& event.Left_Button_Pressed())
		{
			name = Name_textbox.InputString(true);
		}

		if (Telephone_textbox.MousePosition_At_textbox()
			&& event.Left_Button_Pressed())
		{
			telephone = Telephone_textbox.InputString(true);
		}

		if (OK_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			if (Name_textbox.Non_empty && Telephone_textbox.Non_empty)
			{
				FILE* dhb;
				fopen_s(&dhb, "dianhuabu.txt", "a");
				fprintf(dhb, "%-10s %-20s\n", name, telephone);
				fclose(dhb);

				Name_textbox.Clear_textbox();
				Telephone_textbox.Clear_textbox();
			}
		}
	}
}

}

void Delete()
{
Screen screen;
screen.SetTitle(“删除”);
screen.Paint_screen(14);

Button RETURN_button = Button(30, 22, "返回");
RETURN_button.button_color_num = 2;
RETURN_button.button_Bgcolor_num = 14;
RETURN_button.Display_button();

Button OK_button = Button(10, 22, "删除");
OK_button.button_color_num = 2;
OK_button.button_Bgcolor_num = 14;
OK_button.Display_button();

screen.TextAttribute(0, 14);
//screen.DisplayText(20,1,"删除");
screen.DisplayText(10, 3, "姓名       电话");

File file;
int lines = file.TotalLines("dianhuabu.txt");

char dhb1[50][100];

FILE* fp;
fopen_s(&fp, "dianhuabu.txt", "r");
for (int i = 0; i < lines; i++)
{
	fgets(dhb1[i], 100, fp);
	dhb1[i][strlen(dhb1[i]) - 1] = '\0';
}
fclose(fp);

Listbox listbox = Listbox(10, 5, 40, 10);
listbox.Attribute1(true, false, 0);
listbox.AddArrayto_MultiPageListbox(dhb1, lines);
listbox.MultiPageButtonPageDown.button_color_num = 0;
listbox.MultiPageButtonPageDown.button_Bgcolor_num = 14;
listbox.MultiPageButtonPageDown.Display_button();
listbox.MultiPageButtonPageUp.button_color_num = 0;
listbox.MultiPageButtonPageUp.button_Bgcolor_num = 14;
listbox.MultiPageButtonPageUp.Display_button();

int index;
bool selection = false;

Event event;

while (1)
{
	event.ReadEvent();
	if (event.Mouse_Event())
	{
		if (RETURN_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			interface_num = 0;
			return;
		}

		if (listbox.MultiPageButtonPageDown.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			listbox.MultiPageListboxPageTurn_Array(DOWN, dhb1, lines);
		}

		if (listbox.MultiPageButtonPageUp.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			listbox.MultiPageListboxPageTurn_Array(UP, dhb1, lines);
		}

		if (listbox.MousePosition_At_listbox()
			&& event.Left_Button_Pressed())
		{
			index = listbox.MultiPageItemIndex();

			listbox.index_textbox.PaintText_textbox(listbox.listbox_textcolor_num, listbox.listbox_textBgcolor_num);
			event.Get_mouse_position();
			listbox.index_textbox.cy0 = mouse.Y;
			// listbox.index_textbox.wide=(int)strlen(dhb1[index]);
			listbox.index_textbox.PaintText_textbox(5, 4);
			selection = true;
		}

		if (OK_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			if (lines != 0)
			{
				if (selection)
				{
					HWND cmd = GetConsoleWindow();
					if (MessageBox(cmd, TEXT("Are you sure to delete?"), TEXT("删除选中项"), MB_YESNO | MB_ICONQUESTION) == IDYES)
					{
						for (int i = index; i < lines - 1; i++)
						{
							strcpy_s(dhb1[i], 100, dhb1[i + 1]);
						}
						lines--;
						listbox.AddArrayto_MultiPageListbox(dhb1, lines);

						selection = false;

						fopen_s(&fp, phone_book, "w");
						for (int i = 0; i < lines; i++)
						{
							fprintf(fp, "%s\n", dhb1[i]);
						}
						fclose(fp);
					}
				}

				event.FlushInputBuffer();
			}
		}
	}
}

}

void Find()
{
Screen screen;
screen.SetTitle(“查找”);
screen.Paint_screen(14);

Button RETURN_button = Button(54, 21, "返回");
RETURN_button.Display_button();

screen.TextAttribute(0, 14);
//screen.DisplayText(20,1,"查找");
screen.DisplayText(10, 3, "姓名       电话");

File file;
int lines = file.TotalLines(phone_book);

Dhb dhb[50];
char dhb1[50][100];

FILE* fp;
fopen_s(&fp, phone_book, "r");
for (int i = 0; i < lines; i++)
{
	fgets(dhb1[i], 100, fp);
	dhb1[i][strlen(dhb1[i]) - 1] = '\0';  // 去掉换行符
	sscanf_s(dhb1[i], "%s %s", dhb[i].name, 10, dhb[i].telephone, 20);
}
fclose(fp);

Listbox listbox = Listbox(10, 5, 40, 10);
listbox.Attribute2(true, false, 0);
listbox.AddArrayto_ScrollListbox(dhb1, lines);
listbox.ScrollButtonDown.Display_button();
listbox.ScrollButtonUp.Display_button();

Window Result_window = Window(8, 19, 44, 3);
Result_window.Attribute(0, 1, 1, false, false, 0);

char* wangli = NULL;

Button Mode_button[2] = { Button(8,17,"[ ]按姓名"),Button(19,17,"[ ]按电话") };
for (int i = 0; i < 2; i++)Mode_button[i].Display_button();
Mode_button[0].FillCharacter_color_num = 2;
Mode_button[1].FillCharacter_color_num = 3;
int mode = 0;
Mode_button[mode].FillCharacter('*');

Textbox Search_textbox = Textbox(32, 17, 20);
Search_textbox.Attribute(2, 1, 1);
Button SEARCH_button = Button(54, 17, "搜索");
SEARCH_button.Display_button();

Event event;

while (1)
{
	event.ReadEvent();
	if (event.Mouse_Event())
	{
		if (RETURN_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			interface_num = 0;
			return;
		}

		if (listbox.ScrollButtonDown.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			listbox.ScrollListboxText_array(DOWN, dhb1, lines);
		}

		if (listbox.ScrollButtonUp.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			listbox.ScrollListboxText_array(UP, dhb1, lines);
		}

		for (int i = 0; i < 2; i++)
		{
			if (Mode_button[i].MousePosition_At_button() && event.Left_Button_Pressed())
			{
				Mode_button[mode].FillCharacter(' ');
				mode = i;
				Mode_button[mode].FillCharacter('*');
			}
		}

		if (Search_textbox.MousePosition_At_textbox()
			&& event.Left_Button_Pressed())
		{
			wangli = Search_textbox.InputString(true);
		}

		if (SEARCH_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			if (Search_textbox.Non_empty)
			{
				Result_window.Clear_window();

				switch (mode)
				{
				case 0:
				{
					int y = 0;

					for (int i = 0; i < lines; i++)
					{
						if (strcmp(wangli, dhb[i].name) == 0)
						{
							Result_window.DisplayString_window(0, y++, dhb1[i]);
						}
					}
				}
				break;

				case 1:
					for (int i = 0; i < lines; i++)
					{
						if (strcmp(wangli, dhb[i].telephone) == 0)
						{
							Result_window.DisplayString_window(0, 0, dhb1[i]);
						}
					}
					break;
				}
			}
		}
	}
}

}

void Modify()
{
Screen screen;
screen.SetTitle(“修改”);
screen.Paint_screen(14);

screen.TextAttribute(0, 14);
screen.DisplayText(10, 3, "姓名       电话");

Button RETURN_button = Button(25, 22, "返回");
RETURN_button.Display_button();

Button OK_button = Button(8, 22, "确定");
OK_button.Display_button();

File file;
int lines = file.TotalLines(phone_book);

char dhb1[50][100];

FILE* fp;
fopen_s(&fp, phone_book, "r");
for (int i = 0; i < lines; i++)
{
	fgets(dhb1[i], 100, fp);
	dhb1[i][strlen(dhb1[i]) - 1] = '\0';
}
fclose(fp);

Listbox listbox = Listbox(10, 5, 40, 10);
listbox.Attribute(0, 1, 1, true, false, 0);
listbox.AddArrayto_ScrollListbox(dhb1, lines);
listbox.ScrollButtonDown.Display_button();
listbox.ScrollButtonUp.Display_button();

Textbox Name_textbox = Textbox(8, 19, 12);
Name_textbox.Attribute(2, 1, 1);
Textbox Telephone_textbox = Textbox(25, 19, 27);
Telephone_textbox.Attribute(2, 1, 1);

int index;
bool selection = false;
char* name = NULL, * telephone = NULL;

Event event;

while (1)
{
	event.ReadEvent();
	if (event.Mouse_Event())
	{
		if (RETURN_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			interface_num = 0;
			return;
		}

		if (listbox.ScrollButtonDown.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			listbox.ScrollListboxText_array(DOWN, dhb1, lines);
		}

		if (listbox.ScrollButtonUp.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			listbox.ScrollListboxText_array(UP, dhb1, lines);
		}

		if (listbox.MousePosition_At_listbox()
			&& event.Left_Button_Pressed())
		{
			index = listbox.ScrollItemIndex();
			listbox.index_textbox.PaintText_textbox(listbox.listbox_textcolor_num, listbox.listbox_textBgcolor_num);
			event.Get_mouse_position();
			listbox.index_textbox.cy0 = mouse.Y;
			// listbox.index_textbox.wide=(int)strlen(dhb1[index]);
			listbox.index_textbox.PaintText_textbox(listbox.listbox_textBgcolor_num, listbox.listbox_textcolor_num);
			selection = true;
		}

		if (Name_textbox.MousePosition_At_textbox()
			&& event.Left_Button_Pressed())
		{
			name = Name_textbox.InputString(true);
		}

		if (Telephone_textbox.MousePosition_At_textbox()
			&& event.Left_Button_Pressed())
		{
			telephone = Telephone_textbox.InputString(true);
		}

		if (OK_button.MousePosition_At_button()
			&& event.Left_Button_Pressed())
		{
			if (Name_textbox.Non_empty && Telephone_textbox.Non_empty && selection)
			{
				sprintf_s(dhb1[index], 100, "%-10s %-20s", name, telephone);

				listbox.AddArrayto_ScrollListbox(dhb1, lines);
				Name_textbox.Clear_textbox();
				Telephone_textbox.Clear_textbox();
				selection = false;

				errno_t ss = fopen_s(&fp, phone_book, "w");
				for (int i = 0; i < lines; i++)
				{
					fprintf(fp, "%s\n", dhb1[i]);
				}
				fclose(fp);
			}
		}
	}
}

}

运行效果:

C++控制台界面库_实例6: 电话簿_第1张图片

你可能感兴趣的:(C++控制台界面库_实例6: 电话簿)