青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

我住包子山

this->blog.MoveTo("blog.baozishan.in")

Win32 Console Applications - Part 4 of 6 from adrianxw.dk

Win32 Console Applications - Part 4.

So far all of the console output I have shown have been in black and white. This part of the tutorial demonstrates how I did this!

Now, before you start getting all excited, let me say from the start, the range of colours available is not huge. You may want to compile and run these programs for yourself, the .jpg's I use on the web site show up fine on the monitor connected to the machine I am using now, but when viewed on my older machine, appear very dark and fuzzy.

We will use the Win32 API function SetConsoleTextAttribute(). This function takes 2 arguments. The first is the standard output handle, we have covered that already. The second is a 16bit WORD bit mask containg 0 or more bits.

It is not necessary to concern yourself over the values of the various bits. There are a series of constants defined in windows.h, (actually in wincon.h but that is included in windows.h), which symbolically allow you to build the required value, it is very easy. Look at this program and it's output.

#include <windows.h>
#include <iostream.h>

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED);
    cout << "This text is red." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN);
    cout << "This text is green." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE);
    cout << "This text is blue." << endl;

    return 0;
}

A dingy red green and blue. Being bits, they can be OR'd together to make combinations, the next program shows the result of OR'ing the bits together.

#include <windows.h>
#include <iostream.h>

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED | 
                            FOREGROUND_GREEN);
    cout << "This text is yellow." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN | 
                            FOREGROUND_BLUE);
    cout << "This text is cyan." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE | 
                            FOREGROUND_RED);
    cout << "This text is magenta." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED | 
                            FOREGROUND_GREEN | 
                            FOREGROUND_BLUE);
    cout << "This text is white." << endl;

    return 0;
}

The bitwise OR operator "|" combines the requested bits. Thus red | green gives yellow, green | blue gives cyan, blue | red gives magenta and all three combined gives white. The order is unimportant, red | blue gives the same result as blue | red.

Another bit that can be OR'd in is FOREGROUND_INTENSITY. All the colours I've shown so far are a bit dim, adding the intensity bit brightens them up. This program shows the use and results of the intensity bit.

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED);
    cout << "Red     " << flush;
    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED |
                            FOREGROUND_INTENSITY);
    cout << "Red" << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN);
    cout << "Green   " << flush;
    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN |
                            FOREGROUND_INTENSITY);
    cout << "Green" << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE);
    cout << "Blue    " << flush;
    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE |
                            FOREGROUND_INTENSITY);
    cout << "Blue" << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED | 
                            FOREGROUND_GREEN);
    cout << "Yellow  " << flush;
    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED | 
                            FOREGROUND_GREEN |
                            FOREGROUND_INTENSITY);
    cout << "Yellow" << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN | 
                            FOREGROUND_BLUE);
    cout << "Cyan    " << flush;
    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN | 
                            FOREGROUND_BLUE |
                            FOREGROUND_INTENSITY);
    cout << "Cyan" << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE | 
                            FOREGROUND_RED);
    cout << "Magenta " << flush;
    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE | 
                            FOREGROUND_RED |
                            FOREGROUND_INTENSITY);
    cout << "Magenta" << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED | 
                            FOREGROUND_GREEN | 
                            FOREGROUND_BLUE);
    cout << "White   " << flush;
    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED | 
                            FOREGROUND_GREEN | 
                            FOREGROUND_BLUE |
                            FOREGROUND_INTENSITY);
    cout << "White" << endl;

    return 0;
}

Black is also a colour, and is made by having no foreground colors set, no red and no green and no blue is nothing, is black. So in all we have 15 colours. I did warn you not to get excited.

In addition to changing the foreground colour, we can also change the background colour. The same colours are available.

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED);
    cout << "Red     " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED |
                            BACKGROUND_INTENSITY);
    cout << "Red     " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN);
    cout << "Green   " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN |
                            BACKGROUND_INTENSITY);
    cout << "Green   " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE);
    cout << "Blue    " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE |
                            BACKGROUND_INTENSITY);
    cout << "Blue    " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN);
    cout << "Yellow  " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN |
                            BACKGROUND_INTENSITY);
    cout << "Yellow  " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE);
    cout << "Cyan    " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE |
                            BACKGROUND_INTENSITY);
    cout << "Cyan    " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE | 
                            BACKGROUND_RED);
    cout << "Magenta " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE | 
                            BACKGROUND_RED |
                            BACKGROUND_INTENSITY);
    cout << "Magenta " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE);
    cout << "White   " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE |
                            BACKGROUND_INTENSITY);
    cout << "White   " << endl;

    return 0;
}

A space character will just show up as the coloured background. That is how I made the Danish flag at the top of this section.

It is possible to set both the foreground and background colours, here for example, I set the background to low intensity yellow and the foreground to high intensity cyan.

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN |
                            BACKGROUND_RED |
                            FOREGROUND_GREEN | 
                            FOREGROUND_BLUE |
                            FOREGROUND_INTENSITY);
    cout << "Intense Cyan on yellow background." << endl;

    return 0;
}

You can use any combination you like of the available colours, although obviously, setting the foreground and background colour to the same value is a little pointless. Remember also, that setting no foreground bits gives black text, and no background bits, a black background.

A common "gotcha" is forgetting that both the foreground AND background must be set at each call. If you want to write first in green then in red on a yellow background you must set the background in both calls, otherwise the function assumes you want to return to a black background.

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN |
                            BACKGROUND_RED |
                            FOREGROUND_GREEN | 
                            FOREGROUND_INTENSITY);
    cout << "Green " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN |
                            BACKGROUND_RED |
                            FOREGROUND_RED |
                            FOREGROUND_INTENSITY);
    cout << "Red" << endl;

    return 0;
}

If you are going to do a lot of colour changes, rather than OR'ing everything together each time, you may prefer to do it once using a #define. Here, I #define FRI to be Foreground Red Intense, BC to be Background Cyan, FW to Foreground White and BNULL to Background "Nothing" i.e. black. Using these in a small test program does not really make much difference, but in large programs with many colour changes, the code can be much shorter using this technique.

#include <windows.h>
#include <iostream>
using namespace std;

#define FRI FOREGROUND_RED |\
            FOREGROUND_INTENSITY
#define FW  FOREGROUND_RED |\
            FOREGROUND_GREEN |\
            FOREGROUND_BLUE
#define BC  BACKGROUND_GREEN |\
            BACKGROUND_BLUE
#define BNULL 0

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    cout << "Normal " << flush;

    SetConsoleTextAttribute(hOut,
                            FRI | BC);

    cout <<"coloured" << flush;

    SetConsoleTextAttribute(hOut,
                            FW | BNULL);

    cout << " and back." << endl;

    return 0;
}

Note the backslashes in the #defines, this is necessary when extending a #define onto more than one line. You don't have to do this of course if you are happy with long lines/horizontal scrolling. Finally, in this section, here is a header file that contains a full set of #defines. To use it, copy the code and paste it into a file with a .h extension, say "ShortColours.h" for example. Put a copy of this file into the same directory as your code file, and include it into your program like this.

#include "ShortColours.h"

Enclosing the header file name in quotes rather than a less than/greater than pair tells the compiler to look in the same directory as the code.

#ifndef SHORTCOLOURS_H
#define SHORTCOLOURS_H

#define FW FOREGROUND_RED | \
           FOREGROUND_GREEN | \
           FOREGROUND_BLUE
#define FR FOREGROUND_RED
#define FG FOREGROUND_GREEN
#define FB FOREGROUND_BLUE
#define FY FOREGROUND_RED | \
           FOREGROUND_GREEN
#define FC FOREGROUND_GREEN | \
           FOREGROUND_BLUE
#define FM FOREGROUND_BLUE | \
           FOREGROUND_RED 
#define FWI FOREGROUND_RED | \
            FOREGROUND_GREEN | \
            FOREGROUND_BLUE | \
            FOREGROUND_INTENSITY
#define FRI FOREGROUND_RED | \
            FOREGROUND_INTENSITY
#define FGI FOREGROUND_GREEN | \
            FOREGROUND_INTENSITY
#define FBI FOREGROUND_BLUE | \
            FOREGROUND_INTENSITY
#define FYI FOREGROUND_RED | \
            FOREGROUND_GREEN | \
            FOREGROUND_INTENSITY
#define FCI FOREGROUND_GREEN | \
            FOREGROUND_BLUE | \
            FOREGROUND_INTENSITY
#define FMI FOREGROUND_BLUE | \
            FOREGROUND_RED | \
            FOREGROUND_INTENSITY 
#define FNULL 0
 
#define BW BACKGROUND_RED | \
           BACKGROUND_GREEN | \
           BACKGROUND_BLUE
#define BR BACKGROUND_RED
#define BG BACKGROUND_GREEN
#define BB BACKGROUND_BLUE
#define BY BACKGROUND_RED | \
           BACKGROUND_GREEN
#define BC BACKGROUND_GREEN | \
           BACKGROUND_BLUE
#define BM BACKGROUND_BLUE | \
           BACKGROUND_RED 
#define BWI BACKGROUND_RED | \
            BACKGROUND_GREEN | \
            BACKGROUND_BLUE | \
            BACKGROUND_INTENSITY
#define BRI BACKGROUND_RED | \
            BACKGROUND_INTENSITY
#define BGI BACKGROUND_GREEN | \
            BACKGROUND_INTENSITY
#define BBI BACKGROUND_BLUE | \
            BACKGROUND_INTENSITY
#define BYI BACKGROUND_RED | \
            BACKGROUND_GREEN | \
            BACKGROUND_INTENSITY
#define BCI BACKGROUND_GREEN | \
            BACKGROUND_BLUE | \
            BACKGROUND_INTENSITY
#define BMI BACKGROUND_BLUE | \
            BACKGROUND_RED | \
            BACKGROUND_INTENSITY 
#define BNULL 0

#endif

This program produces the same output as the last.

#include <windows.h>
#include <iostream>
#include "ShortColours.h"
using namespace std;

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    cout << "Normal " << flush;

    SetConsoleTextAttribute(hOut,
                            FRI | BC);

    cout <<"coloured" << flush;

    SetConsoleTextAttribute(hOut,
                            FW | BNULL);

    cout << " and back." << endl;

    return 0;
}

The colour support is not great, but at the end of the day, it can be good enough. Consider, there are probably many of you that have played, or at least seen "BattleChess", where the pieces animated battle when one captured another. I bet nobody played it for long, because after you've seen the animations, they simply slow the game down, and the AI behind the graphics was actually not very good.

You build a really good chess playing AI and put it behind a simple interface like this, people will use it over and over, because, it is good enough to get the job done.



----------

In part 2 of the tutorial, I showed you how to use ReadConsoleOutputCharacter() to retrieve the characters at one or more screen buffer locations. By itself, it is not capable of recovering the foreground and background attributes those characters were written with. This information is also available, the API routine used is ReadConsoleOutputAttribute(), and is really very similar to ReadConsoleOutputCharacter(). The prototype is shown here.

BOOL ReadConsoleOutputAttribute(
  HANDLE hConsoleOutput,
  LPWORD lpAttribute,
  DWORD nLength,
  COORD dwReadCoord,
  LPDWORD lpNumberOfAttrsRead
);

This program sets the foreground colout to FOREGROUND_GREEN | FOREGROUND_RED and outputs a string. It then uses ReadConsoleOutputAttribute() to retrieve the attribute of the character at (4,0). The result is 6, this is because FOREGROUND_GREEN is defined to have the value 0x0002 and FOREGROUND_RED 0x0004. 2+4=6. It may be tempting to use the numerical values rather than the defined constants, but this is not a good idea. At some time in the future, MicroSoft may change the values associated with the constants, (unlikely, but a reserved possibility). Using the constants, the code will still work, use the values and the results may be unpredictable.

#include <windows.h>
#include <iostream.h>

int main()
{
    HANDLE hOut;
    WORD Attribute;
    COORD Where;
    unsigned long NumRead;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN | 
                            FOREGROUND_RED);

    cout << "This text is yellow." << endl;

    Where.X = 4;
    Where.Y = 0;
    ReadConsoleOutputAttribute(hOut,
                               &Attribute,
                               1,
                               Where,
                               &NumRead);

    cout << "Attribute is " << Attribute << endl;

    return 0;
}

The output looks like this.

ReadConsoleOutputAttribute

In the next part of the tutorial, we'll investigate keyboard and mouse events.

posted on 2006-07-20 22:54 Gohan 閱讀(1459) 評論(0)  編輯 收藏 引用 所屬分類: C++

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产自产精品| 久久网站免费| 久久久久久国产精品mv| 亚洲在线成人| 午夜日韩av| 欧美在线视频二区| 久久久久久久欧美精品| 可以免费看不卡的av网站| 美女免费视频一区| 亚洲国产婷婷| 一道本一区二区| 午夜综合激情| 亚洲欧美国产毛片在线| 亚洲线精品一区二区三区八戒| 亚洲另类一区二区| 亚洲一区二区三区在线| 欧美一区二区精美| 老色鬼精品视频在线观看播放| 欧美aa国产视频| 日韩视频一区二区三区在线播放免费观看| 亚洲伦理在线免费看| 亚洲自拍偷拍一区| 免费久久精品视频| 国产精品视频九色porn| 亚洲国产欧美一区二区三区久久| 日韩视频在线免费| 久久全国免费视频| 99综合精品| 久久久五月天| 国产精品av久久久久久麻豆网| 国产在线观看91精品一区| 亚洲精品孕妇| 巨胸喷奶水www久久久免费动漫| 亚洲人成人一区二区三区| 国产精品99久久久久久人 | 欧美激情视频给我| 亚洲视频在线观看三级| 久久另类ts人妖一区二区| 欧美天天在线| 国产精品久久久久毛片软件 | 亚洲一区欧美| 欧美成年人网| 亚洲欧美日韩在线不卡| 欧美激情精品久久久久久久变态 | 亚洲国产另类久久精品| 亚洲资源av| 亚洲国产日韩欧美综合久久| 欧美在线欧美在线| 欧美系列精品| 日韩天天综合| 亚洲电影下载| 蜜臀av一级做a爰片久久| 国产视频在线观看一区 | **性色生活片久久毛片| 久久久久se| 午夜精品亚洲| 国产在线视频不卡二| 欧美在线免费播放| 亚洲欧美国产毛片在线| 国产精品视频不卡| 欧美有码在线视频| 欧美精品一区二区精品网| 亚洲欧美日本另类| 欧美午夜精品久久久久久人妖| 亚洲人成艺术| 欧美激情第三页| 久久久青草青青国产亚洲免观| 精品成人a区在线观看| 久久精品99国产精品酒店日本| 亚洲一区二区三区四区五区黄| 欧美无乱码久久久免费午夜一区| 99这里只有久久精品视频| 亚洲福利av| 欧美日本在线看| 亚洲一区二区高清| 亚洲永久免费视频| 黄色一区二区在线观看| 免费成人黄色av| 欧美成人精品在线视频| 99在线精品视频| 亚洲视频香蕉人妖| 狠狠色噜噜狠狠色综合久| 久久亚洲春色中文字幕| 久久久久久久综合| 亚洲欧洲在线免费| 亚洲精品在线电影| 国产欧美不卡| 欧美风情在线观看| 欧美四级电影网站| 快she精品国产999| 欧美日韩亚洲激情| 久久视频一区二区| 欧美日韩国产三区| 久久综合九色99| 欧美日韩一区二区在线| 久久精品日韩欧美| 欧美精品免费观看二区| 午夜视频久久久| 免费短视频成人日韩| 午夜在线观看欧美| 欧美国产日韩一二三区| 久久精品国内一区二区三区| 欧美国产日韩一区二区| 久久精品国产精品亚洲综合| 欧美金8天国| 久久综合久久久久88| 欧美午夜国产| 亚洲国产精品成人精品| 国产一区二区成人| 在线中文字幕一区| 日韩一级黄色片| 久久综合伊人77777蜜臀| 性欧美大战久久久久久久免费观看 | 国产精品magnet| 欧美大片91| 国产一级久久| 亚洲一区二区成人在线观看| 亚洲三级国产| 久久久久久夜精品精品免费| 欧美亚洲日本网站| 亚洲一区二区免费| 欧美日韩小视频| 欧美大片18| 一色屋精品视频在线观看网站| 一区二区成人精品| 亚洲裸体视频| 欧美成人免费在线| 欧美不卡视频一区发布| 国产真实乱偷精品视频免| 在线亚洲一区| 一区二区激情视频| 欧美成人一区二区三区| 免费成人在线视频网站| 国产亚洲欧美aaaa| 亚洲欧美日韩第一区| 亚洲综合不卡| 国产精品裸体一区二区三区| 一区二区欧美在线| 亚洲欧美日韩国产中文| 欧美视频在线一区二区三区| 日韩亚洲精品电影| 亚洲午夜电影在线观看| 欧美另类一区| 日韩一二三在线视频播| 亚洲天堂av综合网| 欧美色播在线播放| 亚洲午夜国产成人av电影男同| 亚洲在线视频一区| 国产日韩欧美视频在线| 久久www免费人成看片高清| 久久夜色精品亚洲噜噜国产mv| 韩日精品中文字幕| 久久久久久久一区| 亚洲黄色免费电影| 亚洲一区在线看| 国产日产欧产精品推荐色 | 在线综合亚洲欧美在线视频| 一区二区三区成人| 国产精品爽黄69| 久久精品人人做人人爽| 欧美国产视频在线观看| 99热这里只有成人精品国产| 国产精品国产精品| 欧美在线观看视频一区二区三区| 麻豆freexxxx性91精品| 亚洲久色影视| 国产精品综合久久久| 久久综合国产精品| 一区二区三区高清| 猛男gaygay欧美视频| 宅男噜噜噜66一区二区| 国产一区二区久久| 欧美电影打屁股sp| 香蕉久久夜色精品国产使用方法| 美女日韩在线中文字幕| 中文网丁香综合网| 精久久久久久久久久久| 欧美日韩一区二区三区| 久久国产日韩| 日韩一本二本av| 欧美岛国在线观看| 欧美在线三级| 一本一本大道香蕉久在线精品| 一区二区日韩免费看| 久久久久五月天| 在线亚洲观看| 亚洲国产三级| 国模 一区 二区 三区| 欧美日韩一区二区三区| 久久综合伊人77777蜜臀| 午夜欧美大片免费观看| 亚洲日本精品国产第一区| 久久最新视频| 久久精品二区亚洲w码| 亚洲色图自拍| 亚洲免费成人| 亚洲激情亚洲| 在线成人av.com| 国产一区二区在线观看免费| 国产精品久久久久影院色老大 |