| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3133 人关注过本帖
标题:我承认 我懒了 求 清屏代码啊~诶 怎么都不能清屏
只看楼主 加入收藏
零波丽
Rank: 3Rank: 3
来 自:莉莉斯
等 级:论坛游侠
帖 子:222
专家分:107
注 册:2011-9-8
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:8 
我承认 我懒了 求 清屏代码啊~诶 怎么都不能清屏
如题 谢谢了 百度 找不到的说
搜索更多相关主题的帖子: 百度 
2011-10-23 15:22
naruto01
Rank: 4
等 级:业余侠客
帖 子:103
专家分:280
注 册:2011-5-23
收藏
得分:7 
http://topic.  1楼 13楼
2011-10-23 15:27
Y_Jo_1
Rank: 4
等 级:业余侠客
帖 子:59
专家分:215
注 册:2011-10-20
收藏
得分:7 
system("cls");
不能直接clrscr();
要通过system函数调用。
2011-10-23 15:32
gball
Rank: 3Rank: 3
等 级:禁止发言
帖 子:56
专家分:192
注 册:2011-9-23
收藏
得分:7 
提示: 作者被禁止或删除 内容自动屏蔽

在网吧通宵泡论坛发贴子,挣齐所有大学学费,详情请点击:   http://www.vikkk.tk/
2011-10-23 15:50
gball
Rank: 3Rank: 3
等 级:禁止发言
帖 子:56
专家分:192
注 册:2011-9-23
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

在网吧通宵泡论坛发贴子,挣齐所有大学学费,详情请点击:   http://www.vikkk.tk/
2011-10-23 15:57
我是菜鸟是我
Rank: 2
等 级:论坛游民
帖 子:49
专家分:94
注 册:2011-3-21
收藏
得分:0 
system("cls");  
   就是这个可以清全屏!
2011-10-23 16:04
gball
Rank: 3Rank: 3
等 级:禁止发言
帖 子:56
专家分:192
注 册:2011-9-23
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

在网吧通宵泡论坛发贴子,挣齐所有大学学费,详情请点击:   http://www.vikkk.tk/
2011-10-23 16:38
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
送你一个吧

Screen.h
程序代码:
#ifndef SCREEN_H
#define SCREEN_H

#include <string>

// 兼容API原来设计采用宏定义
#define FOREGROUND_YELLOW    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define FOREGROUND_WHITE    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
#define BACKGROUND_PURPLE    BACKGROUND_RED | BACKGROUND_BLUE

// 按键常数
#define K_SOH            0x0001        // 标题开始
#define K_STX            0x0002        // 正文开始
#define K_ETX            0x0003        // 正文结束
#define K_EOT            0x0004        // 传输结束
#define K_ENQ            0x0005        // 请求
#define K_ACK            0x0006        // 收到通知
#define K_BEL            0x0007        // 响铃
#define K_BACKSPACE        0x0008        // 退格
#define K_TAB            0x0009        // 水平制表符
#define K_LF            0x000A        // 换行
#define K_VT            0x000B        // 垂直制表符
#define K_FF            0x000C        // 换页符
#define K_ENTER            0x000D        // 回车
#define K_SO            0x000E        // 不用切换
#define K_SI            0x000F        // 启用切换
#define K_DLE            0x0010        // 数据链路转义
#define K_DC1            0x0011        // 设备控制1
#define K_DC2            0x0012        // 设备控制2
#define K_DC3            0x0013        // 设备控制3
#define K_DC4            0x0014        // 设备控制4
#define K_NAK            0x0015        // 拒绝接收
#define K_SYN            0x0016        // 同步空闲
#define K_ETB            0x0017        // 传输块结束
#define K_CAN            0x0018        // 取消/作废
#define K_EM            0x0019        // 介质中断
#define K_SUB            0x001A        // 替补
#define K_ESC            0x001B        // 溢出
#define K_FS            0x001C        // 文件分割符
#define K_GS            0x001D        // 分组符
#define K_RS            0x001E        // 记录分离符
#define K_US            0x001F        // 单元分隔符
#define K_SPACE            0x0020
#define K_CDEL            0x007F        // ASCII DEL
#define K_UP            0xE048
#define K_DOWN            0xE050
#define K_LEFT            0xE04B
#define K_RIGHT            0xE04D
#define K_HOME            0xE047
#define K_END            0xE04F
#define K_PGUP            0xE049
#define K_PGDN            0xE051
#define K_INS            0xE052
#define K_DEL            0xE053

class Screen
{
public:

    Screen();                                                                                            // 构造函数
    SHORT GetRows(VOID) const {return ScreenInfo.dwSize.Y;}                                                // 获取总行数
    SHORT GetCols(VOID) const {return ScreenInfo.dwSize.X;}                                                // 获取总列数
    WCHAR ReadKey(VOID) const;                                                                            // 从键盘接收一个按键
    VOID SetTitle(const std::wstring& title) const;                                                        // 设置控制台标题文字
    COORD GetCursorPosition(VOID) const {return ScreenInfo.dwCursorPosition;};                            // 获取当前光标位置
    Screen& MoveCursor(const SHORT x, const SHORT y);                                                    // 将光标定位到指定位置
    Screen& MoveCursor(const COORD& position);                                                            // 将光标定位到指定位置
    WORD GetTextAttribute(VOID) const {return ScreenInfo.wAttributes;};                                    // 获取当前文本颜色
    Screen& SetTextAttribute(const WORD attribute);                                                        // 设置文本颜色
    VOID WriteText(const WCHAR character);                                                                // 在当前光标位置输出单个字符
    VOID WriteText(const WCHAR* text);                                                                    // 在当前光标位置输出字符串
    VOID WriteText(const std::wstring& text);                                                            // 在当前光标位置输出字符串
    VOID Cls(VOID);                                                                                        // 用当前颜色清屏
    VOID Cls(const WORD attribute);                                                                        // 用指定颜色清屏
    VOID Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2);                            // 将指定区域用当前颜色清屏
    VOID Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute);    // 将指定区域用指定颜色清屏
    VOID Box(const SMALL_RECT& rect);                                                                    // 用当前颜色绘画矩形
    VOID Box(const SMALL_RECT& rect, const WORD attribute);                                                // 用指定颜色绘画矩形
    VOID Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2);                            // 用当前颜色绘画矩形
    VOID Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute);        // 用指定颜色绘画矩形

private:

    HANDLE hOutput;                                // 输出设备句柄
    CONSOLE_SCREEN_BUFFER_INFO ScreenInfo;        // 屏幕缓冲区数据
    CONSOLE_CURSOR_INFO CursorInfo;                // 光标数据

};

#endif

Screen.cpp



程序代码:
#include <Windows.h>
#include <my_Tools.h>
#include <Screen.h>

Screen::Screen()
{
    this->hOutput = GetStdHandle(STD_OUTPUT_HANDLE);                            // 获取控制台句柄
    if (hOutput != INVALID_HANDLE_VALUE)
    {
        GetConsoleScreenBufferInfo(hOutput, &ScreenInfo);
        ScreenInfo.dwSize.X = 80;
        ScreenInfo.dwSize.Y = 25;
        SetConsoleScreenBufferSize(hOutput, ScreenInfo.dwSize);
        GetConsoleCursorInfo(hOutput, &CursorInfo);
    }
    else
    {
        MessageBoxW(NULL, L"获取控制台窗口句柄失败!", L"Screen Class", MB_ICONSTOP | MB_OK);
    }
}

WCHAR Screen::ReadKey(VOID) const
{
    WCHAR keycode = _getwch();
    if ((keycode & 0xE0) == 0xE0)
    {
        keycode = keycode * 256 + _getwch();
    }
    return keycode;
}

VOID Screen::SetTitle(const std::wstring& title) const
{
    SetConsoleTitleW(title.c_str());
}

Screen& Screen::MoveCursor(const SHORT x, const SHORT y)
{
    ScreenInfo.dwCursorPosition.X = x;
    ScreenInfo.dwCursorPosition.Y = y;
    SetConsoleCursorPosition(hOutput, ScreenInfo.dwCursorPosition);
    return *this;
}

Screen& Screen::MoveCursor(const COORD& position)
{
    SetConsoleCursorPosition(hOutput, position);
    return *this;
}

Screen& Screen::SetTextAttribute(const WORD attribute)
{
    ScreenInfo.wAttributes = attribute;
    SetConsoleTextAttribute(hOutput, ScreenInfo.wAttributes);
    return *this;
}

VOID Screen::WriteText(const WCHAR character)
{
    DWORD number;
    WriteConsoleW(hOutput, &character, 1, &number, NULL);
    if (IsAscii(character))
    {
        ++ScreenInfo.dwCursorPosition.X;
    }
    else
    {
        ScreenInfo.dwCursorPosition.X += 2;
    }
    MoveCursor(ScreenInfo.dwCursorPosition);
}

VOID Screen::WriteText(const WCHAR* text)
{
    while (*text)
    {
        WriteText(*text);
        ++text;
    }
}

VOID Screen::WriteText(const std::wstring& text)
{
    for (std::wstring::const_iterator index(text.begin()); index != text.end(); ++index)
    {
        WriteText(*index);
    }
}

VOID Screen::Cls(VOID)
{
    COORD pos = {ScreenInfo.dwCursorPosition.X, ScreenInfo.dwCursorPosition.Y};
    for (SHORT y = 0; y != ScreenInfo.dwSize.Y; ++y)
    {
        for (SHORT x = 0; x != ScreenInfo.dwSize.X; ++x)
        {
            MoveCursor(x, y).WriteText(L' ');
        }
    }
    MoveCursor(pos);
}

VOID Screen::Cls(const WORD attribute)
{
    SetTextAttribute(attribute).Cls();
}

VOID Screen::Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2)
{
    COORD old_pos(GetCursorPosition());
    for (SHORT y = y1; y <= y2; ++y)
    {
        for (SHORT x = x1; x <= x2; ++x)
        {
            MoveCursor(x,y).WriteText(L' ');
        }
    }
    MoveCursor(old_pos);
}

VOID Screen::Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute)
{
    WORD old_attr(GetTextAttribute());
    COORD old_pos(GetCursorPosition());
    SetTextAttribute(attribute);
    for (SHORT y = y1; y <= y2; ++y)
    {
        for (SHORT x = x1; x <= x2; ++x)
        {
            MoveCursor(x,y).WriteText(L' ');
        }
    }
    MoveCursor(old_pos);
    SetTextAttribute(old_attr);
}

VOID Screen::Box(const SMALL_RECT& rect)
{
    std::wstring text;

    text = L'';
    for (SHORT i = rect.Left + 2; i != rect.Right; ++i)
    {
        text += L'';
    }
    text += L'';
    MoveCursor(rect.Left, rect.Top).WriteText(text);

    for (SHORT j = rect.Top + 1; j < rect.Bottom; ++j)
    {
        text = L'';
        for (SHORT i = rect.Left + 2; i != rect.Right; ++i)
        {
            text += L"  ";
        }
        text += L'';
        MoveCursor(rect.Left, j).WriteText(text);
    }

    text = L'';
    for (SHORT i = rect.Left + 2; i != rect.Right; ++i)
    {
        text += L'';
    }
    text += L'';
    MoveCursor(rect.Left, rect.Bottom).WriteText(text);
}

VOID Screen::Box(const SMALL_RECT& rect, const WORD attribute)
{
    WORD old_attr(ScreenInfo.wAttributes);
    SetTextAttribute(attribute).Box(rect);
    SetTextAttribute(old_attr);
}

VOID Screen::Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2)
{
    SMALL_RECT rect;
    rect.Left = x1;
    rect.Top = y1;
    rect.Right = x2;
    rect.Bottom = y2;
    Box(rect);
}

VOID Screen::Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute)
{
    WORD old_attr(ScreenInfo.wAttributes);
    SetTextAttribute(attribute).Box(x1, y1, x2, y2);
    SetTextAttribute(old_attr);
}



[ 本帖最后由 TonyDeng 于 2011-10-23 21:59 编辑 ]

授人以渔,不授人以鱼。
2011-10-23 21:56
wang155423
Rank: 6Rank: 6
等 级:侠之大者
帖 子:216
专家分:408
注 册:2011-9-4
收藏
得分:0 
system("cls");  
2011-10-23 22:01
快速回复:我承认 我懒了 求 清屏代码啊~诶 怎么都不能清屏
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025282 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved