Linux 下實現控制屏幕顯示信息和光標的狀態

 

Linux 下實現控制屏幕顯示信息和光標的狀態

 

//display.h

/*************************************************************    
    FileName : display.h
    FileFunc : 控制屏幕顯示信息和光標的狀態頭文件  
    Version  : V0.1    
    Author   : Sunrier    
    Date     : 2012-06-09
    Descp    : Linux下實現屏幕和光標的控制    
*************************************************************/
#ifndef   _DISPLAY_H_    
#define   _DISPLAY_H_ 

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define MAX_DISPLAY_ITEM	25	//25行
#define MAX_DISPLAY_WIDTH	80	//80列

void Display_Message(int x,int y,char *str);
void Draw_Box(int row,int col,int len,int wid);
void Move_Cursor(int x,int y);
void Display_Cursor( void );
void Hide_Cursor( void );
void Clear_All_Display( void );
void Clear_Screen( void );

#ifdef __cplusplus
}
#endif

#endif 



 

 

 

//display.c

/*************************************************************    
    FileName : display.c
    FileFunc : 控制屏幕顯示信息和光標的狀態實現文件   
    Version  : V0.1    
    Author   : Sunrier    
    Date     : 2012-06-09
    Descp    : Linux下實現屏幕和光標的控制    
*************************************************************/
#include "display.h"

//x->行(從1開始),y->列(從1開始)
//在X行Y列顯示信息
void Display_Message(int x,int y,char *str)
{
	unsigned int uiLen=0;
	char szMessage[512];
	memset(szMessage,0,sizeof(szMessage));

	sprintf(szMessage,"\033[%d;%dH%s",x,y,str);
	uiLen = strlen(szMessage);
	write(1,szMessage,uiLen);
}

//畫邊框
void Draw_Box(int row,int col,int len,int wid)
{
	int i = 0,end = 0;
	char szTop[100],szBottom[100];

	memset(szTop,0,sizeof(szTop));
	memset(szBottom,0,sizeof(szBottom));
	strcpy(szTop,"┏");
	strcpy(szBottom,"┗");
	for (i=1;i<wid/2-1;i++)
	{
		strcat(szTop,"━");
		strcat(szBottom,"━");
	}
	strcat(szTop,"┓\0");
	strcat(szBottom,"┛\0");

	end=col+wid/2*2-2;
	Display_Message(row,col,szTop);
	for (i=1;i<len-1;i++) 
	{
		Display_Message(row+i,col,"┃");
		Display_Message(row+i,end,"┃\0");
	}
	Display_Message(row+len-1,col,szBottom);
}

//移動光標到X行Y列
void Move_Cursor(int x,int y)
{
	unsigned int uiLen = 0;
	char szMessage[16];

	memset(szMessage,0,sizeof(szMessage));
	sprintf(szMessage,"\033[%d;%dH",x,y);
	uiLen=strlen(szMessage);
	write(1,szMessage,uiLen);
}

//顯示光標
void Display_Cursor( void )
{
	printf("\033[?25h");
}

//隱藏光標
void Hide_Cursor( void )
{
	printf("\033[?25l");
}

//清除所有的顯示信息(X:1到25行;Y:1到80列)
void Clear_All_Display( void )
{
	int i=0;
	char  szZero[MAX_DISPLAY_WIDTH];
	memset(szZero, ' ', sizeof(szZero));
	szZero[sizeof(szZero) -1] = 0;
	for(i = 1; i <= MAX_DISPLAY_ITEM; i++)
	{
		Display_Message(i,1,szZero);
	}
}

//清除屏幕
void Clear_Screen( void )
{
	//printf("\033[2J\033[1;1H\n");
	printf("\033[2J\033[1;1H");
}


 

 


附:
Linux 終端下顏色的輸出

在命令行下也能產生五顏六色的字體和圖案,只需要加上一些顏色代碼
例1:
    printf("\033[44;31m Sunrier\033[0m")
    其中44代表字背景色, 31代表字體的顏色,Sunrier是字符串,後面的\033[0m是控制碼,表示關閉所有屬性,m意味着設置屬性然後結束
    
    
例2:
    echo -e "\033[41;36m 紅底綠字\033[0m"
    其中41代表字背景色, 36代表字體的顏色

    字背景顏色範圍:40----47
    40:黑
    41:深紅
    42:綠
    43:黃色
    44:藍色
    45:紫色
    46:深綠
    47:白色

    字顏色:30-----------37
    30:黑
    31:紅
    32:綠
    33:黃
    34:藍色
    35:紫色
    36:深綠
    37:白色

    ANSI控制碼的說明
    \33[0m 關閉所有屬性,設置成默認屬性
    \33[1m 設置高亮度
    \33[4m 下劃線
    \33[5m 閃爍
    \33[7m 反顯
    \33[8m 消隱
    \33[30m -- \33[37m 設置前景色
    \33[40m -- \33[47m 設置背景色
    \33[nA 光標上移n行
    \33[nB 光標下移n行
    \33[nC 光標右移n行
    \33[nD 光標左移n行
    \33[y;xH設置光標位置
    \33[2J 清屏
    \33[K 清除從光標到行尾的內容
    \33[s 保存光標位置
    \33[u 恢復光標位置
    \33[?25l 隱藏光標
    \33[?25h 顯示光標

一般使用時習慣把\33寫成\033,其中\nnn 插入nnn(注n爲1到3位)(八進制)所代表的ASCII字符



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章