【一套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)'\0', 2);
        text_count_now_save = 2;
    }
    if (text_count_now_save > text_count_max_save)
    {
        //超出范圍
    }
    else
    {
        //現在的數據        =           新增的數據       +   原來的數據包含\0
        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++]) != '\0');
        //將text拼接到temp中
        temp_n = 0;//temp指針的偏移變量 置零
        text_n = 0;//text的偏移變量 置零
        while (1)
        {
            if (temp[temp_n] == '\0')
            {
                while (1)
                {
                    temp[temp_n++] = text[text_n++];
                    if (text[text_n - 1] == '\0')
                    {
                    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)'\n')//若不是\n
        {
            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 == '\0')
        {
            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)'\n')//若不是\n
        {
            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 == '\0')
        {
            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("--.-.--.-.-...---.-.-.\n摩爾斯電碼你怕不怕\n"),wcslen(_T("這是測試字符串")),0);
    LoadStringToBox(0, _T("1001110101001110101010\n二進制你怕不怕\n-1001101-10011-10-1-11\n三進制你怕不怕,\n你怕不怕我不知道,\n反正我是怕了\n!!!!!!省略號\n大壞蛋!!!!!"), wcslen(_T("這是測試字符串")), 0);
    LoadStringToBox(0,_T("aaaaaa\n\naaa亮閃閃\n的中文aaaa\n"),wcslen(_T("這是測試字符串")),0);
    LoadStringToBox(0, _T("bbb\nbbbb\nbbb飛哥你好.000000000000.0.0.0.0.0.0.0.0.0.0.0.0.0.000.0.0.2333\n"), wcslen(_T("這是測試字符串")),0);
    LoadStringToBox(0, _T("新的一頁\n"), wcslen(_T("這是測試字符串")), 0);
    showOutPutBox(0);
    //打印指定頁的內容
    showPage(0);
    getchar();
    showPage(1);
    getchar();
    showPage(2);
    getchar();//其實你 可以在這里實現上下翻頁
    

    getchar();
    return 0;
}

注意:

代碼並不是完善的,因此可以看到許多參數是"廢物",先無視就好啦,隨便寫個什么也可以...........


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM