|
-
Aug 3rd, 2001, 08:26 PM
#1
Thread Starter
Fanatic Member
Show/Hide window problems
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:
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;
}
Alcohol & calculus don't mix.
Never drink & derive.
-
Aug 4th, 2001, 01:56 AM
#2
Monday Morning Lunatic
Code:
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:
Code:
string sName;
cout << "Type in the window name: " << endl;
cin.readline(sName);
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 4th, 2001, 11:29 AM
#3
Thread Starter
Fanatic Member
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?
Alcohol & calculus don't mix.
Never drink & derive.
-
Aug 4th, 2001, 11:33 AM
#4
Monday Morning Lunatic
Code:
getline(cin, sName);
// ...
FindWindow(sName.c_str(), ...);
Oops...I really shouldn't drink before coding
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 4th, 2001, 11:39 AM
#5
transcendental analytic
So, it's not the monitor's fault this time?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 4th, 2001, 11:42 AM
#6
Monday Morning Lunatic
Nope Totally my fault 
Although that problem could have been solved with a simple search on Google but oh well...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 4th, 2001, 11:49 AM
#7
Thread Starter
Fanatic Member
Thanks parksie, it works now 
Code:
#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;
}
Alcohol & calculus don't mix.
Never drink & derive.
-
Aug 8th, 2001, 07:24 PM
#8
PowerPoster
Woo..So you always drink before coding Parskie?
-
Aug 9th, 2001, 02:08 AM
#9
Monday Morning Lunatic
No, I drink full stop
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|