c++ 繪制方框


知識點:
GetStdHandle函數
FillConsoleOutputCharacter函數
SetConsoleCursorPosition函數
system函數
 

一、 GetStdHandle 獲取標准設備句柄 :  
 HANDLE GetStdHandle(   DWORD nStdHandle   );  
 GetStdHandle()返回標准的輸入、輸出或錯誤的設備的句柄,也就是獲得輸入、輸出/錯誤的屏幕緩沖區的句柄。  
 其參數nStdHandle的值為下面幾種類型的一種:  
 值含義  
 STD_INPUT_HANDLE   標准輸入的句柄  
 STD_OUTPUT_HANDLE 標准輸出的句柄   
  STD_ERROR_HANDLE  標准錯誤的句柄 
二、FillConsoleOutputCharacter 填充字符
BOOL FillConsoleOutputCharacter(

HANDLE hConsoleOutput, // 緩沖區句柄 一般通過
GetStdHandle(STD_OUTPUT_HANDLE)獲取
TCHAR cCharacter,      // 要填充的字符 
DWORD nLength,         // 填充的字符數量  
COORD dwWriteCoord,    // 填充的起始坐標x,y 
LPDWORD lpNumberOfCharsWritten 
  // 返回一個寫入數量的指針

 );


三、SetConsoleCursorPosition 移動光標位置
BOOL SetConsoleCursorPosition(
 
    HANDLE hConsoleOutput,   //緩沖區句柄 一般通過
GetStdHandle(STD_OUTPUT_HANDLE)獲取
  
                  COORD dwCursorPosition   // 指定新的光標位置
                           );

四、設置當前光標位置 gotoxy(int x, int y);
   //之前需要用字符填滿窗口緩沖區
  1、獲取當前輸出緩沖區句柄 GetStdHandle
  2、設置當前光標位置(x,y)   SetConsoleCursorPosition

   
五、用system命令設置顏色和標題
system("title 郁金香灬老師:QQ150330575"); //設置標題
system("color 0A"); //設置顏色
六、繪制方框
  1、確定左上角坐標X,Y //畫邊前先用空格填滿窗口緩沖區
  2、畫上邊
  3、畫下邊
  4、畫左邊
  5、下右邊
 

 
作業:
A、探討以下2個控制台函數的使用方法:
1、GetConsoleTitle 獲取控制台窗口標題
2、SetConsoleTitle 設置控制台窗口標題
B、繪制一個2*2的表格 : 類似下圖
    
    






#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h> 
#include <time.h>
#include "hello.h"
#include <share.h>
#include <Windows.h>

//清屏
void cls_screen(char a)
{
    COORD xy={0,0};
    long byw;
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    FillConsoleOutputCharacter(hout,a,2000,xy,&byw);
}
//#define  var 333;
void gotoxy(int x,int y)
{
    COORD xy={0,0};
     
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    xy.X=x;
    xy.Y=y;
    SetConsoleCursorPosition(hout,xy);
}
//繪制方框
#define X 22
#define Y 6
void drawM(void)
{ int i,j;
    //到達x,y
    gotoxy(X,Y);
    //上邊--------------------
    printf("");
    for (i=0;i<=12;i++)
    {
        printf("");
    }
    printf("");
    //左邊
 
    for (i=0;i<15;i++)
    { 
        gotoxy(X,Y+1+i);
        printf("");
    }
    //右邊
     
    for (i=0;i<15;i++)
    { 
        gotoxy(X+28,Y+1+i);
        printf("");
    }
    //下邊
    gotoxy(X,Y+16);
    //上邊--------------------
    printf("");
    for (i=0;i<=12;i++)
    {
        printf("");
    }
    printf("");

}
int main(void)
{   system("title QQ150330575");
    system("color 0a");
    system("dir");
    cls_screen(' ');//清屏;
    cls_screen(' ');//;
    gotoxy(3,2);
    //繪制方框
    drawM();
    getchar();
    getchar();
    return 0;
}

 


免責聲明!

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



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