Printing text in different colours in the Windows CMD - c++

I am planning to make a console game in C++, kind of like Dwarf Fortress (although I don't know if it was coded in C++), but I have encountered a big problem.
I want to display different things in different colours on the screen at once. I also need to change something that has already been printed (so that I don't have to re-print everything and make the screen flash annoyingly, especially that I will want to make changes every second or so) and I found a way to do that with WriteConsoleOutput() and although I don't know yet exactly how to use it, I'll be reading into it soon.
As for the colours, I found a post here about some ANSI Escape things, but it
a) doesn't work, even when copying the linked code from github
b) I don't know if it'll work with attempts to over-write the console in specific places with WriteConsoleOutput()
So my request is: could someone please ELI5 to me what am I supposed to do and why, as well as present with a viable solution to this combination of two problems so that I can replicate something like this, with the possibilty of changing what the characters show.
Thanks in advance!

Related

How to change the value of wxDatePickerCtrl?

I have tried this, But I am not able to get date displayed on the datePickerCtrl.
Here SDOB is a wxDatePickerCtrl.
SDOB->SetValue(Rs.GetDate(3));
But I am able to get the date onto a label or text field by doing
wxStaticText11->SetLabel(Rs.GetDate(3).FormatDate());
But for my purpose I would like to get it onto the datePickerCtrl itself. Help!
Also where can I find good documentation with examples for wxWidgets? I am a beginner.
You're using the correct function for setting wxDatePickerCtrl value, so if you don't see what you expect, the date you pass to it must be invalid or otherwise different from what you think it is. Generally speaking, when asking about why something doesn't work you should explain both how do you expect it to work and what actually happens because otherwise nobody else can know what's really going on.
The documentation for this control is at https://docs.wxwidgets.org/3.1.4/classwx_date_picker_ctrl.html as could be expected and there are a hundred of examples distributed with wxWidgets, so if you want to see an example of this class in action, I recommend doing grep -lR wxDatePickerCtrl samples in the directory containing wxWidgets sources.
Good luck!

I need to keep a text static, so when I run the .exe the text won't disappear (go up) no matter how much I type

First of all, I need this just for a program than runs in console only.
So, I need a specific text to always be displayed in the console, in the same place, no matter how much other text appears below it. It can't "go up", it has to be shown always.
I don't know if there's actually a way/command, to do that. So I figured I could just clear the console (clear the screen) and show it, repeating this multiple times. But I've read in several similar questions that doing so is not actually safe or the correct way to go, and I can't find an actual answer on why it isn't safe.
So I was wondering, first why clearing the console is not safe?
And most importantly how should I approach this?
Sorry if it's not well written, I'm a little rusty with my English.
If running it in windows compiler only.

Moving cursor using code and opening folder

This is a very random and maybe a bit strange question that i thought of at 3AM. I was thinking about how code could make my day to day life easier. Every morning I wake up, open chrome to the facebook conversations with my boyfriend, and write "good morning". And thats when i thought about this hypothetical project(just out of curiosity, I wouldn't use it haha): making a code that i can just run that does all of this for me.
I could have a html file that could redirect to the facebook link(https://www.facebook.com/messages/t/boyfriend_name). But how would I go on to make the code open this file, then move the mouse to where its supposed to go (the white area where the user inputs the text) then insert the text then press send?
I'm not asking for any code help as I can imagine that is too much, but my question is: could this be achievable in C++?(This is what we've been studying at school so far). If not, what coding language should I use? Is the idea achievable without a vast knowledge in computer science? If yes, have you got any sources about opening files using C++, moving cursor etc.
Note:The OS this would happen on is Windows 10
To do what you want is possible by using AutoIT and to use it from C++ you can try AutoITX for C++. With AutoIT it's possible to detect windows, move the mouse and insert text, although a web page is like a blackbox to it, so you'll have to rely on relative pixel coordinates (it might not be very robust).

Problems with setting insertion point in a wxTextCtrl

I'm trying to implement a custom widget in wxWidgets (version 2.8.12), essentially a modified single line wxTextCtrl. It takes a text input that consists of two sets of 8-digit hexadecimal numbers separated by a space, and I want the control to automatically handle that space. For example, if the user types 1-8 in twice (no press of the space bar required), the resulting contents of the text control should be:
12345678 12345678
If the user were to then place their cursor after the first '3' and backspace three times, that space should automatically adjust as they edit, resulting in:
45678123 45678
Essentially, the space should be completely transparent to the user.
I have this, for the most part, done. The code is a bit long to paste here, so here is a Gist:
https://gist.github.com/036c1a90f77521a8653c
There is one bug, however, that I can't seem to fix- upon typing the ninth digit, it automatically inserts the space, but the cursor ends up right after the space, right behind the ninth digit.
All attempts to use wxTextCtrl::SetInsertionPoint() and wxTextCtrl::SetInsertionPointEnd() have shown no success, so I've come here as a last resort in hopes that somebody can point out what I'm doing wrong.
To assist in testing this widget out, I've prepared a very quick and dirty application containing the widget:
https://gist.github.com/cf3219eb47e2bfe85b38
Or click here to download the code as a tarball:
https://gist.github.com/gists/cf3219eb47e2bfe85b38/download
Just run make to clean, compile, and run the code all at once. You'll need wxWidgets 2.8.12 installed to compile, of course.
Mostly every other facet of this widget works exactly as I want it to- it is just this one very large problem that is keeping this from working. There is, however, one tiny bug that I'm curious about, and though it isn't a major concern I'd like to fix it some time.
The space will be automatically handled even upon deleting characters, even if you delete a selection- however, if the selection that you delete ends on the ninth digit, it won't reformat the contents of the control after the deletion.
If anybody can pinpoint the cause of that it would be appreciated, but the insertion point problem is obviously my main concern.
Thanks to anybody willing to help, I tried to provide as much information and explanation as possible- if I missed anything, please let me know.
Generally speaking, it's difficult to modify the input handling of a native control as it behaves differently under different platforms. I don't know if you tested your code under all (or at least more than one) of them already but I fully expect you to find more problems.
Instead of trying to do it on your own, I'd recommend that you look at wxMaskedEdit proposed for the addition to wxWidgets itself. It's not final yet but there is a preliminary version already and I think it could work with 2.8 too (although it's developed with 2.9, of course).

help with type of window dialog resource needed

I am writing a windows program (no mfc) and need to output a status line to the operator every few seconds or so. I tried using rich text boxes but after so many hours it seems to hang up. Does anybody have an suggestions on what I can use instead?
People mentioned that my buffers might have been exhausted. I thought I had planned for that. After I had about 1000 lines displayed I would take the first 500 and remove them using the select and cut options in rich text boxes. I still ran into the same problem.
This question appears relevant, and this one too. But they don't give any concrete recommendations for an alternative to rich text boxes.
You might try the Scintilla control (scintilla.org) which does not appear to have any hard limitations on text size. It has a permissive license. It is used by many text editors such as Notepad++, Notepad2, Code::Blocks, FlashDevelop. I haven't tried it personally but there from the documentation it looks easy to use it in a Windows API application. Of course, it might be overkill for your purposes.
If you keep appending to the text in the control every few seconds for hours then you are probably running into some memory constraint on the control or the process. I think you would have this problem with any control you choose given update frequence and how long you're running the program.
Have you considered implementing a simple circular buffer for the content of the text box? Say only keep the last hour's messages. You could maintain a separate log file for history if the operator needed to go back in time for hours.
I ended up writing my own control to do this, essentially duplicating the Output window in Visual Studio. It was a success, but it ended up being much more code than I thought it would be when I started - I insisted on features such as auto-scrolling when the cursor was on the last line, select/copy, bold text, etc. It was backed by a std::deque so I could limit the number of lines stored for the window.
Unfortunately the code belongs to a former employer so I can't share it here.