Page 1 of 1

Microsoft : Applications With Windows And Console You can create an application that creates windows and have a console Rate Topic: -----

#1 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Posted 01 April 2010 - 09:46 AM

Microsoft : Applications With Windows And Console

Introduction

You can create and use a windows console in a windows application. This tutorial shows you how.

Creating a Console

We need to start with our base code that creates a simple frame window as provided below.

#include <windows.h>  
#include <CommCtrl.h> 
#include <ctime>  
#include <cstdlib>  
using namespace std;

#pragma comment(lib, "comctl32.lib")

class frame_window {
    private:
        LPWSTR window_class_name;  
        HINSTANCE instance_handle;  
        HCURSOR cursor_arrow;  
        HWND window_handle;
        HWND header_handle;
        RECT client_rectangle;
     public:
        frame_window(LPWSTR window_class_identity) : window_class_name(window_class_identity) {         
            int screen_width = GetSystemMetrics(SM_CXFULLSCREEN);  
            int screen_height = GetSystemMetrics(SM_CYFULLSCREEN);  
            instance_handle = GetModuleHandle(NULL);  
               
            WNDCLASS window_class = { CS_OWNDC, main_window_proc, 0, 0,    
                                      instance_handle, NULL,    
                                      NULL, NULL, NULL,    
                                      window_class_name };   
            
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
            // Create a standard frame window   
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
            
            RegisterClass(&window_class);   
            window_handle = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,    
                                           window_class_name,    
                                           L"Console Window Example",   
                                           WS_OVERLAPPEDWINDOW | 
                                           WS_CLIPCHILDREN | WS_CLIPSIBLINGS,   
                                           (screen_width-900)/2, (screen_height-700)/2, 
                                           900, 700, 
                                           NULL, NULL, instance_handle, NULL);
            SetWindowLongPtr(window_handle, GWL_USERDATA, (LONG)this);  
            GetClientRect(window_handle, &client_rectangle);
            int width = client_rectangle.right - client_rectangle.left;
            int height = client_rectangle.bottom - client_rectangle.top;
            SetCursor(LoadCursor(NULL, IDC_ARROW)); 
            ShowWindow(window_handle, SW_SHOW);   
            UpdateWindow(window_handle); 
        }  
        ~frame_window() {  
            UnregisterClass(window_class_name, instance_handle);   
        }  
        
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        // Windows message processing function   
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        static LRESULT WINAPI main_window_proc(HWND window_handle, UINT message,    
                                               WPARAM wparam, LPARAM lparam) {  
            frame_window *This = (frame_window *)GetWindowLongPtr(window_handle, GWL_USERDATA);  
            switch ( message ) {  
                case WM_PAINT:   
                {   
                    PAINTSTRUCT paint_structure;   
                    RECT client_rect;   
                    HDC paint_device_context, paint_dc;   
                    HBITMAP bitmap;   
  
                    paint_device_context = BeginPaint(window_handle, &paint_structure);   
                    paint_dc = CreateCompatibleDC(paint_device_context);   
                    GetClientRect(window_handle, &client_rect);   
                    int window_width = client_rect.right - client_rect.left;  
                    int window_height = client_rect.bottom - client_rect.top;  
                    bitmap = CreateBitmap(window_width, window_height, 1, 32, NULL);  
                    HGDIOBJ old_bitmap = SelectObject(paint_dc, bitmap);  
  
                    // Fill the client aread with the user selected face colour  
                    HBRUSH light_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));  
                    FillRect(paint_dc, &paint_structure.rcPaint, light_brush);  
                    DeleteObject(light_brush);
                    BitBlt(paint_device_context, 0, 0,    
                           client_rect.right-client_rect.left,   
                           client_rect.bottom-client_rect.top,   
                           paint_dc, 0, 0, SRCCOPY);   
                    SelectObject(paint_dc, old_bitmap);   
                    DeleteObject(bitmap);   
                    DeleteDC(paint_dc);   
                    EndPaint(window_handle, &paint_structure);   
                    return 0;   
                }   
                case WM_ERASEBKGND:   
                {   
                    return TRUE;   
                }
                case WM_SIZE:   
                {
                    InvalidateRect(window_handle, NULL, TRUE);   
                    return 0;   
                }
                case WM_CLOSE:   
                {   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    // The user wants to close the window   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
           
                    PostQuitMessage(0);   
                    return 0;   
                }   
            }   
            return DefWindowProc(window_handle, message, wparam, lparam);   
        }
          
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        // Windows message loop   
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        void run() {  
            MSG window_message;   
            while ( GetMessage(&window_message, NULL, 0, 0) ) {   
                TranslateMessage(&window_message);   
                DispatchMessage(&window_message);   
            }   
        } 
};  

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
// Windows main entry point   
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
   
int WINAPI wWinMain(HINSTANCE instance_handle, HINSTANCE, LPWSTR, INT) {  
    frame_window main_window(L"my base window");
    main_window.run();  
    return 0;   
}



Creating a console window

Now, to create a console, we create a console object class (I've called it console_window), and instanciate it. The console_window constructor allocates a console to the application by using AllocConsole function. Next, you need to get hold of the standard input and output handles. This is acheived by using the GetStdHandle(STD_INPUT_HANDLE) and GetStdHandle(STD_OUTPUT_HANDLE) functions. These calls return a handle to standard input and standard output respectively.

Reading and writing to the console

We are now in a position to write to and read from the console we have created. In the code example found at the end of this tutorial, you will notice I call a function named SetConsoleMode. This enables echo mode when the user inputs data, and I have also set the console in line mode so that the read will not return until the user presses the ENTER key.

To write to the console, you use WriteConsole and to read from the console you use ReadConsole. It should be noted that the ReadConsole function blocks until the user presses the ENTER key. To make any real use of frame windows and console windows, any console window you create should be running on it's own thread.

Code Example

The following example displays a frame window and a console window. The characters you input to the console window are then used to set the title of the frame window.

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

#pragma comment(lib, "comctl32.lib")

class console_window;

class frame_window {
    private:
        LPWSTR window_class_name;  
        HINSTANCE instance_handle;  
        HCURSOR cursor_arrow;  
        HWND window_handle;
        RECT client_rectangle;
        console_window * console;
    public:
        frame_window(LPWSTR window_class_identity);

        ~frame_window() {  
            UnregisterClass(window_class_name, instance_handle);   
        }

        void set_window_text(wchar_t * string) {
            SetWindowText(window_handle, string);
        }
        
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        // Windows message processing function   
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        static LRESULT WINAPI main_window_proc(HWND window_handle, UINT message,    
                                               WPARAM wparam, LPARAM lparam) {  
            frame_window *This = (frame_window *)GetWindowLongPtr(window_handle, GWL_USERDATA);  
            switch ( message ) {  
                case WM_PAINT:   
                {   
                    PAINTSTRUCT paint_structure;   
                    RECT client_rect;   
                    HDC paint_device_context, paint_dc;   
                    HBITMAP bitmap;   
  
                    paint_device_context = BeginPaint(window_handle, &paint_structure);   
                    paint_dc = CreateCompatibleDC(paint_device_context);   
                    GetClientRect(window_handle, &client_rect);   
                    int window_width = client_rect.right - client_rect.left;  
                    int window_height = client_rect.bottom - client_rect.top;  
                    bitmap = CreateBitmap(window_width, window_height, 1, 32, NULL);  
                    HGDIOBJ old_bitmap = SelectObject(paint_dc, bitmap);  
  
                    // Fill the client aread with the user selected face colour  
                    HBRUSH light_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));  
                    FillRect(paint_dc, &paint_structure.rcPaint, light_brush);  
                    DeleteObject(light_brush);
                    BitBlt(paint_device_context, 0, 0,    
                           client_rect.right-client_rect.left,   
                           client_rect.bottom-client_rect.top,   
                           paint_dc, 0, 0, SRCCOPY);   
                    SelectObject(paint_dc, old_bitmap);   
                    DeleteObject(bitmap);   
                    DeleteDC(paint_dc);   
                    EndPaint(window_handle, &paint_structure);   
                    return 0;   
                }   
                case WM_ERASEBKGND:   
                {   
                    return TRUE;   
                }
                case WM_SIZE:   
                {
                    InvalidateRect(window_handle, NULL, TRUE);   
                    return 0;   
                }
                case WM_CLOSE:   
                {   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    // The user wants to close the window   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
           
                    PostQuitMessage(0);   
                    return 0;   
                }   
            }   
            return DefWindowProc(window_handle, message, wparam, lparam);   
        }
          
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        // Windows message loop   
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        void run() {  
            MSG window_message;   
            while ( GetMessage(&window_message, NULL, 0, 0) ) {   
                TranslateMessage(&window_message);   
                DispatchMessage(&window_message);   
            }   
        } 
};  

class console_window {
    private:
        HINSTANCE instance_handle;
        wchar_t * window_class_name;
        HWND parent_handle;
        HANDLE output_handle;
        HANDLE input_handle;
    public:
        console_window(HINSTANCE instance, HWND parent) 
                      : instance_handle(instance), parent_handle(parent) {
            AllocConsole();
            input_handle = GetStdHandle(STD_INPUT_HANDLE);
            output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
            SetConsoleMode(input_handle, ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
            wchar_t message[] = L"Console is up and running\n\nPlease enter some characters : ";
            DWORD bytes_written, bytes_read;
            wchar_t buffer[256];
            WriteConsole(output_handle, message, wcslen(message), &bytes_written, NULL);
            ReadConsole(input_handle, buffer, sizeof(buffer), &bytes_read, NULL);
            buffer[bytes_read] = 0;
            frame_window * frame = (frame_window *)GetWindowLongPtr(parent_handle, GWL_USERDATA);
            frame->set_window_text(buffer);
        }
        ~console_window() {
            FreeConsole();
        }
};

frame_window::frame_window(LPWSTR window_class_identity) : window_class_name(window_class_identity) {         
    int screen_width = GetSystemMetrics(SM_CXFULLSCREEN);  
    int screen_height = GetSystemMetrics(SM_CYFULLSCREEN);  
    instance_handle = GetModuleHandle(NULL);  
       
    WNDCLASS window_class = { CS_OWNDC, main_window_proc, 0, 0,    
                              instance_handle, NULL,    
                              NULL, NULL, NULL,    
                              window_class_name };   
    
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
    // Create a standard frame window   
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
    
    RegisterClass(&window_class);   
    window_handle = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,    
                                   window_class_name,    
                                   L"Console Window Example",   
                                   WS_OVERLAPPEDWINDOW | 
                                   WS_CLIPCHILDREN | WS_CLIPSIBLINGS,   
                                   (screen_width-900)/2, (screen_height-700)/2, 
                                   900, 700, 
                                   NULL, NULL, instance_handle, NULL);
    SetWindowLongPtr(window_handle, GWL_USERDATA, (LONG)this);  
    GetClientRect(window_handle, &client_rectangle);
    int width = client_rectangle.right - client_rectangle.left;
    int height = client_rectangle.bottom - client_rectangle.top;
    SetCursor(LoadCursor(NULL, IDC_ARROW)); 
    ShowWindow(window_handle, SW_SHOW);   
    UpdateWindow(window_handle); 
    console = new console_window(instance_handle, window_handle);
    delete console;
}  

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
// Windows main entry point   
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
   
int WINAPI wWinMain(HINSTANCE instance_handle, HINSTANCE, LPWSTR, INT) {  
    frame_window main_window(L"my base window");
    main_window.run();  
    return 0;   
}



Is This A Good Question/Topic? 1
  • +

Page 1 of 1