#include <windows.h>
#include "resource.h"
#define APP_MUTEX_NAME "SPY2PMUTEX"
#define DEFEXSTYLE_COUNT 3
#define EXSTYLE_COUNT 18
#define STYLE_COUNT 20
typedef struct tagSTYLE
{
DWORD dwStyle;
LPTSTR pStyleName;
}STYLE;
STYLE DefWndExStyle[] =
{
{WS_EX_LEFT , "WS_EX_LEFT" },
{WS_EX_LTRREADING , "WS_EX_LTRREADING" },
{WS_EX_RIGHTSCROLLBAR , "WS_EX_RIGHTSCROLLBAR" }
};
STYLE WndExStyle[] =
{
{WS_EX_ACCEPTFILES , "WS_EX_ACCEPTFILES" },
{WS_EX_APPWINDOW , "WS_EX_APPWINDOW" },
{WS_EX_CLIENTEDGE , "WS_EX_CLIENTEDGE" },
{WS_EX_CONTEXTHELP , "WS_EX_CONTEXTHELP" },
{WS_EX_CONTROLPARENT , "WS_EX_CONTROLPARENT" },
{WS_EX_DLGMODALFRAME , "WS_EX_DLGMODALFRAME" },
{WS_EX_LEFTSCROLLBAR , "WS_EX_LEFTSCROLLBAR" },
{WS_EX_LTRREADING , "WS_EX_LTRREADING" },
{WS_EX_MDICHILD , "WS_EX_MDICHILD" },
{WS_EX_NOPARENTNOTIFY , "WS_EX_NOPARENTNOTIFY" },
{WS_EX_RIGHT , "WS_EX_RIGHT" },
{WS_EX_RIGHTSCROLLBAR , "WS_EX_RIGHTSCROLLBAR" },
{WS_EX_RTLREADING , "WS_EX_RTLREADING" },
{WS_EX_STATICEDGE , "WS_EX_STATICEDGE" },
{WS_EX_TOOLWINDOW , "WS_EX_TOOLWINDOW" },
{WS_EX_TOPMOST , "WS_EX_TOPMOST" },
{WS_EX_TRANSPARENT , "WS_EX_TRANSPARENT" },
{WS_EX_WINDOWEDGE , "WS_EX_WINDOWEDGE" }
};
STYLE WndStyle[] =
{
{WS_CAPTION , "WS_CAPTION" },
{WS_BORDER , "WS_BORDER" },
{WS_CLIPCHILDREN , "WS_CLIPCHILDREN" },
{WS_CLIPSIBLINGS , "WS_CLIPSIBLINGS" },
{WS_CHILDWINDOW , "WS_CHILDWINDOW" },
{WS_DISABLED , "WS_DISABLED" },
{WS_DLGFRAME , "WS_DLGFRAME" },
{WS_GROUP , "WS_GROUP" },
{WS_HSCROLL , "WS_HSCROLL" },
{WS_ICONIC , "WS_ICONIC" },
{WS_MAXIMIZE , "WS_MAXIMIZE" },
{WS_MAXIMIZEBOX , "WS_MAXIMIZEBOX" },
{WS_MINIMIZE , "WS_MINIMIZE" },
{WS_MINIMIZEBOX , "WS_MINIMIZEBOX" },
{WS_POPUP , "WS_POPUP" },
{WS_SYSMENU , "WS_SYSMENU" },
{WS_TABSTOP , "WS_TABSTOP" },
{WS_THICKFRAME , "WS_THICKFRAME" },
{WS_VISIBLE , "WS_VISIBLE" },
{WS_VSCROLL , "WS_VSCROLL" }
};
HINSTANCE m_hInstance = NULL;
HWND m_hWndFoundWindow = NULL;
HANDLE m_hApplicationMutex = NULL;
BOOL m_bStartSearchWindow = FALSE;
HCURSOR m_hCursorSearchWindow = NULL;
HCURSOR m_hCursorPrevious = NULL;
HBITMAP m_hBitmapFinderToolFilled;
HBITMAP m_hBitmapFinderToolEmpty;
BOOL InitializeApplication(HINSTANCE hThisInst, HINSTANCE hPrevInst,
LPTSTR lpszArgs, int nWinMode)
{
BOOL bRet = FALSE;
DWORD dwLastError = 0;
m_hApplicationMutex = CreateMutex((LPSECURITY_ATTRIBUTES)NULL,
(BOOL)TRUE, (LPCTSTR)APP_MUTEX_NAME);
dwLastError = GetLastError();
if (m_hApplicationMutex == NULL)
{
bRet = FALSE;
goto InitializeApplication_0;
}
if (dwLastError == ERROR_ALREADY_EXISTS)
{
CloseHandle(m_hApplicationMutex);
m_hApplicationMutex = NULL;
bRet = FALSE;
goto InitializeApplication_0;
}
bRet = TRUE;
InitializeApplication_0:
return bRet;
}
BOOL UninitializeApplication()
{
if(m_hApplicationMutex)
{
ReleaseMutex(m_hApplicationMutex);
CloseHandle(m_hApplicationMutex);
m_hApplicationMutex = NULL;
}
return TRUE;
}
BOOL InitialiseResources()
{
BOOL bRet = FALSE;
m_hCursorSearchWindow = LoadCursor(m_hInstance,
MAKEINTRESOURCE(IDC_FIND_WINDOW));
if(m_hCursorSearchWindow == NULL)
{
bRet = FALSE;
goto InitialiseResources_0;
}
m_hBitmapFinderToolFilled = LoadBitmap(m_hInstance,
MAKEINTRESOURCE(IDB_FINDER_FILLED));
if(m_hBitmapFinderToolFilled == NULL)
{
bRet = FALSE;
goto InitialiseResources_0;
}
m_hBitmapFinderToolEmpty = LoadBitmap(m_hInstance,
MAKEINTRESOURCE(IDB_FINDER_EMPTY));
if(m_hBitmapFinderToolEmpty == NULL)
{
bRet = FALSE;
goto InitialiseResources_0;
}
bRet = TRUE;
InitialiseResources_0:
return bRet;
}
BOOL UninitialiseResources()
{
BOOL bRet = TRUE;
if(m_hBitmapFinderToolFilled)
{
DeleteObject(m_hBitmapFinderToolFilled);
m_hBitmapFinderToolFilled = NULL;
}
if(m_hBitmapFinderToolEmpty)
{
DeleteObject(m_hBitmapFinderToolEmpty);
m_hBitmapFinderToolEmpty = NULL;
}
return bRet;
}
int StartSearchWindow(HWND hDlg)
{
int nRet = 0;
HWND hWndFT = NULL;
RECT rcClient;
POINT ptScreen;
m_bStartSearchWindow = TRUE;
SendDlgItemMessage(hDlg, IDC_FINDER_TOOL, STM_SETIMAGE,
(WPARAM)IMAGE_BITMAP, (LPARAM)m_hBitmapFinderToolEmpty);
hWndFT = GetDlgItem(hDlg, IDC_FINDER_TOOL);
if(hWndFT)
{
GetWindowRect(hWndFT, &rcClient);
ptScreen.x = rcClient.left + 15;
ptScreen.y = rcClient.top + 18;
SetCursorPos(ptScreen.x, ptScreen.y);
}
if(m_hCursorSearchWindow)
m_hCursorPrevious = SetCursor(m_hCursorSearchWindow);
else
m_hCursorPrevious = NULL;
SetCapture(hDlg);
return nRet;
}
BOOL CheckWindowValidity(HWND hDlg, HWND hWndToCheck)
{
BOOL bRet = TRUE;
if (hWndToCheck == NULL)
{
bRet = FALSE;
goto CheckWindowValidity_0;
}
if (IsWindow(hWndToCheck) == FALSE)
{
bRet = FALSE;
goto CheckWindowValidity_0;
}
if(hWndToCheck == m_hWndFoundWindow)
{
bRet = FALSE;
goto CheckWindowValidity_0;
}
if(hWndToCheck == hDlg)
{
bRet = FALSE;
goto CheckWindowValidity_0;
}
if(GetParent(hWndToCheck) == hDlg)
{
bRet = FALSE;
goto CheckWindowValidity_0;
}
CheckWindowValidity_0:
return bRet;
}
void DisplayInfoOnFoundWindow(HWND hDlg, HWND hWndFoundWindow)
{
char szClassName[100];
int nMaxCount, i;
char* pCaption = NULL;
RECT rcWindow;
DWORD dwWndParent, dwWndProc, dwThreadID, dwProcessID;
DWORD dwStyle = 0, dwExStyle = 0;
char szText[256];
GetClassName(hWndFoundWindow, szClassName,
99);
nMaxCount = GetWindowTextLength(hWndFoundWindow) + 1;
if(nMaxCount)
{
pCaption = new char[nMaxCount];
GetWindowText(hWndFoundWindow, pCaption, nMaxCount);
}
GetWindowRect(hWndFoundWindow, &rcWindow);
dwWndParent = GetWindowLong(hWndFoundWindow, GWL_HWNDPARENT);
dwWndProc = GetWindowLong(hWndFoundWindow, GWL_WNDPROC);
dwThreadID = GetWindowThreadProcessId(hWndFoundWindow,
&dwProcessID);
dwStyle = GetWindowLong(hWndFoundWindow, GWL_STYLE);
dwExStyle = GetWindowLong(hWndFoundWindow, GWL_EXSTYLE);
wsprintf(szText, "%08X", hWndFoundWindow);
SetDlgItemText(hDlg, IDC_HANDLE, szText);
SetDlgItemText(hDlg, IDC_CLASS, szClassName);
if(pCaption)
{
SetDlgItemText(hDlg, IDC_CAPTION, pCaption);
delete [] pCaption;
}
wsprintf(szText, "{%d, %d, %d, %d} - %dx%d",
rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom
, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top);
SetDlgItemText(hDlg, IDC_RECT, szText);
wsprintf(szText, "%08X", dwWndParent);
SetDlgItemText(hDlg, IDC_PARENT, szText);
wsprintf(szText, "%08X", dwWndProc);
SetDlgItemText(hDlg, IDC_WNDPROC, szText);
wsprintf(szText, "%08X", dwProcessID);
SetDlgItemText(hDlg, IDC_PROCESSID, szText);
wsprintf(szText, "%08X", dwThreadID);
SetDlgItemText(hDlg, IDC_THREADID, szText);
SendDlgItemMessage(hDlg, IDC_STYLE_LIST, LB_RESETCONTENT, 0, 0);
SendDlgItemMessage(hDlg, IDC_EXSTYLE_LIST, LB_RESETCONTENT, 0, 0);
wsprintf(szText, "%08X", dwStyle);
SetDlgItemText(hDlg, IDC_STYLE, szText);
SendDlgItemMessage(hDlg, IDC_STYLE_LIST, LB_ADDSTRING,
0, (LPARAM)(LPCTSTR)"WS_OVERLAPPED");
for(i = 0; i < STYLE_COUNT; i++)
{
if(dwStyle & WndStyle[i].dwStyle)
{
SendDlgItemMessage(hDlg, IDC_STYLE_LIST, LB_ADDSTRING,
0, (LPARAM)(LPCTSTR)WndStyle[i].pStyleName);
dwStyle &= ~WndStyle[i].dwStyle;
}
}
if(dwStyle)
{
wsprintf(szText, "%08X", dwStyle);
SendDlgItemMessage(hDlg, IDC_STYLE_LIST, LB_ADDSTRING,
0, (LPARAM)(LPCTSTR)szText);
}
wsprintf(szText, "%08X", dwExStyle);
SetDlgItemText(hDlg, IDC_EXSTYLE, szText);
for(i = 0; i < DEFEXSTYLE_COUNT; i++)
SendDlgItemMessage(hDlg, IDC_EXSTYLE_LIST, LB_ADDSTRING,
0, (LPARAM)(LPCTSTR)DefWndExStyle[i].pStyleName);
for(i = 0; i < EXSTYLE_COUNT; i++)
{
if(dwExStyle & WndExStyle[i].dwStyle)
{
SendDlgItemMessage(hDlg, IDC_EXSTYLE_LIST, LB_ADDSTRING,
0, (LPARAM)(LPCTSTR)WndExStyle[i].pStyleName);
dwExStyle &= ~WndExStyle[i].dwStyle;
}
}
if(dwExStyle)
{
wsprintf(szText, "%08X", dwExStyle);
SendDlgItemMessage(hDlg, IDC_EXSTYLE_LIST, LB_ADDSTRING,
0, (LPARAM)(LPCTSTR)szText);
}
}
void HighlightRect(HDC hDC, LPCRECT pRect)
{
HBITMAP hBitmap = NULL;
HBRUSH hBrush = NULL, hOldBrush = NULL;
static WORD _dotPatternBmp[8] =
{
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff
};
hBitmap = CreateBitmap(8, 8, 1, 1, _dotPatternBmp);
hBrush = CreatePatternBrush(hBitmap);
SetBrushOrgEx(hDC, pRect->left, pRect->top, 0);
hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
PatBlt(hDC, pRect->left, pRect->top,
pRect->right - pRect->left, 3, PATINVERT);
PatBlt(hDC, pRect->left, pRect->top + 3,
3, pRect->bottom - pRect->top - 6, PATINVERT);
PatBlt(hDC, pRect->left, pRect->bottom - 3,
pRect->right - pRect->left, 3, PATINVERT);
PatBlt(hDC, pRect->right - 3, pRect->top + 3,
3, pRect->bottom - pRect->top - 6, PATINVERT);
SelectObject(hDC, hOldBrush);
DeleteObject(hBrush);
DeleteObject(hBitmap);
}
void RefreshWindow(HWND hWndToBeRefreshed)
{
HDC hWindowDC = NULL;
RECT rcWindow;
hWindowDC = GetWindowDC(hWndToBeRefreshed);
if(hWindowDC)
{
GetWindowRect(hWndToBeRefreshed, &rcWindow);
OffsetRect(&rcWindow, -rcWindow.left, -rcWindow.top);
HighlightRect(hWindowDC, &rcWindow);
ReleaseDC(hWndToBeRefreshed, hWindowDC);
}
}
void HighlightFoundWindow(HWND hDlg, HWND hWndFoundWindow)
{
HDC hWindowDC = NULL;
RECT rcWindow;
hWindowDC = GetWindowDC(hWndFoundWindow);
if(hWindowDC)
{
GetWindowRect(hWndFoundWindow, &rcWindow);
OffsetRect(&rcWindow, -rcWindow.left, -rcWindow.top);
HighlightRect(hWindowDC, &rcWindow);
ReleaseDC(hWndFoundWindow, hWindowDC);
}
}
LRESULT OnMouseMove(HWND hDlg)
{
POINT ptScreen;
HWND hWndFoundWindow = NULL;
GetCursorPos(&ptScreen);
hWndFoundWindow = WindowFromPoint(ptScreen);
if(CheckWindowValidity(hDlg, hWndFoundWindow))
{
DisplayInfoOnFoundWindow(hDlg, hWndFoundWindow);
if(m_hWndFoundWindow)
RefreshWindow(m_hWndFoundWindow);
m_hWndFoundWindow = hWndFoundWindow;
HighlightFoundWindow(hDlg, m_hWndFoundWindow);
}
return 0;
}
LRESULT OnMouseUp(HWND hDlg)
{
if (m_hCursorPrevious)
SetCursor(m_hCursorPrevious);
if (m_hWndFoundWindow)
RefreshWindow(m_hWndFoundWindow);
SendDlgItemMessage(hDlg, IDC_FINDER_TOOL, STM_SETIMAGE,
(WPARAM)IMAGE_BITMAP, (LPARAM)m_hBitmapFinderToolFilled);
ReleaseCapture();
m_bStartSearchWindow = FALSE;
return 0;
}
LRESULT CALLBACK DialogProc(HWND hDlg, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
BOOL bRet = FALSE;
switch(uMsg)
{
case WM_COMMAND:
{
WORD wID = LOWORD(wParam);
if((wID == IDC_OK) || (wID == IDC_CANCEL))
{
bRet = TRUE;
EndDialog(hDlg, wID);
}
if(wID == IDC_FINDER_TOOL)
{
bRet = TRUE;
StartSearchWindow(hDlg);
}
break;
}
case WM_MOUSEMOVE:
{
bRet = TRUE;
if(m_bStartSearchWindow)
OnMouseMove(hDlg);
break;
}
case WM_LBUTTONUP:
{
bRet = TRUE;
if(m_bStartSearchWindow)
OnMouseUp(hDlg);
break;
}
case WM_CLOSE:
{
EndDialog(hDlg, 0);
break;
}
}
return bRet;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
BOOL bRet = FALSE;
int nRet = 0;
m_hInstance = hInstance;
bRet = InitializeApplication(hInstance, hPrevInstance,
lpCmdLine, nCmdShow);
if(bRet == FALSE)
{
nRet = 0;
goto WinMain_0;
}
bRet = InitialiseResources();
if(bRet == FALSE)
{
nRet = 0;
goto WinMain_0;
}
nRet = DialogBox((HINSTANCE)m_hInstance,
(LPCTSTR)MAKEINTRESOURCE(IDD_FINDWINDOW_DIALOG),
NULL, (DLGPROC)DialogProc);
WinMain_0:
UninitializeApplication();
UninitialiseResources();
return nRet;
}