• 【一套C语言控制台的输出框代码】


    效果演示

    可以生成一个输出框

    首先

    要创建输出框,设置输出框风格,可以设置的元素包括:

    左上角符号,右上角符号,左下角符号,右下角符号,以及上下左右边界线符号,理论上,只要你电脑能显示的符号,都可以支持

    该套代码在:

    宽字符下工作

    再次

    你需要加载字符串,此函数可以多次调用,可以向输出框中添加内容

    之后

    show输出框 此时完成自动分页,外边框也将显示出来

    最后

    可调用函数输出指定页的内容

    所有文字不会越出输出框,自动换行。

    以下是代码

    cwguser.h

    #include<tchar.h>
    #include<string.h>
    #include <locale>
    #include <conio.h>
    #ifndef _CWGUSER_H_
    #define _CWGUSER_H_
    #include "cwgtype.h"
    #include "output.h"
    
    
    #endif

    cwgtype.h

    1 #ifndef _CWG_TYPE_H_
    2 #define _CWG_TYPE_H_
    3 typedef int CWG_COLOR;
    4 typedef int CWG_HANDLE;
    5 
    6 #endif

    output.h

    #ifndef _CWG_OUTPUT_H_
    #define _CWG_OUTPUT_H_
    #include<tchar.h>
    #include "cwgtype.h"
    #include <malloc.h>
    #define SC(x) L##x
    typedef struct _outputbox
    {
        int xbox;
        int ybox;                //起始坐标
        int xWidth;
        int yHeight;            //范围
        CWG_COLOR box_color;    //边框颜色
    
        TCHAR* pBoxUpLineCh;
        int    nBoxUpLineCh;
        TCHAR* pBoxDownLineCh;
        int    nBoxDownLineCh;
        TCHAR* pBoxLeftLineCh;
        int    nBoxLeftLineCh;
        TCHAR* pBoxRightLineCh;    //边线符号
        int    nBoxRightLineCh; //符号占位 
        TCHAR* pLeUpCh;
        int    nLeUpCh;
        TCHAR* pRiUpCh;
        int    nRiUpCh;
        TCHAR* pLeDoCh;
        int    nLeDoCh;
        TCHAR* pRiDoCh;            //四个角的符号
        int    nRiDoCh;
    }OUTPUTBOX;
    //移动光标
    void gotoxy(int x, int y);
    //输出相关
    //----------------------------------------------------------------------------------------------------------
    //功能:
    //创建一个用于展示字符串的区域 (这是逻辑的 不显示)
    //参数:
    //输出框结构-包括输出框大小,位置,颜色(颜色宏),边框字符数组(8个字符构成,分别是上边框,下边框,左边框,右边框,左上角,右上角,左下角,右下角)
    //          -不被赋值的成员将会采用DEFAULTSET中的设置.
    //字符串指针-输出框的标题,输入NULL表明无标题,最大长度为10个汉字,超出则返回 OE_CAPTIONERROR
    //内容大小  -表示要输出的最大字节数,如果输入的内容多于一个输出框的最大容量,输出框会自动增加翻页功能.
    //返回值 输出框句柄
    //----------------------------------------------------------------------------------------------------------
    CWG_HANDLE OutPutBox(OUTPUTBOX outputbox, const TCHAR * caption, int text_count_max);
    //----------------------------------------------------------------------------------------------------------
    //功能:
    //载入字符串,将字符串写入到逻辑的输出框中
    //参数:
    //输入框句柄
    //字符串指针
    //字符串大小             
    //字符串颜色
    //返回值 成功返回0 如果数量超限则返回 OE_TEXTERROR
    //输入函数
    int LoadStringToBox(CWG_HANDLE handle, const TCHAR * test, int count, CWG_COLOR color);
    //功能
    //显示输出框
    //参数
    //输出框句柄
    void showOutPutBox(CWG_HANDLE handle);
    //该函数由showOutPutBox调用 
    //功能:
    //画出边框
    //参数 
    //输出框结构
    //返回值
    //一行所占的空格位(包含边框)
    int showBoxSide(OUTPUTBOX outputbox);
    //分页
    //内容指针 分页数组的数组名 一行空格位 一页总空格位
    void makePage(TCHAR *p,int line_s_count, int maxcount);
    //显示指定页的内容 分页数组名   页数()
    void showPage(int n);
    
    #endif

    cwguser.cpp(vs .c不方便 若.c出现问题可以自行更改)

    #include"cwguser.h"
    #include<stdio.h>
    CWG_HANDLE OutPutBox(OUTPUTBOX outputbox, const TCHAR * caption, int text_count_max)
    {
        static CWG_HANDLE ihandle = 100;
        extern OUTPUTBOX outputbox_save;
        extern CWG_HANDLE handle;
        extern TCHAR CAPTION[10];
        extern int text_count_max_save;
        handle = ihandle++;
    
        outputbox_save.xbox                = outputbox.xbox ;
        outputbox_save.ybox                = outputbox.ybox ;
        outputbox_save.xWidth            = outputbox.xWidth;
        outputbox_save.yHeight            = outputbox.yHeight;
        outputbox_save.box_color        = outputbox.box_color;
    
        outputbox_save.pBoxUpLineCh        = outputbox.pBoxUpLineCh;
        outputbox_save.nBoxUpLineCh        = outputbox.nBoxUpLineCh;
    
        outputbox_save.pBoxDownLineCh    = outputbox.pBoxDownLineCh;
        outputbox_save.nBoxDownLineCh    = outputbox.nBoxDownLineCh;
    
        outputbox_save.pBoxLeftLineCh    = outputbox.pBoxLeftLineCh;
        outputbox_save.nBoxLeftLineCh    = outputbox.nBoxLeftLineCh;
    
        outputbox_save.pBoxRightLineCh = outputbox.pBoxRightLineCh;
        outputbox_save.nBoxRightLineCh = outputbox.nBoxRightLineCh;
    
     
        outputbox_save.pLeDoCh = outputbox.pLeDoCh;
        outputbox_save.nLeDoCh = outputbox.nLeDoCh;
     
        outputbox_save.pLeUpCh = outputbox.pLeUpCh;
        outputbox_save.nLeUpCh = outputbox.nLeUpCh;
    
        outputbox_save.pRiDoCh = outputbox.pRiDoCh;
        outputbox_save.nRiDoCh = outputbox.nRiDoCh;
    
        outputbox_save.pRiUpCh = outputbox.pRiUpCh;
        outputbox_save.nRiUpCh = outputbox.nRiUpCh;
    
        wcscpy_s(CAPTION,wcslen(caption)+2,caption);
        text_count_max_save = text_count_max;
    
        return handle;
    }
    int LoadStringToBox(CWG_HANDLE handle, const TCHAR * text,int count, CWG_COLOR color)
    {
        //句柄这时候只是装逼用的 还没有什么卵用 这个位置先留给它
        extern int text_count_max_save;
        extern int text_count_now_save;
        extern TCHAR * pText;
        if (text_count_now_save == 0)//如果是第一次加载string 准备好pText
        {
            pText = (TCHAR *)malloc(sizeof(TCHAR)*1);
            pText = (TCHAR *)memset(pText,(TCHAR)'', 2);
            text_count_now_save = 2;
        }
        if (text_count_now_save > text_count_max_save)
        {
            //超出范围
        }
        else
        {
            //现在的数据        =           新增的数据       +   原来的数据包含
            text_count_now_save = wcslen(text)*sizeof(TCHAR) + text_count_now_save;
            //申请更大的容量
            TCHAR * temp = (TCHAR *)malloc(text_count_now_save);
            int temp_n = 0;//temp指针的偏移变量
            int text_n = 0;//text的偏移变量
            int p_n = 0;//pText的偏移变量
    
            //将pText的内容复制到temp中
            while ((temp[temp_n++] = pText[p_n++]) != '');
            //将text拼接到temp中
            temp_n = 0;//temp指针的偏移变量 置零
            text_n = 0;//text的偏移变量 置零
            while (1)
            {
                if (temp[temp_n] == '')
                {
                    while (1)
                    {
                        temp[temp_n++] = text[text_n++];
                        if (text[text_n - 1] == '')
                        {
                        goto  BREAKLOOP;
                        }
                    }
                }
                temp_n++;
            }
            BREAKLOOP:
            free(pText);
            pText = temp;
        }
    
        
        return 0;
    }
    int showBoxSide(OUTPUTBOX outputbox)
    {
        int sum = 0;//计算一行有多少个空格位(包含边框)
        int i, j;
        int line = outputbox.ybox;
        int row = outputbox.xbox;
        for (i = 0; i < outputbox.yHeight; i++)
        {
            if (i == 0)//第一行
            {
                gotoxy(row, line++);
                for (j = 0; j < outputbox.xWidth; j++)//列循环
                {
                    if (j == 0)//第一列
                    {
                        wprintf(L"%s", outputbox.pLeUpCh);
                        sum += outputbox.nLeUpCh;
                    }
                    else if (j == outputbox.xWidth - 1)//最后一列
                    {
                        wprintf(L"%s", outputbox.pRiUpCh);
                        sum += outputbox.nRiUpCh;
                    }
                    else//中间列
                    {
                        wprintf(L"%s", outputbox.pBoxUpLineCh);
                        sum += outputbox.nBoxUpLineCh;
                    }
                }
            }
            else if (i == outputbox.yHeight - 1)//最后一行
            {
                gotoxy(row, line++);
                wprintf(L"%s", outputbox.pLeDoCh);
                for (j = 0; j < (sum - outputbox.nLeUpCh - outputbox.nRiUpCh) / outputbox.nBoxDownLineCh; j++)
                {
                    wprintf(L"%s", outputbox.pBoxDownLineCh);
                }
                wprintf(L"%s", outputbox.pRiDoCh);
            }
            else//中间行
            {
                gotoxy(row, line++);
                wprintf(L"%s", outputbox.pBoxLeftLineCh);
                for (j = 0; j < sum - outputbox.nLeUpCh - outputbox.nRiUpCh; j++)
                {
                    wprintf(L" ");
                }
                wprintf(L"%s", outputbox.pBoxRightLineCh);
            }
        }
        return sum;
    }
    void makePage(TCHAR *p, int line_s_count, int maxcount)
    {
        int space;//当前行剩余空格位
        int count;//当前页剩余空格位
        int n = 0;//记录页数
        extern TCHAR ** page_add_save;
        space = line_s_count;
        count = maxcount;
        page_add_save[0] = p;
        while (1)
        {
            const int isSingle = (0 <= *p&&*p <= 0X2C77);//是否只占一个空格位
            if (*p != (TCHAR)'
    ')//若不是
    
            {
                space -= isSingle ? 1 : 2;
                count -= isSingle ? 1 : 2;//减去对应的量
                p++;
            }
            else
            {
                count -= space;                //一回车 一下子用了好多空格位
                space = line_s_count;        //本行剩余空格位重设
                p++;                        //定位到下一个字符
            }
            if (space == 0)
            {
                space = line_s_count;//本行剩余空格位重设
    
            }
            //如果一页使用完毕
            if (count == 0)
            {
                n++;
                page_add_save[n] = p;//更新页数
                count = maxcount;
    
            }
            if (*p == '')
            {
                return;
            }
        }
    }
    void showPage(int n)
    {
        extern TCHAR ** page_add_save;
        extern OUTPUTBOX outputbox_save;
        TCHAR ** page_add = page_add_save;
        TCHAR *p;
        int space;
        int count;
        int line;
        int row;
        //开始清空本页
        int i, j;
        line = outputbox_save.ybox + 1;
        row = outputbox_save.xbox + outputbox_save.nLeUpCh;
        gotoxy(row, line);//移动坐标到输入点
        for (i = 0; i < outputbox_save.yHeight - 2; i++)
        {
            for (j = 0; j < (outputbox_save.xWidth - 2)*outputbox_save.nBoxUpLineCh; j++)
            {
                wprintf(L" ");
            }
            line++;
            gotoxy(row, line);
        }
        p = page_add[n];//当前页数重新设置
        space = outputbox_save.nBoxUpLineCh*(outputbox_save.xWidth - 2);
        count = space * (outputbox_save.yHeight - 2);
        line = outputbox_save.ybox + 1;
        row = outputbox_save.xbox + outputbox_save.nLeUpCh;
        gotoxy(row, line);//移动坐标到输入点
        while (1)
        {
            const int isSingle = (0 <= *p&&*p <= 0X2C77);//是否只占一个空格位
            if (*p != (TCHAR)'
    ')//若不是
    
            {
                printf("%lc", *p);
                space -= isSingle ? 1 : 2;
                count -= isSingle ? 1 : 2;//减去对应的量
                p++;
            }
            else
            {
                count -= space;//一回车 一下子用了好多空格位
                space = outputbox_save.nBoxUpLineCh*(outputbox_save.xWidth - 2);//本行剩余空格位重设
                line++;
                gotoxy(row, line);
                p++;    //定位到下一个字符
            }
            if (*p == '')
            {
                getchar();
                exit(0);
            }
            if (space == 0)
            {
                line++;
                gotoxy(row, line);
                space = outputbox_save.nBoxUpLineCh*(outputbox_save.xWidth - 2);//本行剩余空格位重设
            }
            if (count == 0)
            {
                break;
            }
        }//end 读取一页 while(1)
    }
    void showOutPutBox(CWG_HANDLE handle)
    {
        extern OUTPUTBOX outputbox_save;
        int sum= 0;                                //计算一行有多少个空格位
        //以下打印输出框    
        sum = showBoxSide(outputbox_save);
        //获取输出文字的大小
        extern int text_count_now_save;
        int textcount = text_count_now_save;
        int maxcount = ((sum - outputbox_save.nBoxLeftLineCh * 2))*(outputbox_save.yHeight - 2);//计算输出框一页最大容量
        int n_max = text_count_now_save / maxcount;//最大页数
        extern TCHAR * pText;
        int line_s_count = maxcount/ (outputbox_save.yHeight - 2);//计算一行的空格位
        //申请页 地址的储存空间
        extern TCHAR** page_add_save;
        page_add_save = (TCHAR**)malloc(sizeof(TCHAR*)*n_max);
        makePage(pText,line_s_count,maxcount);//将每一页的首地址放在page_add中
    }

    gotoxy.cpp

    #include<windows.h>
    void gotoxy(int x, int y)
    {
        COORD pos;
        pos.X = x;
        pos.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
    }

    outputdate.cpp

    #include "output.h"
    
    OUTPUTBOX outputbox_save;    //输出框结构
    CWG_HANDLE handle;            //储存句柄
    TCHAR CAPTION[10];            //标题数组
    int text_count_max_save = 10240;    //最大内容字节数
    int text_count_now_save = 0;    //当前内容字节数
    TCHAR *pText;                    //指向内容的指针
    TCHAR ** page_add_save;            //分页

    以下是对本套代码的使用示例

    #include"cwguser.h"
    int main()
    {
        setlocale(LC_ALL, "chs");
        OUTPUTBOX outputbox;
        outputbox.box_color = 0;
        outputbox.xbox = 6;
        outputbox.ybox = 2;
        outputbox.yHeight = 10;
        outputbox.xWidth = 10;
    
        outputbox.pLeUpCh = SC("");
        outputbox.nLeUpCh = 2;
    
        outputbox.pLeDoCh = SC("");
        outputbox.nLeDoCh = 2;
    
        outputbox.pRiUpCh = SC("");
        outputbox.nRiUpCh = 2;
    
        outputbox.pRiDoCh = SC("");
        outputbox.nRiDoCh = 2;
    
        outputbox.pBoxUpLineCh = SC("");
        outputbox.nBoxUpLineCh = 2;
    
        outputbox.pBoxDownLineCh = SC("");
        outputbox.nBoxDownLineCh = 2;
    
        outputbox.pBoxLeftLineCh = SC("<<");
        outputbox.nBoxLeftLineCh = 2;
    
        outputbox.pBoxRightLineCh = SC("");
        outputbox.nBoxRightLineCh = 2;
    
        OutPutBox(outputbox,L"这是输出框标题", 1000*sizeof(TCHAR));
        LoadStringToBox(0,_T("--.-.--.-.-...---.-.-.
    摩尔斯电码你怕不怕
    "),wcslen(_T("这是测试字符串")),0);
        LoadStringToBox(0, _T("1001110101001110101010
    二进制你怕不怕
    -1001101-10011-10-1-11
    三进制你怕不怕,
    你怕不怕我不知道,
    反正我是怕了
    !!!!!!省略号
    大坏蛋!!!!!"), wcslen(_T("这是测试字符串")), 0);
        LoadStringToBox(0,_T("aaaaaa
    
    aaa亮闪闪
    的中文aaaa
    "),wcslen(_T("这是测试字符串")),0);
        LoadStringToBox(0, _T("bbb
    bbbb
    bbb飞哥你好.000000000000.0.0.0.0.0.0.0.0.0.0.0.0.0.000.0.0.2333
    "), wcslen(_T("这是测试字符串")),0);
        LoadStringToBox(0, _T("新的一页
    "), wcslen(_T("这是测试字符串")), 0);
        showOutPutBox(0);
        //打印指定页的内容
        showPage(0);
        getchar();
        showPage(1);
        getchar();
        showPage(2);
        getchar();//其实你 可以在这里实现上下翻页
        
    
        getchar();
        return 0;
    }

    注意:

    代码并不是完善的,因此可以看到许多参数是"废物",先无视就好啦,随便写个什么也可以...........

  • 相关阅读:
    iOS App Store审核上传应用预览视频
    mac 下常用命令(xcode常用命令,环境相关等)
    Xcode遇到的一些常见异常
    Tomcat的SSL配置keytool生成证书
    iOS Developer TODO
    Linix常用命令
    iOS&OSX系统初步了解
    Mac下安装MySQL及启动等常用命令
    Android WebView存在跨域访问漏洞(CNVD-2017-36682)介绍及解决
    HTML5 Audio/Video 标签,属性,方法,事件汇总 (转)
  • 原文地址:https://www.cnblogs.com/xdblog/p/4852070.html
Copyright © 2020-2023  润新知