Click to See Complete Forum and Search --> : Show/Hide window problems
Wynd
Aug 3rd, 2001, 08:26 PM
This code I wrote will hide or show windows that have a one-word title (such as the Run dialog), but it stops responding until I press Ctrl+C if I try to do a window with more than one word("My Documents"). Why is this happening? Here is the code:
#include <windows.h>
#include <iostream.h>
int main()
{
char* winName = new char;
cout<<"Type in the window name:"<<endl;
cin>>winName;
HWND h = FindWindow(NULL, winName);
if (h == NULL)
MessageBox(NULL, "Could not find window", "Error", MB_OK | MB_ICONERROR);
else
{
if (ShowWindow(h, SW_HIDE) == 0) // window was hidden, show it
ShowWindow(h, 1);
else //window was visible, hide it
ShowWindow(h, 0);
}
return 0;
}
parksie
Aug 4th, 2001, 01:56 AM
char* winName = new char;
cout<<"Type in the window name:"<<endl;
cin>>winName;This is why it doesn't work. You're only allocating space for one character. Perhaps:string sName;
cout << "Type in the window name: " << endl;
cin.readline(sName);
Wynd
Aug 4th, 2001, 11:29 AM
Doesn't work:
Error E2316 test.cpp 8: 'readline' is not a member of 'istream' in function main()
Error E2034 test.cpp 10: Cannot convert 'string' to 'const char *' in function main()
Error E2342 test.cpp 10: Type mismatch in parameter 'lpWindowName' (wanted 'const char *', got 'string') in function main()
I don't think I can use the string in the FindWindow function, is there a way I can convert it to what it needs?
parksie
Aug 4th, 2001, 11:33 AM
getline(cin, sName);
// ...
FindWindow(sName.c_str(), ...);Oops...I really shouldn't drink before coding :eek:
kedaman
Aug 4th, 2001, 11:39 AM
So, it's not the monitor's fault this time? :p
parksie
Aug 4th, 2001, 11:42 AM
Nope :p Totally my fault :)
Although that problem could have been solved with a simple search on Google but oh well... :rolleyes:
Wynd
Aug 4th, 2001, 11:49 AM
Thanks parksie, it works now :)
#include <windows.h>
#include <iostream.h>
int main()
{
string sName;
cout << "Type in the window name: " << endl;
getline(cin, sName);
HWND h = FindWindow(NULL, sName.c_str());
if (h == NULL)
MessageBox(NULL, "Could not find window", "Error", MB_OK | MB_ICONERROR);
else
{
if (ShowWindow(h, SW_HIDE) == 0) // window was hidden, show it
ShowWindow(h, 1);
else //window was visible, hide it
ShowWindow(h, 0);
}
return 0;
}
abdul
Aug 8th, 2001, 07:24 PM
Woo..So you always drink before coding Parskie?
parksie
Aug 9th, 2001, 02:08 AM
No, I drink full stop :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.