I write both gui and console program. For console I use color output like \33[0m. For gui I need to write more code, if I switch to another gui library I need to rewrite the code. Some simple library(I'm currently using) doesn't even have api customizing text color. So I try to use cmd as output for all the application.
The problem is that I cannot select text in the console, even if I set my console to QuickEdit mode as default.
The code: (if the cmd doesn't appear, please resize the main window, and then it should appear)
#include <windows.h>
#include <iostream>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
static TCHAR lpszAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = lpszAppName;
if (!RegisterClass(&wc))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
lpszAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(lpszAppName,
TEXT("The Hello Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
using namespace std;
AllocConsole();
freopen("CONOUT$", "w", stdout); // redirect std::cout to console
cout << "test console" << endl;
// get console handle
HWND console = GetConsoleWindow();
SetParent(console, hwnd);
SetWindowLong(console, GWL_STYLE, WS_CHILD | WS_VISIBLE);
// ShowWindow(console, SW_MAXIMIZE);
DWORD prev_mode;
GetConsoleMode(console, &prev_mode);
SetConsoleMode(console, prev_mode | ENABLE_QUICK_EDIT_MODE);
cout << "aaaaaaaaaa" << endl;
cout << "aaaaaaaaaa" << endl;
cout << "aaaaaaaaaa" << endl;
cout << "aaaaaaaaaa" << endl;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
How to make it selectable? And is there any other alternative console application that can be used to embed in my application?(I tried ConEmu but no luck)