|
|
Comments and Discussions
|
|
 |

|
Unfortunately this this class it won't work on w7. More, the visual scan method it will take a minute to scan the tray rect, if Aero is enabled...due to a slow call to GetPixel() on desktop's hwnd.
And here it's a fix for the visual scan technique. We drop GetPixel() and will capture the tray icon rect as an CImage and perform the visual scan on that.
please include hist 2 files: ScreenImage.h
#pragma once
#include <atlimage.h>
class CScreenImage : public CImage
{
public:
BOOL CaptureRect(const CRect& rect) throw();
BOOL CaptureScreen() throw();
BOOL CaptureWindow(HWND hWnd) throw();
};
ScreenImage.cpp
#include "StdAfx.h"
#include "ScreenImage.h"
BOOL CScreenImage::CaptureRect(const CRect& rect)
{
CImage::Destroy();
HDC hDCScreen = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
HDC hDCMem = ::CreateCompatibleDC(hDCScreen);
HBITMAP hBitmap =
::CreateCompatibleBitmap(hDCScreen, rect.Width(), rect.Height());
HBITMAP hBmpOld = (HBITMAP)::SelectObject(hDCMem, hBitmap);
DWORD dwRop = SRCCOPY | CAPTUREBLT;
BOOL bRet = ::BitBlt(hDCMem, 0, 0, rect.Width(), rect.Height(),
hDCScreen,
rect.left, rect.top, dwRop);
Attach(hBitmap);
::SelectObject(hDCMem, hBmpOld);
::DeleteDC(hDCMem);
::DeleteDC(hDCScreen);
return bRet;
}
BOOL CScreenImage::CaptureScreen()
{
CRect rect(0, 0, ::GetSystemMetrics(SM_CXSCREEN), ::GetSystemMetrics(SM_CYSCREEN));
return CaptureRect(rect);
}
BOOL CScreenImage::CaptureWindow(HWND hWnd)
{
BOOL bRet = FALSE;
if(::IsWindow(hWnd))
{
CRect rect;
::GetWindowRect(hWnd, rect);
bRet = CaptureRect(rect);
}
return bRet;
}
then in
BOOL CTrayIconPosition::FindOutPositionOfIcon(HICON icon)
capture the image from tray's rect:
m_image.CaptureRect(m_rtRectangleOfTheTray);
and modify your code like this:
m_image.CaptureRect(m_rtRectangleOfTheTray);
for(int iy = m_rtRectangleOfTheTray.Height()-3; iy > 1; iy--)
{
int iNoOfPixelsInLine=0;
for(int ix=0;ix<m_rtRectangleOfTheTray.Width();ix++)
{
COLORREF crPixel = m_image.GetPixel( ix, iy);
COLORREF crPixel2 = m_image.GetPixel( ix, iy-2);
COLORREF crPixel3 = m_image.GetPixel( ix, iy+2);
and here:
m_ptPosition.x = m_rtRectangleOfTheTray.left + ix-1;
m_ptPosition.y = m_rtRectangleOfTheTray.top +iy-6;
where:
CScreenImage m_image;
et voila, the tray rect it's working fine with aero.
At the end I found that after
Shell_NotifyIcon(NIM_MODIFY, &nid);
we must add a Sleep(100) to let the shell paint the black icon; otherwise we may not capture the black icon.
modified 9-Mar-12 1:48am.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Ever wanted to know position of your tray icon? Windows supplies no API for that. This class is a compact solution that works.
| Type | Article |
| Licence | CPOL |
| First Posted | 20 Feb 2003 |
| Views | 172,266 |
| Downloads | 4,461 |
| Bookmarked | 82 times |
|
|