Add console to existing MFC application - c++

I'm working with 2 friends in a class project to make a D&D game. so far for the assignments I've been doing character creation stuff and strutting on the command line.
Now we're bringing or part together and I need to output ny dice rolls on a console and a few things on another one that will have to become the main view or tab or whatever it's called when it requires input/attention.
Problem is I never learned MFC yet because I didn't need it. How hard would it be to make a sample MFC console all that I can give to the teammate in charge of the GUI?
Could anyone point me to some instruction on making a console for an MFC app and how to give it output and receive output?

First, you can't. for both Unix/Linux and Windows, there is a one console/process limit. If you want another console, you have to create another process, that writes and reads the other console, while you send and receive the data.
You can use a NamedPipe http://msdn.microsoft.com/en-us/library/windows/desktop/aa365590%28v=vs.85%29.aspx to send data between processes, and the CreateProcess() function lets you create a process with a separate Console window.
Alternatively you can just write a Console-Look-a-Like window in some GUI.

Related

Expanding A Console In Windows 10 OS

Background Information: I'm developing an Windows 10 app. Within my app, I'm working with nested consoles. I'm familiar with GetSystemMetrics() and using some of it's parameters to define my console physical appearance (i.e. SM_CXBORDER, SM_CXDLGFRAME, etc).
Snip: Nested Child Console
Problem: What parameter should I look into if I want my nested child consoles (i.e. child's child console) to be resizable? My current logic output a user cmd onto this console. Overtime, the outputs accumulate. For example, if a user inputs the cmd Time 10 times then he/she will need to start scrolling through the outputs to see any previous output. In the desired scenario, the user can input the cmd Time 10 times without having to scroll which can be done by extending the console vertically. As a user, I rather extend the console than scroll through the outputs. This is purely for better visibility and less congestion.
Attempt: I tried altering DLGFRAME, DLGWINFRAME, RESIZEFRAME, and SCROLL. However, I didn't have much success.
There is no layout engine in the classic Windows API that will make your window extend size automatically
"Fit window size to text" is a feature that is implemented only in more sophisticated GUI toolkits.
If you insist on using the classic Windows API for your GUI (kind of like using stone age tools) - the only option is to calculate how big your rendered text is going to be (either assume it is always one line or use DrawText with the DT_CALCRECT flag) and extend your main window and text control by that amount.
On the whole you would be far more wise to switch to a real GUI toolkit, than wrestle with WINAPI and reinvent extremely complex wheels
BTW don't call it a console - because console is a term used to refer to Windows console terminals that use a different API - your question is confusing with existing terminology

Console App that produced a form(Windows Form)?

How can I create a windows form using a console app? Like when I run the console app it will show a form(windows form). Anyone can help me?
First, you have to understand, how windows in Windows works. Each windowed program manages something called a message queue. Messages are informations from system (and other applications), that something happened, for example someone pressed a key or moved a mouse, such that window can react to that user action properly.
Processing messages (including dispatching them to proper window) is a responsibility of the program and in Windows Forms applications is done in Application.Run method (or somewhere near that) by some internal message processing loop. But if you want to display a window in your console application, someone has to take responsibility for this process, otherwise window won't work correctly (actually, they won't work at all).
If you display a window modally, it will process messages coming to it by itself, so you can call directly myWindow.ShowDialog from your code and that will work - but will lock console input away until the window is closed. That means, you will be able to send text to console by Console.WriteLine, but you won't be able to - for instance - ask user for text by Console.ReadLine until the window is closed.
class Program
{
public static void Main(string[] args)
{
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.Click += (o, e) => { Console.WriteLine("Clicked!"); };
f.ShowDialog();
Console.ReadLine();
}
}
// (run, then click on a form to observe result)
If, on the other hand, you want to display a window in a non-dialog way, things get complicated. The simplest thing comming to my mind is to create Windows Forms application and use AllocConsole to create separate console window for application (for example Blender does that). The other option is to create two different applications - console and Windows Forms one, run the second from the first and manage their communication by some mechanism (named pipes, shared global memory, windows messages, TCP/IP etc.)
It is not directly supported no.
What you could do is have two separate applications, one form and one console. Start the console one from the form one and pass data between the applications. If you want to set the form to be "inside" the console application you can use
http://msdn.microsoft.com/en-us/library/aa984420%28v=vs.71%29.aspx

Multiple consoles for a single application C++

Is it possible to create two console windows (one being the main) and the secondary being a pop-up like a message box in Windows Forms?
I only want the secondary console window to hold IDs (that will be hard coded into the application) So the user does not have to keep returning to the main menu to check available IDs
If so how would you go about it?
Many Thanks
Yes, you can do it.
The solution is actually very simple - our process can start a new helper child-process, so the helper process will display whatever our process sends it. We can easily implement such a solution with pipes: for each new console (that I'll call logger), we'll open a pipe, and execute a Console-Helper application - the role of this application is very simple, it will print everything sent through the pipe. Check out this article Multiple consoles for a single application for details (contains the source code).
In the code, it implement a console class CConsoleLogger, then you can create multiple console windows like:
CConsoleLogger another_console;
another_console.Create("This is the first console");
another_console.printf("WOW !!! COOLL !!! another console ???");
And you will get something like:
Take a look at http://msdn.microsoft.com/en-us/library/windows/desktop/ms682528(v=vs.85).aspx for instructions for creating a console window.
Nop
A process can be associated with only one console.
AllocConsole

c++ event loop in sub application in a dll

Good day,
I have the following issue:
I have one exe application that writes text files to the disk, and that exe source is unavailable.
Customer has asked that, when users press numpad 5, a new window pops up, and does some operations with some files.
Problem is, numpad 5 + new application popup MUST only work when the application is running and has focus (they use numpad 5 for other operations).
I thought about this
create a dll with a form and buttons that do the required actions
inject the dll in the process
But I'm struggling to understand if there is a way to create a "keypress loop" in the dll.
Please note that I'm a beginner in c++ and forms, but I just need a feasibility check and a direction.
Is it possible to create a window application that waits for a keypress in an injected dll?
Is it there any simple example of this? Using google like a madman I was unable to find references to this so I think I have a problem with the proper terms.
My main issue is that the dllmain is obviously a one shot routine, and I don't understand how to create an "event loop".
Thanks for any information provided.
If you have successfully managed to inject your DLL in the traget process, use the _beginthread API in DllMain to start a new thread, and in that thread you can create a Dialog Box and have a message loop in the usal way.

Win32 application with console output and without new window

I'd like to create a tool that can either act as command line (display some console output based on input parameters), or display a window, based on input parameters.
I'm using MSV2012 with C++, and it seems you have to 'choose' between console and window app.
I know the net is filled with samples which use AllocConsole() and redirect std::out, but it doesn't make it feel like a command line application : calling the exe from the windows console will open a new window with the console output...
Is there a way to have it use the current console window instead of allocating a new one?
If not possible, I'll make 2 applications instead, but that's a pity..
Someone else may have a more authoritative answer, but I don't believe it's supported.
The usual workaround is to create a Windows app, but have a command-line wrapper that launches it from the CLI (and provides a channel for communicating with the original console).
It's not technically supported but I found a good solution by getting a snapshot for the current process, finding the parent process, attaching to it's console if it's a console app or creating one with AllocConsole, redirecting the output, getting the thread of the parent process if it's cmd.exe and suspending it, resuming it just before I exit my app