CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: application?

  1. #1
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    listing application windows

    im using enumwindows to search for application windows.
    how can i tell if the window is an application window.
    Like the windows that appear in task manager.
    thanks
    Last edited by Mitsukai; September 14th, 2008 at 02:47 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: application?

    Pretty useless post title.

    Code:
    BOOL CALLBACK EnumWindowsProc(HWND hwnd, DWORD lParam)
    {
      if (::GetWindow(hwnd, GW_OWNER ) || !::IsWindowVisible(hwnd))
      {
        // not a top level window or not visible, continue enumeration
        return TRUE;
      }
    
      //
      // Do something with the application hWnd
      //
     
      return TRUE;
    }

  3. #3
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: application?

    thank you arjay... that was kinda simple.
    i have two questions:
    1. why does "Program Manager" window appear in the list?
    2. how come i only get the valid icon from explorer.exe? (im sure it comes from GetClassLong)

    Code:
    		BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
    		{
    			if(::GetWindow(hWnd, GW_OWNER) || !::IsWindowVisible(hWnd))
    				return TRUE;
    			CFMain::CAppView*  This = reinterpret_cast<CFMain::CAppView*>(lParam);
    			HICON hIcon = (HICON)::GetClassLong(hWnd, GCL_HICON);
    			int iX = This->ImageList.Add(hIcon);
    			if(iX > -1)
    			{
    				HICON hIconSm = (HICON)::GetClassLong(hWnd, GCL_HICONSM);
    				if(!hIconSm)
    					hIconSm = hIcon;
    				This->ImageListSmall.Add(hIconSm);
    			}
    			LPTSTR sText = NULL;
    			int iY = ::GetWindowTextLength(hWnd) + 1;
    			if(iY > 0)
    			{
    				sText = new TCHAR[iY];
    				::GetWindowText(hWnd, sText, iY);
    			}
    			This->InsertItem(reinterpret_cast<int>(hWnd), sText, iX);
    			if(sText)
    				delete[] sText;
    			return TRUE;
    		}

  4. #4
    Join Date
    Jan 2008
    Posts
    48

    Re: application?

    Quote Originally Posted by Mitsukai
    1. why does "Program Manager" window appear in the list?
    Because this method is wrong.
    See win32 api forums where the right method has been posted hundreds of times...

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: application?

    Quote Originally Posted by Laurentis
    Because this method is wrong.
    See win32 api forums where the right method has been posted hundreds of times...
    How about you help a brother out and post the correct solution? Referring another forum without a link isn't much help. Thanks.

  6. #6
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: application?

    The ***? ofcors i know something is wrong ya dik head, dats why i come to dese forums. U must be the smartest @ home. Thnx for yer help laurentis

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: application?

    You can filter by Window styles as well.

    Try adding the following to the filter
    Code:
    !( GetWindowLong( hWnd, GWL_STYLE ) & WS_CAPTION )

  8. #8
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: application?

    thats a good idea, i will try right away

    hmm i got some other problems now so i cant test yet T_T

    ok the "Program Manager" now is gone. by cleverly checking if the window has a caption... however what if the window has no WS_CAPTION and draws itself, it can still be an application window...

    and somehow for some windows i cannot retrieve the icon and it displays the wrong icon..
    Last edited by Mitsukai; September 14th, 2008 at 05:01 PM.

  9. #9
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: application?

    i have this code:

    Code:
    		if(::GetWindow(hWnd, GW_OWNER) || !::IsWindowVisible(hWnd)  || !(::GetWindowLong(hWnd, GWL_STYLE) & WS_CAPTION))
    			return TRUE;
    		CView*  This = reinterpret_cast<CView*>(lParam);
    		HICON hIcon = (HICON)::GetClassLong(hWnd, GCL_HICON);
    		HICON hIconSm = (HICON)::GetClassLong(hWnd, GCL_HICONSM);
    		if(!hIcon)
    			hIcon = hIconSm;
    		if(!hIconSm)
    			hIconSm = hIcon;
    		int iX = This->ImageList.Add(hIcon);
    		This->ImageListSmall.Add(hIconSm);
    		LPTSTR sText = NULL;
    		int iY = ::GetWindowTextLength(hWnd) + 1;
    		if(iY > 0)
    		{
    			sText = new TCHAR[iY];
    			::GetWindowText(hWnd, sText, iY);
    		}
    		This->InsertItem(reinterpret_cast<int>(hWnd), sText, iX);
    		if(sText)
    			delete[] sText;
    		return TRUE;
    somehow i dont get the right icon for every application... please help me X_X

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: application?

    Comment out the icon code and check if you are getting the proper top level application windows first.

  11. #11
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: application?

    yes i do get the right top level applications. the text under the icons show that

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: application?

    The way you are adding and storing the icons seems a bit suspect.

    Step through the code and make sure an icon is available for each application. The make sure the index into the image list that you store with the app is correct.

  13. #13
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: application?

    well i found out GetClassLong returns NULL for both GCL_HICON and HICONSM, i already thought of that... so there must be a diffrent way of getting the icons..

  14. #14
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: application?

    ok fixed.

    Code:
    		CView*  This = reinterpret_cast<CView*>(lParam);
    		if(::GetWindow(hWnd, GW_OWNER) || !::IsWindowVisible(hWnd)  || !(::GetWindowLong(hWnd, GWL_STYLE) & WS_CAPTION))
    			return TRUE;
    		HICON hIcon = (HICON)::SendMessage(hWnd, WM_GETICON, ICON_BIG, 0);
    		if(!hIcon)
    			hIcon = (HICON)::GetClassLong(hWnd, GCL_HICON);
    		HICON hIconSm = (HICON)::SendMessage(hWnd, WM_GETICON, ICON_SMALL, 0);
    		if(!hIconSm)
    			hIconSm = (HICON)::GetClassLong(hWnd, GCL_HICONSM);
    		int iX  = -1;
    		iX = This->ImageList.Add(hIcon ? hIcon : hIconSm);
    		This->ImageListSmall.Add(hIconSm ? hIconSm : hIcon);
    		LPTSTR sText = NULL;
    		int iY = ::GetWindowTextLength(hWnd) + 1;
    		if(iY > 0)
    		{
    			sText = new TCHAR[iY];
    			::GetWindowText(hWnd, sText, iY);
    		}
    		This->InsertItem(reinterpret_cast<int>(hWnd), sText, iX);
    		if(sText)
    			delete[] sText;
    		return TRUE;
    now i need to either prevent from my own application appearing in the list or fix the icon from my own application.... somehow my own application icon does not work and the other applciation icons do work. maybe because i put HINSTANCE off my window class to the EXE instance, and im doing doing this inside a DLL. im not sure

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: application?

    Use GetWindowThreadProcessId api inside the callback. You can compare the process id from the current window with the one from your application and exclude it from the list if it matches.

Page 1 of 2 12 LastLast

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

Featured