c++ Having trouble about changing name on console window - c++

LPCTSTR Process = ("Test game");
LPCTSTR windowName = Process(" Text");
system("color 0a");
SetConsoleTitle(windowName);
Can anyone please make this posiblle?

To change the current console title you can use the SetConsoleTitle WinAPI function:
#include <iostream>
#include <Windows.h>
int main(){
wchar_t newTitle[255] = L"My New Console Title";
SetConsoleTitle(newTitle); // set the current console title
return 0;
}

Related

GetWindowTextA unable to get the text of ComboBox MFC C++

I'm using GetWindowTextA to get the text of ComboBox but it will be an empty string "" even the hwnd is correct.
No problem when using GetWindowTextA to get the text from other class, but it won't work for the class ComboBox. Is it something related to this that need other function to get text from ComboBox?
Thanks.
Editied:
The combobox is from a control in some other app's window
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
POINT pt;
Sleep(3000);
GetCursorPos(&pt);
HWND hWnd = WindowFromPoint(pt);
char class_name[100];
char title[100];
GetClassNameA(hWnd,class_name, sizeof(class_name));
GetWindowTextA(hWnd,title,sizeof(title));
cout <<"Window name : "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
return 0;
}
If you've added a CComboBox variable to your dialog class, using the "Add Control Variable" wizard, as described here Add a member variable, you can readily use the CComboBox methods to retrieve the text of the selected combo item, as illustrated below:
void CMFCDlgAppDlg::OnBnClickedButton1()
{
CString itemText;
m_Combo.GetLBText(m_Combo.GetCurSel(), itemText);
AfxMessageBox(itemText);
}
Adding TCHAR szBuf[100]; and using SendMessage will be done.
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
POINT pt;
Sleep(3000);
GetCursorPos(&pt);
HWND hWnd = WindowFromPoint(pt);
char class_name[100];
char title[100];
TCHAR szBuf[100];
GetClassNameA(hWnd,class_name, sizeof(class_name));
GetWindowTextA(hWnd,title,sizeof(title));
SendMessage(hWnd, WM_GETTEXT, 100, (LPARAM)szBuf);
//cout <<"Window name : "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
wcout <<"Window name : "<<szBuf<<endl;
system("PAUSE");
return 0;
}

c++ beginning to background

I make the bakcground running program.
this program is just simple [05:00 - reboot]..
I want hide the console windows..
Start from the beginning to background the console window
help.
#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
#include "time.h"
#include "shellapi.h"
using namespace std;
int main()
{
while (true) {
time_t timer = time(NULL);
struct tm t;
localtime_s(&t, &timer);
int count_1 = 0;
if (t.tm_hour == 5) {
if (t.tm_min == 00) {
if (count_1 == 0) {
count_1 = 1;
WCHAR path[260];
GetModuleFileName(NULL, path, 260);
HWND console = FindWindow(L"ConsoleWindowClass", path);
if (IsWindow(console))
ShowWindow(console, SW_HIDE); // hides the window
system("shutdown -r");
if (IsWindow(console))
ShowWindow(console, SW_SHOW); // shows the window
}
}
}
}
}
Usually you would use the WinMain entry point instead of main.
But you need to tell your compiler that you're using WinMain which is compiler dependent.
By the way, there is a simpler way to hide the console window.
ShowWindow(GetConsoleWindow(), SW_HIDE);
But be aware that using this method the console is still being created and you might see it for a fraction of a second.

Output LRESULT to console

I'm trying to output text from Notepad window to console and it's always 0.
What I'm doing wrong?
int main()
{
HWND hwnd = (HWND)0x0031019C; // Window Handler of Notepad
char szBuf[4096];
HWND hwndEdit;
LRESULT result;
hwndEdit = FindWindowEx(hwnd, NULL, L"Edit", NULL); // Class for edit box
result = SendMessage(hwndEdit, WM_GETTEXT, sizeof(szBuf) / sizeof(szBuf[0]), (LPARAM)szBuf);
cout<<"Contents: \n"<<result;
cin.get();
return 0;
}
I tried print_f, but it outputs unreadable characters:
printf( "Contents: %s\n", result, szBuf );
It looks to me like you probably have a little bit of a mismatch happening.
Based on the L"Edit", you seem to be doing a Unicode build (otherwise, you'd get an error message about not being able to convert an wchar_t const[5] to LPCSTR, and the code wouldn't compile.
If you do a Unicode build, however, WM_GETTEXT is going to write Unicode data to your buffer, so you need to prepare for and use Unicode instead of narrow characters for your buffer.
For convenience, I've modified it a little to find Notepad instead of using a hard-coded Window handle.
#include <windows.h>
#include <stdio.h>
#define elements(b) (sizeof(b)/sizeof(b[0]))
int main() {
HWND hwnd; // Window Handler of Notepad
wchar_t buf[4096]={0};
HWND hwndEdit;
LRESULT result;
hwnd=FindWindowEx(NULL, NULL, L"Notepad", NULL);
hwndEdit=FindWindowEx(hwnd, NULL, L"Edit", NULL); // Class for edit box
result = SendMessage(hwndEdit, WM_GETTEXT, elements(buf), (LPARAM)buf);
printf("%S", buf);
return 0;
}
I built with:
cl /DUNICODE whatever.cpp user32.lib
Then I did a quick test that printed out exactly the text I'd typed into Notepad. To verify the result, I then edited the text in notepad, ran it again, and it printed out the modified text.

capture mouse cursor icon c++

I wanted to get the mouse image using C++. I used the code that I got from
How to get mouse cursor icon VS c++ question. I used visual studio 2010 IDE. I created a c++ win32 project and entered that code snippet into _tmain method and I added missing structures(CURSORINFO and ICONINFO) also. after building the project, error console shows
error C2664: 'GetCursorInfo' : cannot convert parameter 1 from 'CURSORINFO *' to 'PCURSORINFO'
what is the reason for this build error. can you explain? this is the code I built.
#include "stdafx.h"
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
typedef struct _ICONINFO {
BOOL fIcon;
DWORD xHotspot;
DWORD yHotspot;
HBITMAP hbmMask;
HBITMAP hbmColor;
} ICONINFO, *PICONINFO;
typedef struct {
DWORD cbSize;
DWORD flags;
HCURSOR hCursor;
POINT ptScreenPos;
} CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
CURSORINFO cursorInfo = { 0 };
cursorInfo.cbSize = sizeof(cursorInfo);
if (::GetCursorInfo(&cursorInfo))
{
ICONINFO ii = {0};
GetIconInfo(cursorInfo.hCursor, &ii);
DeleteObject(ii.hbmColor);
DeleteObject(ii.hbmMask);
::DrawIcon(hdcMem, cursorInfo.ptScreenPos.x - ii.xHotspot, cursorInfo.ptScreenPos.y - ii.yHotspot, cursorInfo.hCursor);
}
return 0;
}
Don't redefine structs that are declared in Windows SDK headers. This is what likely causes the error.
Basically add this:
#include <Windows.h> // includes WinUser.h
unless that is already in stdafx.h. If it is, remove your own typedefs.

C++ Win 32 API: Problem With Passing window SW_HIDE

I am attempting to hide a 3rd part window on bootup of our computers. I am using the following code.
#include<windows.h>
#include <stdio.h>
int main() {
char windowName[500];
HWND window = FindWindow("WindowClassAsReportedByWindowSpy++", NULL);
//GetWindowText(window, windowName, 63);
ShowWindow(firefox,SW_HIDE);
getchar();
return 0;
}
The only problem is the window will not hide. Any ideas on why this isn't working /how I could accomplish this differently.
Most likely your program calls FindWindow before the target window is created, and so doesn't find it.
You'll need to sleep and retry the find.
You probably want to do sanity checks to make sure FindWindow is not returning NULL. Even better, call FindWindow in a loop until it doesn't return NULL.
#include <windows.h>
#include <stdio.h>
static const wchar_t g_cszFirefoxClass[] = L"firefox";
int __cdecl wmain(__in int argc, __in_ecount_z_opt(argc) wchar_t* _wargv[], __in_z_opt __wenviron[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(_wargv);
UNREFERENCED_PARAMETER(__wenviron);
HWND hWnd;
do {
hWnd = FindWindow(g_cszFirefoxClass, NULL);
Sleep(100);
} while (hWnd == NULL);
wprintf(L"[-] Firefox found! [HWND = 0x%X]\n", hWnd);
if (ShowWindow(hWnd, SW_HIDE))
{
wprintf(L"[-] Successfully hid Firefox window!\n");
return EXIT_SUCCESS;
}
else
{
fwprintf(stderr, L"[x] Failed to hide Firefox window..\n");
return EXIT_FAILURE;
}
}