Results 1 to 9 of 9

Thread: Show/Hide window problems

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    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.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  8. #8
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Woo..So you always drink before coding Parskie?
    Baaaaaaaaah

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width