Results 1 to 3 of 3

Thread: Subclass Help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Question Subclass Help

    Can you tell why this code doesn't subclass the window?

    Code:
    #include <windows.h>
    
    #define WIN32_LEAN_AND_MEAN
    
    static HINSTANCE hInstance = NULL;
    char szClassName[] = "StartButton";
    
    HWND hwndTask;
    HWND hwndStart;
    WNDPROC wpOldStBtnProc;
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam,
    								 LPARAM lParam);
    
    LRESULT APIENTRY wpNewStBtnProc(HWND hwnd, UINT message, WPARAM wParam, 
    								LPARAM lParam);
    
    // main window procedure
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpszArgument, int nCmdShow){
    	
    	HWND hwnd;	// main window handle
        MSG messages;
        WNDCLASSEX wincl;
        wincl.hInstance = hInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure; 
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof(WNDCLASSEX);
    	wincl.hIcon = LoadIcon(NULL, IDI_WINLOGO);
        wincl.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
    	wincl.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
    	
        if(!RegisterClassEx(&wincl)) return 0;
    	
        hwnd = CreateWindowEx(0, szClassName, "Start Button", WS_OVERLAPPEDWINDOW, 
    		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    		HWND_DESKTOP, NULL,	hInstance, NULL);
    	
        ShowWindow(hwnd, nCmdShow);
        while(GetMessage(&messages, NULL, 0, 0))
    		
    		
    	{
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    
    // callback procedure
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam,
    								 LPARAM lParam){
        switch(message){
    	case WM_CREATE:
    		hwndTask = FindWindowEx(0, 0, "Shell_TrayWnd", 0);
    		hwndStart = FindWindowEx(hwndTask, 0, "Button", 0);
    		
    		if(hwndStart != NULL){
    			wpOldStBtnProc = (WNDPROC) SetWindowLong(hwndStart,
    				GWL_WNDPROC, (LONG) wpNewStBtnProc);
    		}
    		else{
    			MessageBox(NULL, "Failed to Subclass!", "Error", NULL);
    		}
    		
    		break;
    		
    	case WM_COMMAND:
            break;
    		
    	case WM_DESTROY:
    		SetWindowLong(hwndStart, GWL_WNDPROC, 
    			(LONG) wpOldStBtnProc); 
    		
    		DestroyWindow(hwnd);
            PostQuitMessage(0);
            break;
            
    	default:
            return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    
    
    // Subclass procedure 
    // IS NOT WORKING
    LRESULT APIENTRY wpNewStBtnProc(HWND hwnd, UINT message, WPARAM wParam, 
    								LPARAM lParam) 
    {
    	MessageBox(NULL, "New procedure", "Success", NULL);
    	
    	
    	return CallWindowProc(wpOldStBtnProc, hwnd, message, wParam, lParam); 
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It needs to be in a DLL, because the protected memory space of your program is separate to the shell.
    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
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Here is an example
    Attached Files Attached Files
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

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