Thread: Can't turn off a console close button

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    Can't turn off a console close button

    I had been using the following code to disable the close button of the console window:
    Code:
    DeleteMenu(GetSystemMenu(hwndConsole, FALSE), SC_CLOSE , MF_BYCOMMAND);
    
    DrawMenuBar(hwndConsole);
    The problem is, I recently strarted using WindowBlinds (which is a neat app that let's you skin windows to look however you want), and this code quit working. Does anyone know an alternate soulution that wouldn't have this issue?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can try inserting your own WNDPROC hook and intercepting the close messages.

    gg

  3. #3
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Ok, here's the code I used to attempt your suggestion:
    Code:
    //I installed the hook with this:
    
    SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) ConsoleProc, NULL, GetCurrentThreadId());
    
    //Here is the code for my hook process:
    
    LRESULT CALLBACK ConsoleProc(int code, WPARAM wParam, LPARAM lParam)
    {
    	MSG * messagePtr = (MSG *) &lParam;
    
    	if(code < 0)
    	{
    		CallNextHookEx(consoleWindowHook, code, wParam, lParam);
    	}
    
    	if(messagePtr->hwnd == consoleWindow)
    	{
    		switch(messagePtr->message)
    		{
    
    		case WM_CLOSE:
    			return TRUE;
    			break;
    		
    		default:
    			return FALSE;
    			break;
    
    		}
    	}
    
    	return FALSE;
    
    }
    This doesn't work though, and I'm having trouble figuring out why. Could someone please explain?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Actually, I was refering to:

    oldProc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)ConsoleProc);

    Inside your ConsoleProc, if it's not the message you're looking for, pass it along to the old proc via CallWindowProc().

    Give that a go.

    gg

  5. #5
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    No, that didn't work either. Here's what I tried:
    Code:
    //Set the new window process
    
    consoleOldProc = (WNDPROC)SetWindowLongPtr(consoleWindow, GWLP_WNDPROC, (LONG_PTR)ConsoleProc);
    
    //Here's the processes code:
    
    BOOL ConsoleProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    
    	switch(uMsg)
    	{
    
    	case WM_CLOSE:
    		return TRUE;
    		break;
    		
    	default:
    		CallWindowProc(consoleOldProc,
    					   hWnd,
    					   uMsg,
    					   wParam,
    					   lParam);
    		return TRUE;
    		break;
    
    	}
    
    	return FALSE;
    
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    Interestingly though, the code for the custom process I wrote never seems to actually get executed...

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> the code for the custom process I wrote never seems to actually get executed...

    Which implies that your SetWindowsLong call is failing. What is the return value, and if zero, what does GetLastError() tell you?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Doh, why didn't I think of that? Ok, GetLastError() is returning Invalid Index right after SetWindowLongPtr() which I guess means that the console doesn't allow it's window procedure to be set through this method. Err... ok, so anyone have any other ideas?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, looks like that method won't work after all....

    gg

  9. #9
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    One thing I've noticed regarding the console window, is that windows doesn't like it when you try and mess with it. For the most part it limits you to using built in functions interact with it, and even then it doesn't always work the way it's supposed to. Well, I give up on this one. You can still post a way to do it if you know how, but I'm not gonna hold my breath for anwsers.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    It is possible to subclass a console window. It is, however, fraught with problems. In my Console tutorials , part 7 did exactly that, but I've had so many people have so many problems, I have taken part 7 off for the moment.

    I'll have a look for the old stuff.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close console without hitting enter
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2009, 08:32 AM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 11:32 AM
  4. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  5. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM