顯示具有 Console 標籤的文章。 顯示所有文章
顯示具有 Console 標籤的文章。 顯示所有文章

2011年6月18日 星期六

Hide console window 隱藏控制台視窗

隱藏console視窗

會閃一下,沒辦法一開始就隱藏


#include <windows.h>
void HideWindow(void)
{
    const int BUFSIZE = 1024;
    HWND m_hWnd;
    char *pszTitle;
    pszTitle = (char*)calloc( BUFSIZE, sizeof(char) );

    GetConsoleTitle( pszTitle, BUFSIZE );
    m_hWnd = FindWindow( "ConsoleWindowClass", pszTitle );
    ShowWindow( m_hWnd, SW_HIDE );
}

int main()
{
    HideWindow();
   
    /* Someing you want to do */
   
    return 0;
}

Visual C++ MFC視窗模式下呼叫gnuplot

研究這的目的,是要在Visual C++視窗模式下呼叫gnuplot

也順便解決了呼叫新版gnuplot會出現的問題:
Timeout: gnuplot is not ready


原本只會用C的popen()來呼叫gnuplot,或用Visual C++的_popen()。
這種方法在console(控制台,黑黑的視窗)模式下是可行的。
但在VC++的視窗模式下,是無法使用_popen()的。

因為popen()是Unix系統的pipe function,Windows系統雖有模擬,但使用上有些限制( ex. 視窗程式不能用 )

在Windows下建pipe

在Windows下建pipe

compiler是mingw32-gcc,可用popen。

compiler是VC++,console(控制台,黑黑的視窗)模式下,要用_popen。
但視窗模式下無效,也不會顯示錯誤訊息。

因為popen是Unix的東西,Windows雖有模擬,但使用上有些限制( ex. 視窗程式不能用 )。


Windows有提供它的 Pipe Functions:

Pipe Functions
http://msdn.microsoft.com/en-us/library/aa365781%28v=VS.85%29.aspx


上網一查,發現有人跟我遇到一樣的問題,而且他跟我的目的一樣,都是要呼叫gnuplot

以下只摘錄回答
===================================================================
http://www.programmer-club.com.tw/ShowSameTitleN/c/36114.html
作者 : sunny_gong(simula)
如果是使用 VC++ 的 runtime library,可以參考說明文件。C runtime library中與作業系統有關的function call,在console模式下應該都沒啥問題,但是在視窗模式中可能就有相容性的問題。其原因就是Windows並不是Unix-like系統,而C runtime library的作業系統服務呼叫就是Unix的系統呼叫,Windows是另外設計出子系統來模擬這些Unix系統呼叫的,例如console子系統與POSIX子系統。所以在Windows上面使用C runtime library的作業系統服務呼叫,都得閱讀一下說明文件,看看有沒什麼差異或限制,或者乾脆用Windows本身的系統呼叫。我只知道個大概,Inside Windows NT之類的書籍有更詳細的說明。

[MSDN Library\Run-Time Library Reference\_popen]
if used in a Windows program, the _popen function returns an invalid file pointer that causes the program to stop responding indefinitely. _popen works properly in a console application. To create a Windows application that redirects input and output, see Creating a Child Process with Redirected Input and Output in the Platform SDK.

Windows NT保護子系統:
http://lee-1.com/hlchou/WindowsNTSubSystem.htm
=========================================================================

2011年4月2日 星期六

Windows系統下,用C在console輸出彩色


#include <windows.h>
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), colorCode )


colorCode: 型態是 unsigned short ?


    將colorCode以二進位制表示:

    LRGB LRGB

    前4碼是控制 background 的顏色
    後4碼是控制 foreground 的顏色

    L: Light (亮度)
    R: Red
    G: Green
    B: Blue
=======================================================================


http://en.wikipedia.org/wiki/File:AdditiveColor.svg
windows.h裡有#define一些名稱來代表數字
    FOREGROUND_INTENSITY = 0000 1000
    FOREGROUND_RED       = 0000 0100
    ......
    BACKGROUND_GREEN     = 0010 0000
    BACKGROUND_BLUE      = 0001 0000



像混三原光一樣,調整自己想要的顏色

比方說我要顯示前景亮黃色:

colorCode = 0000 1110 //這是二進位制



colorCode =  
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN


下面的code用到了macro和bitwise的技巧
================================================================================
/**ConsoleColorTest.c*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define setColor(var)\
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BinaryStringToUShort(var))

#define setColor2(var)\
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BinaryStringToUShort(#var))

unsigned short BinaryStringToUShort( char s[] );

int main( void )
{
            // back fore
            // LRGB LRGB
    setColor( "1010 1101" );
    printf("Red + Blue = Magenta.\n");
    setColor2( 0010 1001 );
    printf("Light Blue.\n");
    setColor2( 0000 0111 );
    printf("White.\n");
    system("pause");
    return 0;
}

unsigned short BinaryStringToUShort( char s[] )
{
    unsigned short i, sum;

    sum = 0;
    for( i = 0; s[i] != '\0'; i++ )
    {
        if( s[i]==' ' ) // skip space
            continue;

        if( s[i]=='1' )
            ++sum;

        sum<<=1; // sum*=2;
    }
    sum>>=1; // sum/=2;
    return sum;
}
======================================================================

Output: