Detecting Window class.

This is a discussion on Detecting Window class. within the Windows Programming forums, part of the Platform Specific Boards category; I tried this: Code: if(hWindow == "Button"){ } else if(hWindow == "Edit"){ } else if(hWindow == "Static"){ } else { ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Post Detecting Window class.

    I tried this:

    Code:
    if(hWindow == "Button"){ }
    else if(hWindow == "Edit"){ }
    else if(hWindow == "Static"){ }
    else { }
    But it doesn't work.
    Why?
    And how could I succesfully make my prog. read what type of Window it is?

    Thanks, August.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    GetClassInfoEx()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    GetClassName will get you the window class name.

    GetClassInfoEx requires the actual window class name itself as a parameter.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    he knew the names,he wanted to query system classes.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You're right - thanks for clarifying.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Like this: (?)

    Code:
    GetClassName(hWindow, tClassName, 8);
    if(tClassName == "Button"){ }

  7. #7
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Not exactly. I believe for character array comparisons, you should use one of the strcmp functions.

    Code:
    if( strcmp(tClassName,"Button")==0 ){ }

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Still doesn't work.

  9. #9
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Post your code then.

    Edit: I'll post a working version.

    Code:
    /*------------------------------------------------------------------------------
    GetClassName Demo 
      
    Author: Andrew Lim
    ------------------------------------------------------------------------------*/
    #include <windows.h>
    
    HWND button ;
    
    LRESULT CALLBACK WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam )
    {
      switch(msg)  
      {
        case WM_CREATE:
          {
            button = CreateWindow( "bUTtOn", "Click me!", WS_CHILD|WS_VISIBLE,
                                   10, 10, 100, 22, hwnd, 0, GetModuleHandle(0),
                                   0 ); 
                                   
            return 0 ;                               
          }
          
        case WM_COMMAND:
          {
            HWND control = (HWND) lParam ;
            
            if ( control == button )
            {
              TCHAR buffer[256];
              GetClassName( button, buffer, 256 );
              
              if ( !strcmp(buffer,"Button") )
              {
                MessageBox( hwnd, buffer, "Button Class Name", 0 );
              }
            }
            
            return 0 ;
          }
        
        case WM_DESTROY:
          {
            PostQuitMessage(0);
            return 0;
          }
          
        default: return DefWindowProc(hwnd,msg,wParam,lParam);
      }
    }
    
    int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow )
    {
      WNDCLASS wc = {0};
      wc.lpszClassName = TEXT( "GetClassNameDemo" );
      wc.hInstance     = hInst ;
      wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
      wc.lpfnWndProc   = WndProc ;
      wc.hCursor       = LoadCursor(0,IDC_ARROW);
      
      RegisterClass(&wc);
      CreateWindow( wc.lpszClassName,TEXT("GetClassName Demo"),
                    WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                    0,0,300,200,0,0,hInst,0);
      
      MSG  msg ;  
      while( GetMessage(&msg,0,0,0) > 0 ) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int)msg.wParam;
    }
    Last edited by Dante Shamest; 10-08-2005 at 10:37 AM.

  10. #10
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thanks, I see my problem was that I was using a char var instead of a TCHAR var.

  11. #11
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    It should still work with a char array, as long as UNICODE isn't defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 03:22 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Window Wrapper Class, WM_NCCREATE never sent
    By bennyandthejets in forum Windows Programming
    Replies: 5
    Last Post: 04-18-2004, 07:02 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21