www.pudn.com > Process_Mo18292312142004.rar > FindWnd.cpp
/*******************************************************
This file is part of Process Monitor.
Copyright (c) 2004 by Michel van Kerkhof, ( michel000@planet.nl http://home.wxs.nl/~wijk0550/ )
For more information consult the Readme file.
This program is free software; you can redistribute it
and/or modify it under the terms of the GNU
General Public License as published by the Free
Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU
General Public License along with this program;
if not, write to:
the Free Software Foundation, Inc.,
59 Temple Place,
Suite 330, Boston,
MA 02111-1307 USA
*******************************************************
If you like my work and you have a job for me please contact me at: michel000@planet.nl
*******************************************************/
#include "includes.h"
/////////////////////////////////////////////////////////////////////////////////
///
/// Get Styles from a window and show them in the dialog
/// and set new styles if you close the dialog
/// only 1 dialog at any time
DWORD WinStyle[]= {
0x00000000L, //WS_OVERLAPPED
0x80000000L, //WS_POPUP
0x40000000L, //WS_CHILD
0x20000000L, //WS_MINIMIZE
0x10000000L, //WS_VISIBLE
0x08000000L, //WS_DISABLED
0x04000000L, //WS_CLIPSIBLINGS
0x02000000L, //WS_CLIPCHILDREN
0x01000000L, //WS_MAXIMIZE
0x00C00000L, //WS_CAPTION /* WS_BORDER | WS_DLGFRAME */
0x00800000L, //WS_BORDER
0x00400000L, //WS_DLGFRAME
0x00200000L, //WS_VSCROLL
0x00100000L, //WS_HSCROLL
0x00080000L, //WS_SYSMENU
0x00040000L, //WS_THICKFRAME
0x00020000L, //WS_GROUP
0x00010000L, //WS_TABSTOP
};
DWORD WinExStyle[]={//19
0x00000001L, //#define WS_EX_DLGMODALFRAME
0x00000004L, //#define WS_EX_NOPARENTNOTIFY
0x00000008L, //#define WS_EX_TOPMOST
0x00000010L, //#define WS_EX_ACCEPTFILES
0x00000020L, //#define WS_EX_TRANSPARENT
0x00000040L, //#define WS_EX_MDICHILD
0x00000080L, //#define WS_EX_TOOLWINDOW
0x00000100L, //#define WS_EX_WINDOWEDGE
0x00000200L, //#define WS_EX_CLIENTEDGE
0x00000400L, //#define WS_EX_CONTEXTHELP
0x00001000L, //#define WS_EX_RIGHT
0x00000000L, //#define WS_EX_LEFT default
0x00002000L, //#define WS_EX_RTLREADING
0x00000000L, //#define WS_EX_LTRREADING default
0x00004000L, //#define WS_EX_LEFTSCROLLBAR
0x00000000L, //#define WS_EX_RIGHTSCROLLBAR default
0x00010000L, //#define WS_EX_CONTROLPARENT
0x00020000L, //#define WS_EX_STATICEDGE
0x00040000L, //#define WS_EX_APPWINDOW
};
CWndInfo *CWindowInfo=NULL;
BOOL CALLBACK InfoWndDlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
CWindowInfo->WindowStartup(hWnd);
break;
case WM_CLOSE:
EndDialog(hWnd, IDCANCEL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
CWindowInfo->WindowClose(hWnd);
EndDialog(hWnd, IDOK);
break;
case IDC_BUTTON2:
EndDialog(hWnd, IDCANCEL);
break;
}
default:
return false;
}
return true;
}
/*
Creates CWndInfo dialogbox
hMain = parent window
hTarget = window to get styles for
*/
int CWndInfo::GetInfo(HWND hMain,HWND hTarget)
{
if (CWindowInfo) return -1; //allready exists
CWindowInfo=this;
//save the target
m_hTarget=hTarget;
//create dialogbox
DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG3),hMain,InfoWndDlgProc);
//unset CWindowInfo
CWindowInfo=NULL;
return 0;
}
/*
Gets Styles from hTarget and sets the correct checkboxes
hWnd = the DialogBox
*/
void ShowBits(long x)
{
int i;
long masker=1<<31;
char buf[100];
long d=x;
for(i=1;i<=32;++i) {
buf[i-1]= ((d & masker) == 0) ? '0' : '1';
d<<=1;
}
buf[i-1]=0;
ShowError(NULL,buf);
}
void CWndInfo::WindowStartup(HWND hWnd)
{
char szBuf[MAX_PATH];
//get windows text
if (SendMessage(m_hTarget,WM_GETTEXT,sizeof(szBuf),(LPARAM)szBuf))
SetDlgItemText(hWnd,IDC_EDIT1,szBuf);
/*
Get Styles
*/
DWORD dwStyle=GetWindowLong(m_hTarget,GWL_STYLE);
int i;
DWORD dwTemp=NULL;
for(i=0;i < (sizeof(WinStyle) / sizeof(DWORD));i++) {
if (dwStyle & WinStyle[i]) {
dwTemp |= WinStyle[i];
SendMessage(GetDlgItem(hWnd,i+1010),BM_SETCHECK,BST_CHECKED,0);
}
}
/*
Save styles that arent in the WinStyle array
*/
m_dwOldStyle=dwStyle & ~dwTemp;
/*
Get Extended Styles
*/
dwStyle=GetWindowLong(m_hTarget,GWL_EXSTYLE);
dwTemp=NULL;
/*
Set default values
*/
if ((dwStyle & WS_EX_RTLREADING) == 0) SendMessage(GetDlgItem(hWnd,IDC_WS_EX_LTRREADING),BM_SETCHECK,BST_CHECKED,0);
if ((dwStyle & WS_EX_RIGHT) == 0) SendMessage(GetDlgItem(hWnd,IDC_WS_EX_LEFT),BM_SETCHECK,BST_CHECKED,0);
if ((dwStyle & WS_EX_LEFTSCROLLBAR) == 0) SendMessage(GetDlgItem(hWnd,IDC_WS_EX_RIGHTSCROLLBAR),BM_SETCHECK,BST_CHECKED,0);
for(i=0;i < (sizeof(WinExStyle) / sizeof(DWORD));i++) {
if (dwStyle & WinExStyle[i]) {
dwTemp |= WinExStyle[i];
SendMessage(GetDlgItem(hWnd,i+1040),BM_SETCHECK,BST_CHECKED,0);
}
}
/*
Save Extended styles that arent in the WinExStyle array
*/
m_dwOldStyleEx = dwStyle & ~dwTemp;
}
/*
Sets Windows styles selected in the dialogbox
*/
void CWndInfo::WindowClose(HWND hWnd)
{
char szBuf[MAX_PATH];
//set text
GetDlgItemText(hWnd,IDC_EDIT1,szBuf,sizeof(szBuf));
SendMessage(m_hTarget,WM_SETTEXT,0,(LPARAM)szBuf);
/*
Set Styles
*/
DWORD dwStyle=NULL;
int i;
//read settings from dialog
for(i=0;i < (sizeof(WinStyle) / sizeof(DWORD));i++) {
if (SendMessage(GetDlgItem(hWnd,i+1010),BM_GETCHECK,0,0) == BST_CHECKED) {
dwStyle |= WinStyle[i];
}
}
//add styles that are not in the WinStyle array
dwStyle |= m_dwOldStyle;
SetWindowLong(m_hTarget,GWL_STYLE,dwStyle);
/*
Set Extended Styles
*/
dwStyle=NULL;
//read settings from dialog
for(i=0;i < (sizeof(WinExStyle) / sizeof(DWORD));i++) {
if (SendMessage(GetDlgItem(hWnd,i+1040),BM_GETCHECK,0,0) == BST_CHECKED) {
dwStyle |= WinExStyle[i];
}
}
//add styles that are not in the WinExStyle array
dwStyle |= m_dwOldStyleEx;
SetWindowLong(m_hTarget,GWL_EXSTYLE,dwStyle);
}
/////////////////////////////////////////////////////////////////////////////////////////
///
/// CFindWnd Hooks the mouse and shows info from the windows below the mouse
/// RightClick in any window to get more control
#define MY_RCLICK WM_USER + 1200
CFindWnd *CFindWindow;
HWND g_hOld; //Saves window under the mouse we only update if there is a other window below the mouse
//mouse hook callback function
LRESULT CALLBACK MouseHook(int iCode, WPARAM wParam, LPARAM lParam)
{
MSLLHOOKSTRUCT *mh;
if (iCode == HC_ACTION) {
mh=(MSLLHOOKSTRUCT *)lParam;
//show cursor position in the dialog
CFindWindow->SetCursorPos(&mh->pt);
//get window handle below the cursor
HWND hCur=WindowFromPoint(mh->pt);
if (GetAsyncKeyState(VK_LBUTTON) != 0) {
//left mouse button click
CFindWindow->LeftClick(&mh->pt);
}
else if (GetAsyncKeyState(VK_RBUTTON) != 0) {
//right mouse button click show a popup..
PostMessage(CFindWindow->m_hMain,MY_RCLICK,wParam,(LPARAM)hCur);
}
if (g_hOld != hCur) {
//window below mouse changed update info
CFindWindow->SetWindow(hCur);
g_hOld=hCur;
}
}
return CallNextHookEx(NULL,iCode,wParam,lParam);
}
BOOL CALLBACK FindWndDlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case MY_RCLICK:
CFindWindow->ShowPopup((HWND)lParam);
break;
case WM_INITDIALOG:
CFindWindow->DialogStartup(hWnd);
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1_WND://cancel button
CFindWindow->ReleaseMouseCapture();
EndDialog(hWnd, IDOK);
break;
case IDC_BUTTON2_WND:
if (!CFindWindow->bCapture) CFindWindow->SetMouseCapture();
else CFindWindow->ReleaseMouseCapture();
break;
case FIND_GETINFO:
{
CWndInfo *CWindowInfo;
CWindowInfo= new CWndInfo;
if (CWindowInfo) {
CWindowInfo->GetInfo(hWnd,CFindWindow->m_hTarget);
delete CWindowInfo;
}
break;
}
case FIND_UNHOOK:
SendMessage(GetDlgItem(hWnd,IDC_BUTTON2_WND),BM_SETCHECK,BST_UNCHECKED,0);
CFindWindow->ReleaseMouseCapture();
break;
case FIND_HIDEWND:
ShowWindow(CFindWindow->m_hTarget, SW_HIDE);
break;
case FIND_DESTROY:
PostMessage(CFindWindow->m_hTarget,WM_CLOSE,0,0);
break;
default:
break;
}
break;
}
case WM_CLOSE:
CFindWindow->ReleaseMouseCapture();
EndDialog(hWnd, IDCANCEL);
break;
default:
return false;
}
return true;
}
CFindWnd::CFindWnd(bool *bRet)
{
/*
CFindWindow is a global so we can have only one
FindWnd at a any time
*/
if (CFindWindow) {
*bRet=false;
return;
}
bCapture=false;
CFindWindow=this;
g_hOld=NULL;
m_hPopup=NULL;
m_hTarget=NULL;
*bRet=true;
}
CFindWnd::~CFindWnd(){
CFindWindow=NULL;
}
void CFindWnd::LeftClick(POINT *pt)
{
/*
if the mouse click isnt in the popup simulate a leftmouse click
in m_hMain window so it knows that it should remove the popup if there is one
*/
if (MenuItemFromPoint(m_hMain,m_hPopup,*pt) == -1) {
PostMessage(m_hMain,WM_LBUTTONDOWN,MK_LBUTTON,(LPARAM)pt);
}
}
/*
create the popup and save hCur
hCur is the window below the mouse at the time right mouse button was pressed
*/
void CFindWnd::ShowPopup(HWND hCur)
{
m_hTarget=hCur;
/*
waith because if the window with the mouse focus creates its own popup
and we dont waith this popup will be below it so not visible
*/
Sleep(400);
POINT pt;
GetCursorPos(&pt);
TrackPopupMenu(m_hPopup,TPM_LEFTALIGN | TPM_LEFTBUTTON,pt.x,pt.y,0,m_hMain,NULL);
}
//set the cursor position in the status bar
void CFindWnd::SetCursorPos(POINT *pt)
{
char szBuf[MAX_PATH];
sprintf(szBuf,"Cursor x=%i y=%i",pt->x,pt->y);
SendMessage(m_hStatus,SB_SETTEXT,(WPARAM)0 | 0,(LPARAM)szBuf);
}
//creates the dialog and adds a statusbar to it
void CFindWnd::DialogStartup(HWND hWnd)
{
m_hMain=hWnd;
m_hStatus=CreateWindowEx(0,
"msctls_statusbar32",
0,
WS_CHILD | WS_BORDER | WS_VISIBLE | SBS_SIZEGRIP,
0, 0, 0, 0,
hWnd,
0,
GetModuleHandle(NULL),
0
);
int iStatus[2];
iStatus[0]=120;
iStatus[1]=-1;
SendMessage(m_hStatus,SB_SETPARTS,2,(WPARAM)&iStatus);
SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_TOPMOST | WS_EX_OVERLAPPEDWINDOW);
SendMessage(hWnd,WM_SETICON,ICON_SMALL,(LPARAM)LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(ID_ICON_FIND)));
HMENU hPop = LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(IDM_FINDWND_POPUP));
m_hPopup=GetSubMenu(hPop,0);
}
//set window info in the dialogbox
void CFindWnd::SetWindow(HWND hWnd)
{
char szBuf[MAX_PATH];
SendMessage(hWnd,WM_GETTEXT,sizeof(szBuf),(LPARAM)szBuf);
SetDlgItemText(m_hMain,IDC_EDIT1,szBuf);
GetClassName(hWnd,szBuf,sizeof(szBuf));
SetDlgItemText(m_hMain,IDC_EDIT2,szBuf);
DWORD dwPid,dwThread;
dwThread=GetWindowThreadProcessId(hWnd,&dwPid);
HANDLE hProc=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,false,dwPid);
if (hProc) {
HandleToName(hProc,szBuf);
CloseHandle(hProc);
char *pbuf;
pbuf=strrchr(szBuf,'\\');
if (pbuf) pbuf++;
else pbuf=szBuf;
SetDlgItemText(m_hMain,IDC_EDIT3,pbuf);
}
SetDlgItemInt(m_hMain,IDC_EDIT4,dwPid,false);
SetDlgItemInt(m_hMain,IDC_EDIT5,dwThread,false);
RECT rc;
GetWindowRect(hWnd,&rc);
sprintf(szBuf,"Window Position: %i %i %i %i",rc.left,rc.top,rc.right,rc.bottom);
SendMessage(m_hStatus,SB_SETTEXT,(WPARAM)1 | 0,(LPARAM)szBuf);
}
//start mouse hook
bool CFindWnd::SetMouseCapture()
{
if (bCapture) return false;
m_hHook=SetWindowsHookEx(WH_MOUSE_LL,(HOOKPROC)MouseHook,GetModuleHandle(NULL), 0);
if (!m_hHook) return false;
SetDlgItemText(m_hMain,IDC_BUTTON2_WND,"&UnHook");
RECT pt;
GetWindowRect(m_hMain,&pt);
//set window ontop
SetWindowPos(m_hMain,HWND_TOPMOST,pt.left,pt.top,pt.right-pt.left,pt.bottom-pt.top,0);
bCapture=true;
return true;
}
//stops mouse hook
bool CFindWnd::ReleaseMouseCapture()
{
if (!bCapture) return false;
UnhookWindowsHookEx(m_hHook);
bCapture=false;
RECT pt;
GetWindowRect(m_hMain,&pt);
//remove ontop setting
SetWindowPos(m_hMain,HWND_NOTOPMOST,pt.left,pt.top,pt.right-pt.left,pt.bottom-pt.top,0);
SetDlgItemText(m_hMain,IDC_BUTTON2_WND,"&Hook");
return true;
}
void CFindWnd::StartFindWndDlg(HWND hWnd)
{
DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG2),NULL, FindWndDlgProc);
}