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

我住包子山

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 閱讀(1464) 評論(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>
            亚洲伊人网站| 欧美激情一区二区三区在线视频观看 | 欧美成人午夜激情| 欧美精品成人一区二区在线观看| 亚洲性图久久| 久久久久久久国产| 亚洲一区二区视频| 久久亚洲一区二区三区四区| 一区二区精品| 一本色道久久加勒比精品| 国产综合在线视频| 日韩视频在线你懂得| 日韩视频精品在线| 免费一级欧美片在线观看| 狼狼综合久久久久综合网| 国产亚洲精品一区二区| 亚洲欧美影院| 欧美一区中文字幕| 国产精品麻豆成人av电影艾秋| 亚洲精品久久视频| 亚洲欧洲一区| 欧美黄色片免费观看| 欧美激情自拍| 亚洲精品国久久99热| 欧美成年人网站| 最新日韩精品| 正在播放欧美视频| 欧美日韩在线影院| 中国成人黄色视屏| 欧美一区国产一区| 国产一区在线观看视频| 久久精品国产一区二区三区| 久久一区中文字幕| 亚洲高清视频一区| 欧美黑人国产人伦爽爽爽| 亚洲黄色在线观看| 亚洲天堂成人| 国产毛片精品国产一区二区三区| 午夜伦理片一区| 蜜桃久久精品一区二区| 91久久在线观看| 欧美日韩国产色站一区二区三区 | 欧美aⅴ99久久黑人专区| 亚洲国产一区二区a毛片| 欧美电影免费观看| 一区二区高清在线| 久久久久久久成人| 亚洲青涩在线| 国产精品美女久久久免费| 欧美在线看片| 亚洲激情成人| 欧美一区永久视频免费观看| 依依成人综合视频| 欧美日韩精品一区视频 | 免费欧美网站| 一区二区高清在线观看| 久久美女艺术照精彩视频福利播放| 在线一区日本视频| 免费欧美高清视频| 亚洲午夜高清视频| 国外成人免费视频| 欧美屁股在线| 久久动漫亚洲| 99视频精品免费观看| 久久午夜国产精品| 在线亚洲成人| 伊人久久婷婷色综合98网| 欧美日韩成人一区二区三区| 欧美一级理论片| 夜夜爽99久久国产综合精品女不卡| 欧美在线观看天堂一区二区三区| 亚洲区第一页| 精品99一区二区| 国产精品欧美一区喷水| 免费久久99精品国产自在现线| 亚洲一区二区视频| 亚洲精品免费观看| 免费成人av资源网| 欧美综合国产精品久久丁香| 妖精成人www高清在线观看| 极品中文字幕一区| 国产精品色网| 欧美日韩精品伦理作品在线免费观看| 久久久国产午夜精品| 亚洲欧美高清| 一区二区三欧美| 亚洲三级免费| 欧美国产精品中文字幕| 久久久久久久久蜜桃| 亚洲影视综合| 亚洲视频一区二区| 一区二区三区福利| 亚洲欧洲一级| 91久久精品美女| 亚洲第一页在线| 影音先锋国产精品| 国内精品久久久久久久果冻传媒| 国产精品亚洲一区二区三区在线| 欧美日韩一二三四五区| 欧美激情aⅴ一区二区三区| 久久伊人免费视频| 久久久欧美一区二区| 性色av一区二区三区红粉影视| 一区二区三区视频观看| 99视频一区| 日韩视频三区| 一区二区激情| 日韩视频精品| 中文有码久久| 亚洲综合视频在线| 午夜一区在线| 久久久久久久激情视频| 久久综合久久综合九色| 久久综合激情| 欧美r片在线| 欧美日韩在线一区| 国产精品女人久久久久久| 国产精品美女久久福利网站| 国产乱肥老妇国产一区二| 国产精品专区h在线观看| 国产女主播视频一区二区| 国产一区自拍视频| 1024精品一区二区三区| 亚洲另类在线一区| 亚洲影音先锋| 久久乐国产精品| 欧美激情片在线观看| 亚洲美女在线观看| 亚洲综合视频一区| 久久婷婷综合激情| 欧美喷潮久久久xxxxx| 国产精品久久国产精品99gif| 国产精品一卡二卡| 亚洲国产精品久久精品怡红院| 日韩视频永久免费观看| 亚洲一区二区三区精品在线观看| 欧美一区二区精品| 欧美成人日韩| 亚洲婷婷在线| 久久在线播放| 欧美日韩中文另类| 国产综合精品一区| 99一区二区| 久久亚洲综合| 一本大道av伊人久久综合| 欧美永久精品| 欧美日韩日日骚| 尤物99国产成人精品视频| 亚洲深夜福利视频| 久久先锋影音| 宅男精品视频| 免费欧美日韩国产三级电影| 国产精品日韩在线播放| 亚洲日韩欧美视频| 久久福利电影| 99这里只有久久精品视频| 久久久水蜜桃| 国产伦精品一区二区三区高清| 亚洲人成在线观看| 久久视频在线看| 亚洲图片欧美日产| 欧美激情综合五月色丁香| 国内成+人亚洲| 亚洲直播在线一区| 亚洲经典视频在线观看| 久久国产手机看片| 国产精品综合久久久| 亚洲精品在线三区| 久久综合中文字幕| 亚洲欧美日韩一区二区| 欧美日韩亚洲一区二区| 亚洲国产日韩一区| 麻豆成人综合网| 欧美在线视频在线播放完整版免费观看 | 亚洲精品一区二区三区婷婷月 | 性8sex亚洲区入口| 欧美日韩在线免费| 99国产一区| 欧美激情导航| 久久久噜久噜久久综合| 国产在线国偷精品产拍免费yy| 亚洲一区二区精品| 亚洲精品中文字| 欧美激情一区二区三级高清视频 | 欧美成人影音| 久久久久一区二区三区| 国产综合色在线视频区| 欧美在线关看| 午夜在线一区| 国产日韩欧美一二三区| 欧美一区二区成人| 亚洲一区二区三区精品视频| 欧美日韩一区在线播放| 中文av一区二区| 99精品视频免费| 欧美视频一区二区三区在线观看 | 在线视频欧美精品| 国产精品v欧美精品v日韩精品| 正在播放欧美视频| 99在线观看免费视频精品观看|