Getline with WinApi - Using WOFSTREAM / WIFSTREAM - C++ / WIN32 - c++

I am using Windows 10 (Visual Studio 2015).
Alright I just want to start off by saying is that I am using an edit box for the user to write a directory,appname,comboboxname then having wofstream output the directory,appname,comboboxname into a .txt file.
My problem is that some directories have spaces so when wifstream reads those spaces then cuts off the directory to early and stores the cut off version into one of the wchar_t variables.
What I need for the program to do (I am not sure if its possible) is output line by line.
Example:
C:\Program Files (x86)\Minecraft
MinecraftLauncher
Minecraft
Then use getline to read the whole line including the spaces, store that line into a variable and then get the next line. (But it also can't grab any extra white spaces that aren't in the directory otherwise it won't work) you guys probably know how directories work lol)
So you guys don't have to look that hard, I want this to occur in the case IDB_CLICK_ME.
Here is my code currently, something I am tinkering with before I decide to add it to my main application:
This is where I am trying to read the file (without whitespaces)
case WM_CREATE:
{
wchar_t testData[20] = L"Hai";
CreateWindow(L"button", L"CLICK ME", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 5, 5, 200, 25, hwnd, (HMENU)IDB_CLICK_ME, NULL, NULL);
comboBox = CreateWindow(L"combobox", L" ", WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST, 5, 50, 100, 100, hwnd, (HMENU)4, NULL, NULL);
hProgramName = CreateWindow(L"edit", L"Program Name", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 5, 100, 200, 25, hwnd, NULL, NULL, NULL);
hProgramDirectory = CreateWindow(L"edit", L"Program Directory", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 5, 130, 200, 25, hwnd, NULL, NULL, NULL);
hProgramNameComboBox = CreateWindow(L"edit", L"Name listed in ComboBox", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 5, 160, 200, 25, hwnd, NULL, NULL, NULL);
std::wifstream myfile;
myfile.open("LaunchLocations.txt");
myfile >> std::noskipws >> gameLaunchtest.directory;
myfile >> std::noskipws >> gameLaunchtest.AppName;
myfile >> std::noskipws >> gameLaunchtest.ComboBoxName;
This is where I am writing it, so far this works correctly
ComboBox_AddString(comboBox, gameLaunchtest.directory);
break;
case WM_COMMAND:
switch (LOWORD(wparam))
{
case IDB_CLICK_ME:
GetWindowText(hProgramDirectory, gameLaunchtest.directory, MAX_PATH);
GetWindowText(hProgramName, gameLaunchtest.AppName, MAX_PATH);
GetWindowText(hProgramNameComboBox, gameLaunchtest.ComboBoxName, MAX_PATH);
wofstream launchLocations;
launchLocations.open("LaunchLocations.txt");
launchLocations << gameLaunchtest.directory << endl;
launchLocations << gameLaunchtest.AppName << endl;
launchLocations << gameLaunchtest.ComboBoxName << endl;
launchLocations.close();

So the way I fixed my problem (I was told by some very helper member that I must use the wifstream getline instead of the string getline)
std::wifstream myfile;
while (myfile.getline(test[number].directory, 100))
{
myfile.getline(test[number].AppName, 100);
myfile.getline(test[number].ComboBoxName, 100);
test[number].ID;
ComboBox_AddString(comboBox, test[number].ComboBoxName);
number++;
test[number].ID = test[number].ID + 1;

Related

I can't set the desired font in Rich Edit Control (Consolas)

I managed to get custom text color in REC, but I can't make it to use the desired font (Consolas).
(EDIT) Creating REC:
LoadLibrary(TEXT("Msftedit.dll"));
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, MSFTEDIT_CLASS, L"",
WS_CHILD | WS_VISIBLE | ES_MULTILINE,
0, 0, 300, 300,
hwnd, NULL, GetModuleHandle(NULL), NULL);
-
CHARFORMAT cf = {};
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR | CFM_FACE;
cf.crTextColor = RGB(255, 0, 255);
cf.bPitchAndFamily = DEFAULT_PITCH | FF_MODERN;
memcpy(cf.szFaceName, L"Consolas", sizeof(L"Consolas"));
SendMessage(hEdit, EM_SETCHARFORMAT, NULL, (LPARAM)&cf);
I'm using
#ifndef UNICODE
#define UNICODE
#endif
Few answers that i found but didn't help me much...
Win32 : set default font and text color for rich edit,
How to set a font in rich edit 4?
Note: the REC has no text in it
This is what i get...
5 'i' and 5 '0' should have same spacing with Consolas, Right?

Getting the wrong string from a ComboBox after executing the Sendmessage function

I'm facing an issue with where I am getting the wrong string from a ComboBox after executing the Sendmessage function.
What do you think could be wrong here?
hCombo = CreateWindow(L"COMBOBOX", L"combobox",
WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_HSCROLL | WS_VSCROLL,
10, 50, 250, 500, hWnd, (HMENU)0, hInstance, NULL);
std::string text = "1 2 3 4 5 6";
SendMessage(hCombo, CB_ADDSTRING, NULL, (LPARAM)text.c_str());
The problem here is that the LPARAM cast hides your error. You are compiling with UNICODE enabled, so by default your strings should be wide (wchar_t) strings, which for string literals means prefixing them with L, ie L"1 2 3 4 5 6".
Your compiler will now tell you that the type of the text variable is wrong. That needs to be changed to std::wstring for wide strings.
c_str() returns an ANSI string. To make it work, you need do like this:
SendMessageA(hCombo, CB_ADDSTRING, NULL, (LPARAM)text.c_str());

Is it possible to change font for an edit control without affecting the other lines?

Hello I want to know if it is possible to change the font of an edit control for some lines only without affecting the remaining:
In my Edit control I have a text but I want some headlines and titles in bigger font and bold while the other lines are with smaller font.
I tried SendMessage(hEdit, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(0, true));
But it sets the whole text in the passed in font.
I thought some messing up with SelectObject(hDcEdit, hFont); But I don't know if it is correct and how.
A standard Edit Control (think, Notepad) does not support what you are looking for. It only supports one Font for the entire text.
What you are looking for is a RichEdit Control instead (think, Wordpad), and in particular its EM_SETCHARFORMAT message, which can be used to apply different formatting (including fonts, colors, etc) to different sections of text.
This is not working with the default Editcontrol, but you can use a Richeditcontrol
#include <Windows.h>
#include <CommCtrl.h>
HINSTANCE relib = LoadLibrary("riched32.dll");
if (relib == NULL) {
MessageBox(NULL, "couldn't load richedit32.dll", "", MB_ICONEXCLAMATION);
hEdit = CreateWindow(RICHEDIT_CLASS, "", WS_VISIBLE | WS_CHILD | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_VSCROLL | WS_HSCROLL, 0, 0, 200, 200, hWnd, NULL,
NULL, NULL);
Now to set the font to your Richeditcontrol use:
CHARFORMAT2 cf;
memset(&cf, 0, sizeof cf);
cf.cbSize = sizeof cf;
cf.dwMask = CFM_FACE;
wsprintf(cf.szFaceName, "Arial"); //Here you can set the fontname you wont (C:/Windows/Fonts)
SendMessage(hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);

Getting battery information for Laptop and put it in label in C++

I am doing an App for laptops Called Batterlizer,
It will get all the information about the battery, So everything was going well until I wanted to get the battery percent and other things , so here is my code:
SYSTEM_POWER_STATUS BatteryPower;
if(GetSystemPowerStatus( &BatteryPower ))
{
long unsigned int BatteryFull = BatteryPower.BatteryLifeTime;
const char BatteryFullTime[900] = {BatteryFull};
BatteryLeftText = CreateWindow("static", "Battery Life:",WS_CHILD |
WS_VISIBLE | WS_TABSTOP, 0, 0, 100, 20,hwnd, (HMENU)(501),(HINSTANCE)
GetWindowLong (hwnd, GWL_HINSTANCE), NULL);
UpdatedBatteryText = CreateWindow("static",BatteryFullTime, WS_VISIBLE |
WS_CHILD | WS_TABSTOP, 90, 0, 50, 20, hwnd, (HMENU)(501),(HINSTANCE)
GetWindowLong (hwnd, GWL_HINSTANCE),NULL);
}
the problem is , it says strange letters when it comes to BatteryFullTime,
Any Ideas guys?
Try the following:
char szBatteryLifeBuffer[900] = {0};
sprintf(szBatteryLifeBuffer, "%lu", BatteryFull);
SetWindowText(UpdatedBatteryText, szBatteryLifeBuffer);
// This also should work, too.
// SendMessage(UpdatedBatteryText, WM_SETTEXT, 0, (LPARAM)(LPTSTR)szBatteryLifeBuffer);
Get rid of
// This does not work.
const char BatteryFullTime[900] = {BatteryFull};
You're using C instead of C++, that makes it harder. In C++ :
std::string BatteryFullTime = std::to_string(BatteryFull);
No need to care about length, or remember details of the conversion function. However:
SetWindowText(UpdatedBatteryText, BatteryFullTime.c_str());
The Windows function does need a C string, but it's easy to get a C string from the C++ string.

Problem in Next Line in Edit Box in Win32C++

I'm having a problem where in displaying my output. Whenever I display the output instead of having multiple lines of output i only got single line output.
Here is my Current code:
int TextLength = GetWindowTextLength(GetEditControl) + 1;
TCHAR Text[100000];
GetWindowText(GetEditControl, Text, TextLength);
if(TextLength > 1) {
vector <string> filelist;
string path;
path = Text;
path = stripPath(path);
filelist = GetPath(path);
stringstream buffer;
copy(filelist.begin()+1, filelist.end(),ostream_iterator<string>(buffer, "\n"));
SetWindowText(EditShow,buffer.str().c_str());
for(unsigned i=0;i<=99999;i++)
{
Text[i]='\0';
}
} else
{
MessageBox(NULL, "No Text", "Error", MB_OK | MB_ICONERROR);
}
this is the property of the Edit box
EditShow = CreateWindowEx(0, TEXT("Edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL ,
60, 70, 400, 150,
GetWindow, (HMENU)ID_EDITSHOW, NULL, 0);
Try specifying "\r\n" to separate the strings instead of just "\n".