鱼C论坛

 找回密码
 立即注册
查看: 2124|回复: 0

[技术交流] 自己写的一个可以设置控制台光标的类

[复制链接]
发表于 2019-9-16 21:45:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 bin554385863 于 2019-9-16 21:47 编辑

GameWnd.h
  1. #include <windows.h>
  2. #include <cstdio>
  3. class GameWnd
  4. {
  5. private:
  6.     TCHAR *Title;   //窗口标题
  7.     int Fclr, Bclr; //前景色,背景色
  8.     COORD Pos;
  9.     HANDLE winHandle; //窗口句柄
  10. public:
  11.     GameWnd(TCHAR *t = nullptr) : Title(t)
  12.     {
  13.         winHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  14.         SetConsoleTitle(Title);
  15.     }
  16.     void SetColor(int fclr, int bclr);
  17.     void SetPosition(short x, short y);
  18.     void CleanScr(short begx, short begy, int endx, int endy);
  19.     ~GameWnd() {}
  20. };
  21. void GameWnd::SetColor(int fclr, int bclr)
  22. {
  23.     Fclr = fclr;
  24.     Bclr = bclr;
  25.     SetConsoleTextAttribute(winHandle, Fclr + Bclr * 0x10);
  26. }
  27. void GameWnd::SetPosition(short x, short y)
  28. {
  29.     Pos = {x, y};
  30.     SetConsoleCursorPosition(winHandle, Pos);
  31. }
  32. void GameWnd::CleanScr(short begx, short begy, int endx, int endy)
  33. {
  34.     for (size_t i = begx; i < endx; i++)
  35.     {
  36.         for (size_t j = begy; j < endy; j++)
  37.         {
  38.             SetPosition(i, j);
  39.             printf(" ");
  40.         }
  41.         printf("\n");
  42.     }
  43. }
复制代码

---------------------------------------------------------------------------------
Game.cpp
  1. #include "GameWnd.h"
  2. #include <iostream>
  3. int main()
  4. {
  5.         char t[] = "*******";
  6.         GameWnd wnd = t;
  7.         wnd.SetColor(2, 0);
  8.    wnd.SetPosition(0,0);
  9.         for (int i = 0; i < 6; i++)
  10.         {
  11.                 for (int j = 0; j < 160; j++)
  12.                 {
  13.                         printf("*");
  14.                 }
  15.                 printf("\n");
  16.         }
  17.         wnd.CleanScr(3,1,157,5);
  18.         wnd.SetPosition(80, 2);
  19.         std::cout<<"八格牙路";
  20.         wnd.SetPosition(170, 8);
  21.         return 0;
  22. }
复制代码

-----------------------------------------------------------------------------------------------------------
****************************************************************************************************************************************************************
***                                                                                                                                                                                                                                                                             ***
***                                                                                                                          八格牙路                                                                                                                                       ***
***                                                                                                                                                                                                                                                                             .***                                                                                                                                                          ****************************************************************************************************************************************************************

E:\Users\86184\Documents\Code>
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 21:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表