help with messy SetWindowText

hi im trying to change a windows text from a dll that is injected to a program.
what this dose is when you click the + key it adds +1 to the count
and puts it in the title bar.
i just think that my code is really messy way to do it and would like someone to help me thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
		if(GetAsyncKeyState(VK_OEM_PLUS)&1){
			Count += 1;
			if(FindWindow(NULL,"MyProgram")){
				char buffer [33];
				char str1[20];
				strcpy (str1,"MyProgram   ");
				_itoa_s(MineCount,buffer,10);
				strncat (str1, buffer,2);
				strncat (str1, " - Count",8);
				HWND hWnd=FindWindow(NULL,"MyProgram");
				SetWindowText(hWnd,str1);}
			else{
				char buffer2 [33];
				char buffer3 [33];
				char str1[20];
				char str2[20];
				strcpy (str1,"MyProgram   ");
				strcpy (str2,"MyProgram   ");
				_itoa_s(MineCount-1,buffer2,10);
				strncat (str1, buffer2, 2);
				strncat (str1, " - Count",8);
				HWND hWnd=FindWindow(NULL,str1);
				_itoa_s(MineCount,buffer3,10);
				strncat (str2, buffer3,2);
				strncat (str2, " - Count",8);
				SetWindowText(hWnd,str2);
			}
		}
anyone?
1
2
3
4
5
6
7
8
9
10
11
char buf[64];
HWND hWnd= FindWindow(NULL,"MyProgram"))
if (hWnd)
{
    _snprintf(buf, sizeof(buf), "MyProgram   %2.2d - Count", MineCount);
    SetWindowText(hWnd, buf);
}
else
{
    //... as above
}
what do you mean as above?

this works once but dont keep counting as i click the key
I mean use sprintf to format the string and don't repeat the call to FindWindow.
yeah but you need to call FindWindow. to get the HWND
SetWindowText needs it?
Sorry, my mistake. You call it, but you must check for NULL.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char buf[64];
HWND hWnd = FindWindow(NULL,"MyProgram"))
if (hWnd)
{
    _snprintf(buf, sizeof(buf), "MyProgram   %2.2d - Count", MineCount);
    SetWindowText(hWnd, buf);
}
else
{
    _snprintf(buf, sizeof(buf), "MyProgram   %2.2d - Count", MineCount - 1);
    hWnd = FindWindow(NULL, buf);
    if (hWnd)
    {
        _snprintf(buf, sizeof(buf), "MyProgram   %2.2d - Count", MineCount);
        SetWindowText(hWnd, buf);
    }
}
Last edited on
Topic archived. No new replies allowed.