mite51
asked on
Hooking menus
I am writing a windows hook to capture some drawing messages. One problem I am having is with menus, also with scrollbars. These types of windows don't show up as children so I don't know how to access them. Is there anyway to locate HWND handles for menus and scrollbars?
Actually, the Menu doesn't have a window handle.
You can get a menu handle.
Example:
GetMenu(m_hWnd);
You can get a menu handle.
Example:
GetMenu(m_hWnd);
Here's a class that can handle windows and there menus.
//Here's the new ExternalPrgController.h file
#pragma warning(disable:4786)
#include <string>
#include <vector>
#include <map>
class WindowPath
{
public:
enum WP_SearchMethod{WP_Normal, WP_CaptionStartWithWindowN ame, WP_CaptionEndWithWindowNam e, WP_CaptionIncludesWindowNa me, WP_CaptionNotIncludeWindow Name};
WindowPath(std::string szClassName, std::string m_szWindowName="", int nIndex=0, WP_SearchMethod SearchMethod = WP_Normal)
:m_szClassName(szClassName ),m_szWind owName(m_s zWindowNam e),m_nInde x(nIndex), m_SearchMethod(SearchMetho d){};
std::string m_szClassName;
std::string m_szWindowName;
int m_nIndex;
WP_SearchMethod m_SearchMethod;
};
class Ext_Wnd
{
public:
// Ext_Wnd(HWND hWnd);
Ext_Wnd(const std::vector<WindowPath> &windowpath, HWND hwndParent = 0, HWND hwndChildAfter = 0);
Ext_Wnd(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine=NULL, LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL);
static HWND FindWindow(const std::vector<WindowPath> &windowpath, int index = 0,
HWND hwndParent = 0, HWND hwndChildAfter = 0);
// Common Windows Commands;
operator HWND() const;
void BringWindowToTop(void);
BOOL EnableWindow(BOOL bEnable);
HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName);
HWND GetLastActivePopup(void);
HMENU GetMenu(void);
#ifdef __AFX_H__
// This function gets a compile error in Win32 (non-MFC) apps
HWND GetNextWindow(UINT nFlag = GW_HWNDNEXT);
#endif //_MSC_VER
HWND GetParent(void);
LONG GetWindowLong(int nIndex);
HMENU GetSystemMenu(BOOL bRevert);
HWND GetTopWindow(void);
BOOL GetUpdateRect(LPRECT lpRect,BOOL bErase);
int GetUpdateRgn(HRGN hRgn,BOOL bErase);
HWND GetWindow(UINT uCmd);
BOOL GetWindowRect(LPRECT lpRect);
int GetWindowText(LPTSTR lpszStringBuf, int nMaxCount);
int GetWindowTextLength(void);
BOOL HiliteMenuItem(HMENU hmenu, UINT uItemHilite, UINT uHilite);
BOOL InvalidateRect(CONST RECT *lpRect, BOOL bErase);
BOOL InvalidateRgn(HRGN hRgn, BOOL bErase);
BOOL IsChild(HWND hWnd);
BOOL IsIconic(void);
BOOL IsWindowEnabled(void);
BOOL IsWindowVisible(void);
BOOL IsZoomed(void);
BOOL KillTimer(UINT uIDEvent);
BOOL MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );
BOOL PostMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
LRESULT SendMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
BOOL SendNotifyMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
HWND SetActiveWindow(void);
HWND SetCapture(void);
HWND SetFocus(void);
HWND SetParent(HWND hWndNewParent);
BOOL SetWindowPos(HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags);
int SetWindowRgn(HRGN hRgn, BOOL bRedraw );
void SetWindowText(std::string text);
void ShowWindow(int nCmdShow);
BOOL UpdateWindow();
BOOL ValidateRect(CONST RECT *lpRect);
BOOL ValidateRgn(HRGN hRgn);
// Custom Window Commands
void CloseApp(void);
static HWND StartPrgViaCreateProcess(P ROCESS_INF ORMATION &pi, const char* ProgramPath,
const char* lpCurrentDirectory, char* lpCommandLine=NULL,
LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL);
HWND StartPrgViaCreateProcess(c onst char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine=NULL,
LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL, PROCESS_INFORMATION *pi=NULL);
BOOL EnableWindow(int nIDDlgItem, BOOL bEnable);
void SetWindowEditBoxText(std:: string text);
void SendKey(char Key);
void SendBnClick(HWND hWnd_Button);
void SendBnClicked(HWND hWnd_Button);
void SendBnDBClick(HWND hWnd_Button);
UINT GetMenuCommandID(HMENU hMenu, int TierIndex, const std::vector<std::string> &SearchItem);
UINT GetMenuCommandID(const std::vector<std::string> &SearchItem);
HMENU GetMenu(HMENU hMenu, int TierIndex, const std::vector<std::string> &SearchItem, int ListSize);
HMENU GetMenu(const std::vector<std::string> &SearchItem, int ListSize = -1);
HINSTANCE GetAppHinstance(void);
//Data members
HWND m_hWnd;
};
class Ext_Menu
{// Class template by David Maisonave (david@axter.com)
public:
Ext_Menu(HMENU hMenu);
BOOL CreateMenu();
BOOL CreatePopupMenu();
// BOOL LoadMenu(LPCTSTR lpszResourceName);
// BOOL LoadMenu(UINT nIDResource);
BOOL LoadMenuIndirect(const void* lpMenuTemplate);
BOOL DestroyMenu();
// Attributes
operator HMENU() const;
BOOL operator==(const Ext_Menu& menu) const;
BOOL operator!=(const Ext_Menu& menu) const;
BOOL Attach(HMENU hMenu);
HMENU Detach();
// Ext_Menu Operations
BOOL DeleteMenu(UINT nPosition, UINT nFlags);
BOOL TrackPopupMenu(UINT nFlags, int x, int y,Ext_Wnd* pWnd, LPCRECT lpRect = 0);
// Ext_MenuItem Operations
BOOL AppendMenu(UINT nFlags, UINT nIDNewItem = 0,LPCTSTR lpszNewItem = NULL);
// BOOL AppendMenu(UINT nFlags, UINT nIDNewItem, const CBitmap* pBmp);
UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck);
UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable);
UINT GetMenuItemCount() const;
UINT GetMenuItemID(int nPos) const;
UINT GetMenuState(UINT nID, UINT nFlags) const;
int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount,UINT nFlags) const;
int GetMenuString(UINT nIDItem, std::string& rString, UINT nFlags) const;
BOOL GetMenuItemInfo(UINT nIDItem, LPMENUITEMINFO lpMenuItemInfo,BOOL fByPos = FALSE);
Ext_Menu* GetSubMenu(int nPos) const;
BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem = 0,LPCTSTR lpszNewItem = NULL);
// BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp);
BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem = 0,LPCTSTR lpszNewItem = NULL);
// BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp);
BOOL RemoveMenu(UINT nPosition, UINT nFlags);
// BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags,const CBitmap* pBmpUnchecked, const CBitmap* pBmpChecked);
BOOL CheckMenuRadioItem(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags);
// BOOL SetDefaultItem(UINT uItem, BOOL fByPos = FALSE);
// UINT GetDefaultItem(UINT gmdiFlags, BOOL fByPos = FALSE);
// Context Help Functions
BOOL SetMenuContextHelpId(DWORD dwContextHelpId);
DWORD GetMenuContextHelpId() const;
//Data members
HMENU m_hMenu;
};
typedef void (*CALLBACK_FUNCTION_TYPE1) (void);
typedef void (*CALLBACK_FUNCTION_TYPE2) (WORD);
typedef void (*CALLBACK_FUNCTION_TYPE3) (WORD, std::string);
typedef LRESULT (*CALLBACK_FUNCTION_TYPE4) (int,WPARA M,LPARAM);
typedef LRESULT (*CALLBACK_FORWARD_FUNC)(H WND, UINT, WPARAM, LPARAM);
class MainApp
{
Ext_Menu menu;
Ext_Wnd m_ApplicationMainWnd;
HINSTANCE m_hiApp;
bool m_bCloseAppInDestructor;
static std::string AppName;
static HHOOK nHook;
static LRESULT CALLBACK HookProc(int, WPARAM, LPARAM);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static CALLBACK_FORWARD_FUNC pWndProc;
static std::map<UINT, CALLBACK_FUNCTION_TYPE1> FunctionCallbackList1;
static std::map<UINT, CALLBACK_FUNCTION_TYPE2> FunctionCallbackList2;
static std::map<UINT, CALLBACK_FUNCTION_TYPE3> FunctionCallbackList3;
static std::map<UINT, CALLBACK_FUNCTION_TYPE4> FunctionCallbackList4;
public:
HWND m_hCallAppWnd;
MainApp(const std::vector<WindowPath> &windowpath, bool bCloseAppInDestructor = false);
MainApp(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine=NULL,
LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL, bool bCloseAppInDestructor = false);
~MainApp();
Ext_Wnd * GetApplicationMainWnd(void );
HINSTANCE GetApplicationHinstance(vo id);
bool AddToMainMenu(const std::vector<std::string> &NewMenuItem, CALLBACK_FUNCTION_TYPE1 f1, UINT uFlags, UINT_PTR uIDNewItem);
};
//Here's the new ExternalPrgController.h file
#pragma warning(disable:4786)
#include <string>
#include <vector>
#include <map>
class WindowPath
{
public:
enum WP_SearchMethod{WP_Normal,
WindowPath(std::string szClassName, std::string m_szWindowName="", int nIndex=0, WP_SearchMethod SearchMethod = WP_Normal)
:m_szClassName(szClassName
std::string m_szClassName;
std::string m_szWindowName;
int m_nIndex;
WP_SearchMethod m_SearchMethod;
};
class Ext_Wnd
{
public:
// Ext_Wnd(HWND hWnd);
Ext_Wnd(const std::vector<WindowPath> &windowpath, HWND hwndParent = 0, HWND hwndChildAfter = 0);
Ext_Wnd(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine=NULL, LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL);
static HWND FindWindow(const std::vector<WindowPath> &windowpath, int index = 0,
HWND hwndParent = 0, HWND hwndChildAfter = 0);
// Common Windows Commands;
operator HWND() const;
void BringWindowToTop(void);
BOOL EnableWindow(BOOL bEnable);
HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName);
HWND GetLastActivePopup(void);
HMENU GetMenu(void);
#ifdef __AFX_H__
// This function gets a compile error in Win32 (non-MFC) apps
HWND GetNextWindow(UINT nFlag = GW_HWNDNEXT);
#endif //_MSC_VER
HWND GetParent(void);
LONG GetWindowLong(int nIndex);
HMENU GetSystemMenu(BOOL bRevert);
HWND GetTopWindow(void);
BOOL GetUpdateRect(LPRECT lpRect,BOOL bErase);
int GetUpdateRgn(HRGN hRgn,BOOL bErase);
HWND GetWindow(UINT uCmd);
BOOL GetWindowRect(LPRECT lpRect);
int GetWindowText(LPTSTR lpszStringBuf, int nMaxCount);
int GetWindowTextLength(void);
BOOL HiliteMenuItem(HMENU hmenu, UINT uItemHilite, UINT uHilite);
BOOL InvalidateRect(CONST RECT *lpRect, BOOL bErase);
BOOL InvalidateRgn(HRGN hRgn, BOOL bErase);
BOOL IsChild(HWND hWnd);
BOOL IsIconic(void);
BOOL IsWindowEnabled(void);
BOOL IsWindowVisible(void);
BOOL IsZoomed(void);
BOOL KillTimer(UINT uIDEvent);
BOOL MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );
BOOL PostMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
LRESULT SendMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
BOOL SendNotifyMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0 );
HWND SetActiveWindow(void);
HWND SetCapture(void);
HWND SetFocus(void);
HWND SetParent(HWND hWndNewParent);
BOOL SetWindowPos(HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags);
int SetWindowRgn(HRGN hRgn, BOOL bRedraw );
void SetWindowText(std::string text);
void ShowWindow(int nCmdShow);
BOOL UpdateWindow();
BOOL ValidateRect(CONST RECT *lpRect);
BOOL ValidateRgn(HRGN hRgn);
// Custom Window Commands
void CloseApp(void);
static HWND StartPrgViaCreateProcess(P
const char* lpCurrentDirectory, char* lpCommandLine=NULL,
LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL);
HWND StartPrgViaCreateProcess(c
LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL, PROCESS_INFORMATION *pi=NULL);
BOOL EnableWindow(int nIDDlgItem, BOOL bEnable);
void SetWindowEditBoxText(std::
void SendKey(char Key);
void SendBnClick(HWND hWnd_Button);
void SendBnClicked(HWND hWnd_Button);
void SendBnDBClick(HWND hWnd_Button);
UINT GetMenuCommandID(HMENU hMenu, int TierIndex, const std::vector<std::string> &SearchItem);
UINT GetMenuCommandID(const std::vector<std::string> &SearchItem);
HMENU GetMenu(HMENU hMenu, int TierIndex, const std::vector<std::string> &SearchItem, int ListSize);
HMENU GetMenu(const std::vector<std::string> &SearchItem, int ListSize = -1);
HINSTANCE GetAppHinstance(void);
//Data members
HWND m_hWnd;
};
class Ext_Menu
{// Class template by David Maisonave (david@axter.com)
public:
Ext_Menu(HMENU hMenu);
BOOL CreateMenu();
BOOL CreatePopupMenu();
// BOOL LoadMenu(LPCTSTR lpszResourceName);
// BOOL LoadMenu(UINT nIDResource);
BOOL LoadMenuIndirect(const void* lpMenuTemplate);
BOOL DestroyMenu();
// Attributes
operator HMENU() const;
BOOL operator==(const Ext_Menu& menu) const;
BOOL operator!=(const Ext_Menu& menu) const;
BOOL Attach(HMENU hMenu);
HMENU Detach();
// Ext_Menu Operations
BOOL DeleteMenu(UINT nPosition, UINT nFlags);
BOOL TrackPopupMenu(UINT nFlags, int x, int y,Ext_Wnd* pWnd, LPCRECT lpRect = 0);
// Ext_MenuItem Operations
BOOL AppendMenu(UINT nFlags, UINT nIDNewItem = 0,LPCTSTR lpszNewItem = NULL);
// BOOL AppendMenu(UINT nFlags, UINT nIDNewItem, const CBitmap* pBmp);
UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck);
UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable);
UINT GetMenuItemCount() const;
UINT GetMenuItemID(int nPos) const;
UINT GetMenuState(UINT nID, UINT nFlags) const;
int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount,UINT nFlags) const;
int GetMenuString(UINT nIDItem, std::string& rString, UINT nFlags) const;
BOOL GetMenuItemInfo(UINT nIDItem, LPMENUITEMINFO lpMenuItemInfo,BOOL fByPos = FALSE);
Ext_Menu* GetSubMenu(int nPos) const;
BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem = 0,LPCTSTR lpszNewItem = NULL);
// BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp);
BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem = 0,LPCTSTR lpszNewItem = NULL);
// BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp);
BOOL RemoveMenu(UINT nPosition, UINT nFlags);
// BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags,const CBitmap* pBmpUnchecked, const CBitmap* pBmpChecked);
BOOL CheckMenuRadioItem(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags);
// BOOL SetDefaultItem(UINT uItem, BOOL fByPos = FALSE);
// UINT GetDefaultItem(UINT gmdiFlags, BOOL fByPos = FALSE);
// Context Help Functions
BOOL SetMenuContextHelpId(DWORD
DWORD GetMenuContextHelpId() const;
//Data members
HMENU m_hMenu;
};
typedef void (*CALLBACK_FUNCTION_TYPE1)
typedef void (*CALLBACK_FUNCTION_TYPE2)
typedef void (*CALLBACK_FUNCTION_TYPE3)
typedef LRESULT (*CALLBACK_FUNCTION_TYPE4)
typedef LRESULT (*CALLBACK_FORWARD_FUNC)(H
class MainApp
{
Ext_Menu menu;
Ext_Wnd m_ApplicationMainWnd;
HINSTANCE m_hiApp;
bool m_bCloseAppInDestructor;
static std::string AppName;
static HHOOK nHook;
static LRESULT CALLBACK HookProc(int, WPARAM, LPARAM);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static CALLBACK_FORWARD_FUNC pWndProc;
static std::map<UINT, CALLBACK_FUNCTION_TYPE1> FunctionCallbackList1;
static std::map<UINT, CALLBACK_FUNCTION_TYPE2> FunctionCallbackList2;
static std::map<UINT, CALLBACK_FUNCTION_TYPE3> FunctionCallbackList3;
static std::map<UINT, CALLBACK_FUNCTION_TYPE4> FunctionCallbackList4;
public:
HWND m_hCallAppWnd;
MainApp(const std::vector<WindowPath> &windowpath, bool bCloseAppInDestructor = false);
MainApp(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine=NULL,
LPVOID lpEnvironment=NULL, WORD wShowWindow=SW_NORMAL, bool bCloseAppInDestructor = false);
~MainApp();
Ext_Wnd * GetApplicationMainWnd(void
HINSTANCE GetApplicationHinstance(vo
bool AddToMainMenu(const std::vector<std::string> &NewMenuItem, CALLBACK_FUNCTION_TYPE1 f1, UINT uFlags, UINT_PTR uIDNewItem);
};
//Here's the new ExternalPrgController.cpp file
#include "stdafx.h"
#include "ExternalPrgController.h"
//#include <AFXRES.H>
//************************ ********** ********** ********** ********** ********** ********** *
//************************ ********** ********** ********** ********** ********** ********** *
Ext_Wnd::Ext_Wnd(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow)
:m_hWnd(StartPrgViaCreateP rocess(Pro gramPath, lpCurrentDirectory, lpCommandLine, lpEnvironment, wShowWindow))
{
}
Ext_Wnd::Ext_Wnd(const std::vector<WindowPath> &windowpath, HWND hwndParent, HWND hwndChildAfter)
:m_hWnd(FindWindow(windowp ath,0,hwnd Parent,hwn dChildAfte r))
{
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
// Common Windows Commands;
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
HWND Ext_Wnd::FindWindow(const std::vector<WindowPath> &windowpath, int index, HWND hwndParent,
HWND hwndChildAfter)
{
if (index >= windowpath.size()) return 0;
HWND retrnValue = NULL;
HWND hwndNextChild = hwndChildAfter;
int MemberIndex = windowpath[index].m_nIndex ;
std::string szClassName = windowpath[index].m_szClas sName.c_st r();
std::string szWindowName = (windowpath[index].m_Searc hMethod == WindowPath::WP_Normal)?
windowpath[index].m_szWind owName.c_s tr():"";
int nLenStr_WindowName = windowpath[index].m_szWind owName.siz e();
do
{
retrnValue = ::FindWindowEx(hwndParent, hwndNextCh ild,
(szClassName.size()>0)?szC lassName.c _str():NUL L,
(szWindowName.size()>0)?sz WindowName .c_str():N ULL);
if (retrnValue == NULL) return retrnValue;
hwndNextChild=retrnValue;
char szCurrentWindowName[1024];
::GetWindowText(hwndNextCh ild,szCurr entWindowN ame,sizeof (szCurrent WindowName ));
switch(windowpath[index].m _SearchMet hod)
{
case WindowPath::WP_CaptionStar tWithWindo wName:
if (!strncmp(windowpath[index ].m_szWind owName.c_s tr(),
szCurrentWindowName,nLenSt r_WindowNa me))
{
MemberIndex--;
}
break;
case WindowPath::WP_CaptionEndW ithWindowN ame:
if (nLenStr_WindowName <= strlen(szCurrentWindowName ))
{
if (!strncmp(windowpath[index ].m_szWind owName.c_s tr(),
szCurrentWindowName+strlen (szCurrent WindowName )-nLenStr_ WindowName ,nLenStr_W indowName) )
{
MemberIndex--;
}
}
break;
case WindowPath::WP_CaptionIncl udesWindow Name:
if (strstr(szCurrentWindowNam e,windowpa th[index]. m_szWindow Name.c_str ()))
{
MemberIndex--;
}
break;
case WindowPath::WP_CaptionNotI ncludeWind owName:
if (!strstr(szCurrentWindowNa me,windowp ath[index] .m_szWindo wName.c_st r()))
{
MemberIndex--;
}
break;
case WindowPath::WP_Normal:
default:
MemberIndex--;
break;
}
}while(MemberIndex>-1);
index++;
if (index < windowpath.size())
{
return FindWindow(windowpath,inde x,retrnVal ue,0);
}
return retrnValue;
}
Ext_Wnd::operator HWND() const
{
return m_hWnd;
}
void Ext_Wnd::BringWindowToTop( void)
{
::BringWindowToTop(m_hWnd) ;
}
BOOL Ext_Wnd::EnableWindow(BOOL bEnable)
{
return ::EnableWindow(m_hWnd, bEnable);
}
HWND Ext_Wnd::FindWindow(LPCTST R lpClassName, LPCTSTR lpWindowName)
{
return ::FindWindow(lpClassName,l pWindowNam e);
}
HWND Ext_Wnd::GetLastActivePopu p(void)
{
return ::GetLastActivePopup(m_hWn d);
}
HMENU Ext_Wnd::GetMenu(void)
{
return ::GetMenu(m_hWnd);
}
#ifdef __AFX_H__
// This function gets a compile error in Win32 (non-MFC) apps
HWND Ext_Wnd::GetNextWindow(UIN T nFlag)
{
return ::GetNextWindow(m_hWnd, nFlag);
}
#endif //_MSC_VER
HWND Ext_Wnd::GetParent(void)
{
return ::GetParent(m_hWnd);
}
LONG Ext_Wnd::GetWindowLong(int nIndex)
{
return ::GetWindowLong(m_hWnd, nIndex);
}
HMENU Ext_Wnd::GetSystemMenu(BOO L bRevert)
{
return ::GetSystemMenu(m_hWnd, bRevert);
}
HWND Ext_Wnd::GetTopWindow(void )
{
return ::GetTopWindow(m_hWnd);
}
BOOL Ext_Wnd::GetUpdateRect(LPR ECT lpRect,BOOL bErase)
{
return ::GetUpdateRect(m_hWnd, lpRect, bErase);
}
int Ext_Wnd::GetUpdateRgn(HRGN hRgn,BOOL bErase)
{
return ::GetUpdateRgn(m_hWnd, hRgn, bErase);
}
HWND Ext_Wnd::GetWindow(UINT uCmd)
{
return ::GetWindow(m_hWnd, uCmd);
}
BOOL Ext_Wnd::GetWindowRect(LPR ECT lpRect)
{
return ::GetWindowRect(m_hWnd, lpRect);
}
int Ext_Wnd::GetWindowText(LPT STR lpszStringBuf, int nMaxCount)
{
return SendMessage(WM_GETTEXT, (WPARAM)nMaxCount,(LPARAM) lpszString Buf);
}
int Ext_Wnd::GetWindowTextLeng th(void)
{
return SendMessage(WM_GETTEXTLENG TH, 0,0);
}
BOOL Ext_Wnd::HiliteMenuItem(HM ENU hmenu, UINT uItemHilite, UINT uHilite)
{
return ::HiliteMenuItem(m_hWnd, hmenu, uItemHilite, uHilite);
}
BOOL Ext_Wnd::InvalidateRect(CO NST RECT *lpRect, BOOL bErase)
{
return ::InvalidateRect(m_hWnd, lpRect, bErase);
}
BOOL Ext_Wnd::InvalidateRgn(HRG N hRgn, BOOL bErase)
{
return ::InvalidateRgn(m_hWnd, hRgn, bErase);
}
BOOL Ext_Wnd::IsChild(HWND hWnd)
{
return ::IsChild(m_hWnd, hWnd);
}
BOOL Ext_Wnd::IsIconic(void)
{
return ::IsIconic(m_hWnd);
}
BOOL Ext_Wnd::IsWindowEnabled(v oid)
{
return ::IsWindowEnabled(m_hWnd);
}
BOOL Ext_Wnd::IsWindowVisible(v oid)
{
return ::IsWindowVisible(m_hWnd);
}
BOOL Ext_Wnd::IsZoomed(void)
{
return ::IsZoomed(m_hWnd);
}
BOOL Ext_Wnd::KillTimer(UINT uIDEvent)
{
return ::KillTimer(m_hWnd, uIDEvent);
}
BOOL Ext_Wnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint )
{
return ::MoveWindow(m_hWnd,x,y,nW idth,nHeig ht,bRepain t);
}
BOOL Ext_Wnd::PostMessage(UINT message, WPARAM wParam , LPARAM lParam )
{
return ::PostMessage(m_hWnd, message, wParam, lParam);
}
LRESULT Ext_Wnd::SendMessage(UINT message, WPARAM wParam , LPARAM lParam )
{
return ::SendMessage(m_hWnd, message, wParam, lParam);
}
BOOL Ext_Wnd::SendNotifyMessage (UINT message, WPARAM wParam , LPARAM lParam )
{
return ::SendNotifyMessage(m_hWnd , message, wParam, lParam);
}
HWND Ext_Wnd::SetActiveWindow(v oid)
{
return ::SetActiveWindow(m_hWnd);
}
HWND Ext_Wnd::SetCapture(void)
{
return ::SetCapture(m_hWnd);
}
HWND Ext_Wnd::SetFocus(void)
{
return ::SetFocus(m_hWnd);
}
HWND Ext_Wnd::SetParent(HWND hWndNewParent)
{
return ::SetParent(m_hWnd, hWndNewParent);
}
BOOL Ext_Wnd::SetWindowPos(HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags)
{
return ::SetWindowPos(m_hWnd, hWndInsertAfter,x,y,cx,cy, nFlags);
}
int Ext_Wnd::SetWindowRgn(HRGN hRgn, BOOL bRedraw )
{
return ::SetWindowRgn(m_hWnd, hRgn, bRedraw);
}
void Ext_Wnd::SetWindowText(std ::string text)
{
::SetWindowText(m_hWnd,tex t.c_str()) ;
}
void Ext_Wnd::ShowWindow(int nCmdShow)
{
::ShowWindow(m_hWnd,nCmdSh ow);
}
BOOL Ext_Wnd::UpdateWindow()
{
return ::UpdateWindow(m_hWnd);
}
BOOL Ext_Wnd::ValidateRect(CONS T RECT *lpRect)
{
return ::ValidateRect(m_hWnd, lpRect);
}
BOOL Ext_Wnd::ValidateRgn(HRGN hRgn)
{
return ::ValidateRgn(m_hWnd, hRgn);
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
// Custom Window Commands
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
void Ext_Wnd::CloseApp(void)
{
PostMessage(WM_SYSCOMMAND, (WPARAM)SC _CLOSE,0);
}
BOOL Ext_Wnd::EnableWindow(int nIDDlgItem, BOOL bEnable)
{
HWND hWnd = ::GetDlgItem(m_hWnd,nIDDlg Item);
if (hWnd) return ::EnableWindow(hWnd, bEnable);
return FALSE;
}
void Ext_Wnd::SetWindowEditBoxT ext(std::s tring text)
{
SendMessage(WM_SETTEXT,0,( LPARAM)(LP CTSTR)text .c_str());
}
void Ext_Wnd::SendKey(char Key)
{
char data[8];
memset(data,0,sizeof(data) );
PostMessage(WM_CHAR,Key,(L PARAM)data );
}
void Ext_Wnd::SendBnClick(HWND hWnd_Button)
{
::SendMessage(hWnd_Button, BM_CLICK,0 ,0);
}
void Ext_Wnd::SendBnClicked(HWN D hWnd_Button)
{
SendMessage(WM_COMMAND,HIW ORD(BN_CLI CKED)+LOWO RD(1),(LPA RAM)hWnd_B utton);
}
void Ext_Wnd::SendBnDBClick(HWN D hWnd_Button)
{
SendMessage(WM_COMMAND,HIW ORD(BN_DBL CLK)+LOWOR D(1),(LPAR AM)hWnd_Bu tton);
}
UINT Ext_Wnd::GetMenuCommandID( HMENU hMenu, int TierIndex, const std::vector<std::string> &SearchItem)
{
if (!hMenu) return 0;
for (int m = 0;m < GetMenuItemCount(hMenu);m+ +)
{
char mnItem[1024];
if (GetMenuString(hMenu,m,mnI tem,sizeof (mnItem),M F_BYPOSITI ON))
{
if (SearchItem[TierIndex] == mnItem)
{
if (SearchItem.size() == (TierIndex+1))
{
return ::GetMenuItemID(hMenu,m);
}
HMENU SubHmenu = ::GetSubMenu(hMenu,m);
if (SubHmenu)
{
return GetMenuCommandID(SubHmenu, TierIndex+ 1,SearchIt em);
}
return 0;
}
}
}
return 0;
}
UINT Ext_Wnd::GetMenuCommandID( const std::vector<std::string> &SearchItem)
{
HMENU hMenu = GetMenu();
if (!hMenu) return 0;
return GetMenuCommandID(hMenu, 0, SearchItem);
}
HMENU Ext_Wnd::GetMenu(HMENU hMenu, int TierIndex, const std::vector<std::string> &SearchItem, int ListSize)
{
if (!hMenu) return 0;
for (int m = 0;m < GetMenuItemCount(hMenu);m+ +)
{
char mnItem[1024];
if (GetMenuString(hMenu,m,mnI tem,sizeof (mnItem),M F_BYPOSITI ON))
{
if (SearchItem[TierIndex] == mnItem)
{
if (ListSize == (TierIndex+1))
{
return GetSubMenu(hMenu,m);
}
HMENU SubHmenu = ::GetSubMenu(hMenu,m);
if (SubHmenu)
{
return GetMenu(SubHmenu,TierIndex +1,SearchI tem, ListSize);
}
return 0;
}
}
}
return 0;
}
HMENU Ext_Wnd::GetMenu(const std::vector<std::string> &SearchItem, int ListSize)
{
HMENU hMenu = GetMenu();
if (!hMenu) return 0;
if (ListSize == -1) ListSize = SearchItem.size()-1;
return GetMenu(hMenu, 0, SearchItem, ListSize);
}
HWND Ext_Wnd::StartPrgViaCreate Process(co nst char* ProgramPath, const char* lpCurrentDirectory,
char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow, PROCESS_INFORMATION *pi)
{
PROCESS_INFORMATION dummy;
if (pi == NULL)
{
pi = &dummy;
}
return StartPrgViaCreateProcess(* pi,Program Path, lpCurrentDirectory, lpCommandLine, lpEnvironment, wShowWindow);
}
HWND Ext_Wnd::StartPrgViaCreate Process(PR OCESS_INFO RMATION &pi, const char* ProgramPath,
const char* lpCurrentDirectory, char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow)
{
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.wShowWindow = wShowWindow;
memset(&pi,0,sizeof(pi));
if (!CreateProcess(ProgramPat h,lpComman dLine,NULL ,NULL,TRUE ,CREATE_NE W_CONSOLE| NORMAL_PRI ORITY_CLAS S,lpEnviro nment,lpCu rrentDirec tory,&si,& pi))
{
LPVOID lpMsgBuf;
::FormatMessage(FORMAT_MES SAGE_ALLOC ATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSE RTS,
NULL,GetLastError(),MAKELA NGID(LANG_ NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0,NULL);
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
LocalFree(lpMsgBuf);
return NULL;
}
Sleep(500);
HWND hwndNextChild = NULL;
do
{
hwndNextChild = ::FindWindowEx(0,hwndNextC hild,NULL, NULL);
if (hwndNextChild)
{
DWORD dwProcessId;
DWORD dwThread = GetWindowThreadProcessId(h wndNextChi ld, &dwProcessId) ;
if(dwProcessId == pi.dwProcessId || pi.dwThreadId == dwThread )
{
break;
}
bool BreakDo = false;
HWND hwndNextChildChild = NULL;
do
{
hwndNextChildChild = ::FindWindowEx(hwndNextChi ld,hwndNex tChildChil d,NULL,NUL L);
if (hwndNextChildChild)
{
dwThread = GetWindowThreadProcessId(h wndNextChi ldChild, &dwProcessId) ;
if(dwProcessId == pi.dwProcessId || pi.dwThreadId == dwThread )
{
BreakDo=true;
break;
}
}
}while(hwndNextChildChild) ;
if (BreakDo) break;
}
}while(hwndNextChild);
return hwndNextChild;
}
HINSTANCE Ext_Wnd::GetAppHinstance(v oid)
{
return (HINSTANCE)GetClassLong(m_ hWnd,GCL_H MODULE);
}
//************************ ********** ********** ********** ********** ********** ********** *
//************************ ********** ********** ********** ********** ********** ********** *
Ext_Menu::Ext_Menu(HMENU hMenu):m_hMenu(hMenu)
{
}
BOOL Ext_Menu::CreateMenu()
{
m_hMenu = ::CreateMenu();
if (m_hMenu == NULL) return FALSE;
return TRUE;
}
BOOL Ext_Menu::CreatePopupMenu( )
{
m_hMenu = ::CreatePopupMenu();
if (m_hMenu == NULL) return FALSE;
return TRUE;
}
/*
BOOL Ext_Menu::LoadMenu(LPCTSTR lpszResourceName)
{
return ::LoadMenu(m_hMenu,lpszRes ourceName)
}
BOOL Ext_Menu::LoadMenu(UINT nIDResource)
{
return ::LoadMenu(m_hMenu,nIDReso urce)
}
*/
BOOL Ext_Menu::LoadMenuIndirect (const void* lpMenuTemplate)
{
m_hMenu = ::LoadMenuIndirect(lpMenuT emplate);
if (m_hMenu == NULL) return FALSE;
return TRUE;
}
BOOL Ext_Menu::DestroyMenu()
{
return ::DestroyMenu(m_hMenu);
}
// Attributes
Ext_Menu::operator HMENU() const
{
return m_hMenu;
}
BOOL Ext_Menu::operator==(const Ext_Menu& menu) const
{
return (m_hMenu==menu.m_hMenu)?TR UE:FALSE;
}
BOOL Ext_Menu::operator!=(const Ext_Menu& menu) const
{
return (m_hMenu!=menu.m_hMenu)?TR UE:FALSE;
}
BOOL Ext_Menu::Attach(HMENU hMenu)
{
if (m_hMenu!=NULL) return FALSE;
m_hMenu = hMenu;
return TRUE;
}
HMENU Ext_Menu::Detach()
{
HMENU hMenu = m_hMenu;
m_hMenu = NULL;
return hMenu;
}
// Ext_Menu Operations
BOOL Ext_Menu::DeleteMenu(UINT nPosition, UINT nFlags)
{
return ::DeleteMenu(m_hMenu,nPosi tion,nFlag s);
}
BOOL Ext_Menu::TrackPopupMenu(U INT nFlags, int x, int y,Ext_Wnd* pWnd, LPCRECT lpRect)
{
return ::TrackPopupMenu(m_hMenu,n Flags,x,y, 0,pWnd->m_ hWnd,lpRec t);
}
// Ext_MenuItem Operations
BOOL Ext_Menu::AppendMenu(UINT nFlags, UINT nIDNewItem,LPCTSTR lpszNewItem)
{
return ::AppendMenu(m_hMenu,nFlag s,nIDNewIt em,lpszNew Item);
}
/*
BOOL Ext_Menu::AppendMenu(UINT nFlags, UINT nIDNewItem, const CBitmap* pBmp)
{
}
*/
UINT Ext_Menu::CheckMenuItem(UI NT nIDCheckItem, UINT nCheck)
{
return ::CheckMenuItem(m_hMenu,nI DCheckItem ,nCheck);
}
UINT Ext_Menu::EnableMenuItem(U INT nIDEnableItem, UINT nEnable)
{
return ::EnableMenuItem(m_hMenu,n IDEnableIt em,nEnable );
}
UINT Ext_Menu::GetMenuItemCount () const
{
return ::GetMenuItemCount(m_hMenu );
}
UINT Ext_Menu::GetMenuItemID(in t nPos) const
{
return ::GetMenuItemID(m_hMenu,nP os);
}
UINT Ext_Menu::GetMenuState(UIN T nID, UINT nFlags) const
{
return ::GetMenuState(m_hMenu,nID ,nFlags);
}
int Ext_Menu::GetMenuString(UI NT nIDItem, LPTSTR lpString, int nMaxCount,UINT nFlags) const
{
return ::GetMenuString(m_hMenu,nI DItem,lpSt ring,nMaxC ount,nFlag s);
}
int Ext_Menu::GetMenuString(UI NT nIDItem, std::string& rString, UINT nFlags) const
{
char buffer[2048];
int nReturnValue = ::GetMenuString(m_hMenu,nI DItem,buff er,sizeof( buffer),nF lags);
rString = buffer;
return nReturnValue;
}
BOOL Ext_Menu::GetMenuItemInfo( UINT nIDItem, LPMENUITEMINFO lpMenuItemInfo,BOOL fByPos)
{
return ::GetMenuItemInfo(m_hMenu, nIDItem,fB yPos,lpMen uItemInfo) ;
}
Ext_Menu* Ext_Menu::GetSubMenu(int nPos) const
{
return (Ext_Menu*)::GetSubMenu(m_ hMenu,nPos ); //???????????????????
}
BOOL Ext_Menu::InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,LPCTSTR lpszNewItem)
{
return ::InsertMenu(m_hMenu,nPosi tion,nFlag s,nIDNewIt em,lpszNew Item);
}
/*
BOOL Ext_Menu::InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp)
{
}
*/
BOOL Ext_Menu::ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,LPCTSTR lpszNewItem)
{
return ::ModifyMenu(m_hMenu,nPosi tion,nFlag s,nIDNewIt em,lpszNew Item);
}
/*
BOOL Ext_Menu::ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp)
{
}
*/
BOOL Ext_Menu::RemoveMenu(UINT nPosition, UINT nFlags)
{
return ::RemoveMenu(m_hMenu,nPosi tion,nFlag s);
}
/*
BOOL Ext_Menu::SetMenuItemBitma ps(UINT nPosition, UINT nFlags,const CBitmap* pBmpUnchecked, const CBitmap* pBmpChecked)
{
}
*/
BOOL Ext_Menu::CheckMenuRadioIt em(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags)
{
return ::CheckMenuRadioItem(m_hMe nu,nIDFirs t,nIDLast, nIDItem,nF lags);
}
/*
BOOL Ext_Menu::SetDefaultItem(U INT uItem, BOOL fByPos)
{
}
UINT Ext_Menu::GetDefaultItem(U INT gmdiFlags, BOOL fByPos)
{
}
*/
// Context Help Functions
BOOL Ext_Menu::SetMenuContextHe lpId(DWORD dwContextHelpId)
{
return ::SetMenuContextHelpId(m_h Menu,dwCon textHelpId );
}
DWORD Ext_Menu::GetMenuContextHe lpId() const
{
return ::GetMenuContextHelpId(m_h Menu);
}
//************************ ********** ********** ********** ********** ********** ********** *
//************************ ********** ********** ********** ********** ********** ********** *
std::map<UINT, CALLBACK_FUNCTION_TYPE1> MainApp::FunctionCallbackL ist1;
std::map<UINT, CALLBACK_FUNCTION_TYPE2> MainApp::FunctionCallbackL ist2;
std::map<UINT, CALLBACK_FUNCTION_TYPE3> MainApp::FunctionCallbackL ist3;
std::map<UINT, CALLBACK_FUNCTION_TYPE4> MainApp::FunctionCallbackL ist4;
std::string MainApp::AppName = "";
MainApp::MainApp(const std::vector<WindowPath> &windowpath, bool bCloseAppInDestructor)
: m_ApplicationMainWnd(windo wpath), menu(m_ApplicationMainWnd. GetMenu()) , m_hiApp(m_ApplicationMainW nd.GetAppH instance() ),
m_bCloseAppInDestructor(bC loseAppInD estructor) , m_hCallAppWnd(NULL)
{
}
MainApp::MainApp(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow, bool bCloseAppInDestructor)
: m_ApplicationMainWnd(Progr amPath,lpC urrentDire ctory,lpCo mmandLine, lpEnvironm ent,wShowW indow), menu(m_ApplicationMainWnd. GetMenu()) ,
m_hiApp(m_ApplicationMainW nd.GetAppH instance() ), m_bCloseAppInDestructor(bC loseAppInD estructor) , m_hCallAppWnd(NULL)
{
}
MainApp::~MainApp()
{
if (nHook != NULL)
{
UnhookWindowsHookEx(nHook) ;
}
if (pWndProc != NULL)
{
::SetClassLong(m_Applicati onMainWnd. m_hWnd,GCL _WNDPROC,( long)pWndP roc);
}
if (m_bCloseAppInDestructor)
{
m_ApplicationMainWnd.Close App();
}
}
Ext_Wnd *MainApp::GetApplicationMa inWnd(void )
{
return &m_ApplicationMainWnd;
}
HINSTANCE MainApp::GetApplicationHin stance(voi d)
{
return m_hiApp;
}
bool MainApp::AddToMainMenu(con st std::vector<std::string> &NewMenuItem, CALLBACK_FUNCTION_TYPE1 f1, UINT uFlags, UINT_PTR uIDNewItem)
{
Ext_Menu hParentMenu = m_ApplicationMainWnd.GetMe nu(NewMenu Item,NewMe nuItem.siz e()-2);
if (hParentMenu.m_hMenu == NULL) return false;
if (!hParentMenu.AppendMenu(u Flags,uIDN ewItem,New MenuItem.a t(NewMenuI tem.size() -1).c_str( ))) return false;
FunctionCallbackList1[uIDN ewItem] = f1;
if (nHook == NULL)
{
nHook = ::SetWindowsHookEx(WH_CALL WNDPROC, HookProc, 0, 0);
}
if (pWndProc == NULL)
{
pWndProc = (CALLBACK_FORWARD_FUNC)::S etClassLon g(m_hCallA ppWnd,GCL_ WNDPROC,(l ong)&WndPr oc);
}
return true;
}
HHOOK MainApp::nHook=NULL;
LRESULT CALLBACK MainApp::HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if (wParam)
{
WORD x = wParam;
}
return ::CallNextHookEx(nHook, nCode, wParam, lParam);
}
CALLBACK_FORWARD_FUNC MainApp::pWndProc=NULL;
LRESULT CALLBACK MainApp::WndProc(HWND hWnd, UINT uCode,WPARAM wParam,LPARAM lParam)
{
PMSG pmsg = (PMSG) lParam;
if (FunctionCallbackList1[wPa ram] != NULL)
{
FunctionCallbackList1[wPar am]();
}
else if (FunctionCallbackList2[wPa ram] != NULL)
{
FunctionCallbackList2[wPar am](wParam );
}
else if (FunctionCallbackList3[wPa ram] != NULL)
{
FunctionCallbackList3[wPar am](wParam ,AppName.c _str());
}
else if (FunctionCallbackList4[wPa ram] != NULL)
{
FunctionCallbackList4[wPar am](uCode, wParam,lPa ram);
}
if (pWndProc != NULL) return pWndProc(hWnd,uCode,wParam ,lParam);
return 0;
}
#include "stdafx.h"
#include "ExternalPrgController.h"
//#include <AFXRES.H>
//************************
//************************
Ext_Wnd::Ext_Wnd(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow)
:m_hWnd(StartPrgViaCreateP
{
}
Ext_Wnd::Ext_Wnd(const std::vector<WindowPath> &windowpath, HWND hwndParent, HWND hwndChildAfter)
:m_hWnd(FindWindow(windowp
{
}
//////////////////////////
//////////////////////////
// Common Windows Commands;
//////////////////////////
HWND Ext_Wnd::FindWindow(const std::vector<WindowPath> &windowpath, int index, HWND hwndParent,
HWND hwndChildAfter)
{
if (index >= windowpath.size()) return 0;
HWND retrnValue = NULL;
HWND hwndNextChild = hwndChildAfter;
int MemberIndex = windowpath[index].m_nIndex
std::string szClassName = windowpath[index].m_szClas
std::string szWindowName = (windowpath[index].m_Searc
windowpath[index].m_szWind
int nLenStr_WindowName = windowpath[index].m_szWind
do
{
retrnValue = ::FindWindowEx(hwndParent,
(szClassName.size()>0)?szC
(szWindowName.size()>0)?sz
if (retrnValue == NULL) return retrnValue;
hwndNextChild=retrnValue;
char szCurrentWindowName[1024];
::GetWindowText(hwndNextCh
switch(windowpath[index].m
{
case WindowPath::WP_CaptionStar
if (!strncmp(windowpath[index
szCurrentWindowName,nLenSt
{
MemberIndex--;
}
break;
case WindowPath::WP_CaptionEndW
if (nLenStr_WindowName <= strlen(szCurrentWindowName
{
if (!strncmp(windowpath[index
szCurrentWindowName+strlen
{
MemberIndex--;
}
}
break;
case WindowPath::WP_CaptionIncl
if (strstr(szCurrentWindowNam
{
MemberIndex--;
}
break;
case WindowPath::WP_CaptionNotI
if (!strstr(szCurrentWindowNa
{
MemberIndex--;
}
break;
case WindowPath::WP_Normal:
default:
MemberIndex--;
break;
}
}while(MemberIndex>-1);
index++;
if (index < windowpath.size())
{
return FindWindow(windowpath,inde
}
return retrnValue;
}
Ext_Wnd::operator HWND() const
{
return m_hWnd;
}
void Ext_Wnd::BringWindowToTop(
{
::BringWindowToTop(m_hWnd)
}
BOOL Ext_Wnd::EnableWindow(BOOL
{
return ::EnableWindow(m_hWnd, bEnable);
}
HWND Ext_Wnd::FindWindow(LPCTST
{
return ::FindWindow(lpClassName,l
}
HWND Ext_Wnd::GetLastActivePopu
{
return ::GetLastActivePopup(m_hWn
}
HMENU Ext_Wnd::GetMenu(void)
{
return ::GetMenu(m_hWnd);
}
#ifdef __AFX_H__
// This function gets a compile error in Win32 (non-MFC) apps
HWND Ext_Wnd::GetNextWindow(UIN
{
return ::GetNextWindow(m_hWnd, nFlag);
}
#endif //_MSC_VER
HWND Ext_Wnd::GetParent(void)
{
return ::GetParent(m_hWnd);
}
LONG Ext_Wnd::GetWindowLong(int
{
return ::GetWindowLong(m_hWnd, nIndex);
}
HMENU Ext_Wnd::GetSystemMenu(BOO
{
return ::GetSystemMenu(m_hWnd, bRevert);
}
HWND Ext_Wnd::GetTopWindow(void
{
return ::GetTopWindow(m_hWnd);
}
BOOL Ext_Wnd::GetUpdateRect(LPR
{
return ::GetUpdateRect(m_hWnd, lpRect, bErase);
}
int Ext_Wnd::GetUpdateRgn(HRGN
{
return ::GetUpdateRgn(m_hWnd, hRgn, bErase);
}
HWND Ext_Wnd::GetWindow(UINT uCmd)
{
return ::GetWindow(m_hWnd, uCmd);
}
BOOL Ext_Wnd::GetWindowRect(LPR
{
return ::GetWindowRect(m_hWnd, lpRect);
}
int Ext_Wnd::GetWindowText(LPT
{
return SendMessage(WM_GETTEXT, (WPARAM)nMaxCount,(LPARAM)
}
int Ext_Wnd::GetWindowTextLeng
{
return SendMessage(WM_GETTEXTLENG
}
BOOL Ext_Wnd::HiliteMenuItem(HM
{
return ::HiliteMenuItem(m_hWnd, hmenu, uItemHilite, uHilite);
}
BOOL Ext_Wnd::InvalidateRect(CO
{
return ::InvalidateRect(m_hWnd, lpRect, bErase);
}
BOOL Ext_Wnd::InvalidateRgn(HRG
{
return ::InvalidateRgn(m_hWnd, hRgn, bErase);
}
BOOL Ext_Wnd::IsChild(HWND hWnd)
{
return ::IsChild(m_hWnd, hWnd);
}
BOOL Ext_Wnd::IsIconic(void)
{
return ::IsIconic(m_hWnd);
}
BOOL Ext_Wnd::IsWindowEnabled(v
{
return ::IsWindowEnabled(m_hWnd);
}
BOOL Ext_Wnd::IsWindowVisible(v
{
return ::IsWindowVisible(m_hWnd);
}
BOOL Ext_Wnd::IsZoomed(void)
{
return ::IsZoomed(m_hWnd);
}
BOOL Ext_Wnd::KillTimer(UINT uIDEvent)
{
return ::KillTimer(m_hWnd, uIDEvent);
}
BOOL Ext_Wnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint )
{
return ::MoveWindow(m_hWnd,x,y,nW
}
BOOL Ext_Wnd::PostMessage(UINT message, WPARAM wParam , LPARAM lParam )
{
return ::PostMessage(m_hWnd, message, wParam, lParam);
}
LRESULT Ext_Wnd::SendMessage(UINT message, WPARAM wParam , LPARAM lParam )
{
return ::SendMessage(m_hWnd, message, wParam, lParam);
}
BOOL Ext_Wnd::SendNotifyMessage
{
return ::SendNotifyMessage(m_hWnd
}
HWND Ext_Wnd::SetActiveWindow(v
{
return ::SetActiveWindow(m_hWnd);
}
HWND Ext_Wnd::SetCapture(void)
{
return ::SetCapture(m_hWnd);
}
HWND Ext_Wnd::SetFocus(void)
{
return ::SetFocus(m_hWnd);
}
HWND Ext_Wnd::SetParent(HWND hWndNewParent)
{
return ::SetParent(m_hWnd, hWndNewParent);
}
BOOL Ext_Wnd::SetWindowPos(HWND
{
return ::SetWindowPos(m_hWnd, hWndInsertAfter,x,y,cx,cy,
}
int Ext_Wnd::SetWindowRgn(HRGN
{
return ::SetWindowRgn(m_hWnd, hRgn, bRedraw);
}
void Ext_Wnd::SetWindowText(std
{
::SetWindowText(m_hWnd,tex
}
void Ext_Wnd::ShowWindow(int nCmdShow)
{
::ShowWindow(m_hWnd,nCmdSh
}
BOOL Ext_Wnd::UpdateWindow()
{
return ::UpdateWindow(m_hWnd);
}
BOOL Ext_Wnd::ValidateRect(CONS
{
return ::ValidateRect(m_hWnd, lpRect);
}
BOOL Ext_Wnd::ValidateRgn(HRGN hRgn)
{
return ::ValidateRgn(m_hWnd, hRgn);
}
//////////////////////////
//////////////////////////
// Custom Window Commands
//////////////////////////
void Ext_Wnd::CloseApp(void)
{
PostMessage(WM_SYSCOMMAND,
}
BOOL Ext_Wnd::EnableWindow(int nIDDlgItem, BOOL bEnable)
{
HWND hWnd = ::GetDlgItem(m_hWnd,nIDDlg
if (hWnd) return ::EnableWindow(hWnd, bEnable);
return FALSE;
}
void Ext_Wnd::SetWindowEditBoxT
{
SendMessage(WM_SETTEXT,0,(
}
void Ext_Wnd::SendKey(char Key)
{
char data[8];
memset(data,0,sizeof(data)
PostMessage(WM_CHAR,Key,(L
}
void Ext_Wnd::SendBnClick(HWND hWnd_Button)
{
::SendMessage(hWnd_Button,
}
void Ext_Wnd::SendBnClicked(HWN
{
SendMessage(WM_COMMAND,HIW
}
void Ext_Wnd::SendBnDBClick(HWN
{
SendMessage(WM_COMMAND,HIW
}
UINT Ext_Wnd::GetMenuCommandID(
{
if (!hMenu) return 0;
for (int m = 0;m < GetMenuItemCount(hMenu);m+
{
char mnItem[1024];
if (GetMenuString(hMenu,m,mnI
{
if (SearchItem[TierIndex] == mnItem)
{
if (SearchItem.size() == (TierIndex+1))
{
return ::GetMenuItemID(hMenu,m);
}
HMENU SubHmenu = ::GetSubMenu(hMenu,m);
if (SubHmenu)
{
return GetMenuCommandID(SubHmenu,
}
return 0;
}
}
}
return 0;
}
UINT Ext_Wnd::GetMenuCommandID(
{
HMENU hMenu = GetMenu();
if (!hMenu) return 0;
return GetMenuCommandID(hMenu, 0, SearchItem);
}
HMENU Ext_Wnd::GetMenu(HMENU hMenu, int TierIndex, const std::vector<std::string> &SearchItem, int ListSize)
{
if (!hMenu) return 0;
for (int m = 0;m < GetMenuItemCount(hMenu);m+
{
char mnItem[1024];
if (GetMenuString(hMenu,m,mnI
{
if (SearchItem[TierIndex] == mnItem)
{
if (ListSize == (TierIndex+1))
{
return GetSubMenu(hMenu,m);
}
HMENU SubHmenu = ::GetSubMenu(hMenu,m);
if (SubHmenu)
{
return GetMenu(SubHmenu,TierIndex
}
return 0;
}
}
}
return 0;
}
HMENU Ext_Wnd::GetMenu(const std::vector<std::string> &SearchItem, int ListSize)
{
HMENU hMenu = GetMenu();
if (!hMenu) return 0;
if (ListSize == -1) ListSize = SearchItem.size()-1;
return GetMenu(hMenu, 0, SearchItem, ListSize);
}
HWND Ext_Wnd::StartPrgViaCreate
char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow, PROCESS_INFORMATION *pi)
{
PROCESS_INFORMATION dummy;
if (pi == NULL)
{
pi = &dummy;
}
return StartPrgViaCreateProcess(*
}
HWND Ext_Wnd::StartPrgViaCreate
const char* lpCurrentDirectory, char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow)
{
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.wShowWindow = wShowWindow;
memset(&pi,0,sizeof(pi));
if (!CreateProcess(ProgramPat
{
LPVOID lpMsgBuf;
::FormatMessage(FORMAT_MES
NULL,GetLastError(),MAKELA
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
LocalFree(lpMsgBuf);
return NULL;
}
Sleep(500);
HWND hwndNextChild = NULL;
do
{
hwndNextChild = ::FindWindowEx(0,hwndNextC
if (hwndNextChild)
{
DWORD dwProcessId;
DWORD dwThread = GetWindowThreadProcessId(h
if(dwProcessId == pi.dwProcessId || pi.dwThreadId == dwThread )
{
break;
}
bool BreakDo = false;
HWND hwndNextChildChild = NULL;
do
{
hwndNextChildChild = ::FindWindowEx(hwndNextChi
if (hwndNextChildChild)
{
dwThread = GetWindowThreadProcessId(h
if(dwProcessId == pi.dwProcessId || pi.dwThreadId == dwThread )
{
BreakDo=true;
break;
}
}
}while(hwndNextChildChild)
if (BreakDo) break;
}
}while(hwndNextChild);
return hwndNextChild;
}
HINSTANCE Ext_Wnd::GetAppHinstance(v
{
return (HINSTANCE)GetClassLong(m_
}
//************************
//************************
Ext_Menu::Ext_Menu(HMENU hMenu):m_hMenu(hMenu)
{
}
BOOL Ext_Menu::CreateMenu()
{
m_hMenu = ::CreateMenu();
if (m_hMenu == NULL) return FALSE;
return TRUE;
}
BOOL Ext_Menu::CreatePopupMenu(
{
m_hMenu = ::CreatePopupMenu();
if (m_hMenu == NULL) return FALSE;
return TRUE;
}
/*
BOOL Ext_Menu::LoadMenu(LPCTSTR
{
return ::LoadMenu(m_hMenu,lpszRes
}
BOOL Ext_Menu::LoadMenu(UINT nIDResource)
{
return ::LoadMenu(m_hMenu,nIDReso
}
*/
BOOL Ext_Menu::LoadMenuIndirect
{
m_hMenu = ::LoadMenuIndirect(lpMenuT
if (m_hMenu == NULL) return FALSE;
return TRUE;
}
BOOL Ext_Menu::DestroyMenu()
{
return ::DestroyMenu(m_hMenu);
}
// Attributes
Ext_Menu::operator HMENU() const
{
return m_hMenu;
}
BOOL Ext_Menu::operator==(const
{
return (m_hMenu==menu.m_hMenu)?TR
}
BOOL Ext_Menu::operator!=(const
{
return (m_hMenu!=menu.m_hMenu)?TR
}
BOOL Ext_Menu::Attach(HMENU hMenu)
{
if (m_hMenu!=NULL) return FALSE;
m_hMenu = hMenu;
return TRUE;
}
HMENU Ext_Menu::Detach()
{
HMENU hMenu = m_hMenu;
m_hMenu = NULL;
return hMenu;
}
// Ext_Menu Operations
BOOL Ext_Menu::DeleteMenu(UINT nPosition, UINT nFlags)
{
return ::DeleteMenu(m_hMenu,nPosi
}
BOOL Ext_Menu::TrackPopupMenu(U
{
return ::TrackPopupMenu(m_hMenu,n
}
// Ext_MenuItem Operations
BOOL Ext_Menu::AppendMenu(UINT nFlags, UINT nIDNewItem,LPCTSTR lpszNewItem)
{
return ::AppendMenu(m_hMenu,nFlag
}
/*
BOOL Ext_Menu::AppendMenu(UINT nFlags, UINT nIDNewItem, const CBitmap* pBmp)
{
}
*/
UINT Ext_Menu::CheckMenuItem(UI
{
return ::CheckMenuItem(m_hMenu,nI
}
UINT Ext_Menu::EnableMenuItem(U
{
return ::EnableMenuItem(m_hMenu,n
}
UINT Ext_Menu::GetMenuItemCount
{
return ::GetMenuItemCount(m_hMenu
}
UINT Ext_Menu::GetMenuItemID(in
{
return ::GetMenuItemID(m_hMenu,nP
}
UINT Ext_Menu::GetMenuState(UIN
{
return ::GetMenuState(m_hMenu,nID
}
int Ext_Menu::GetMenuString(UI
{
return ::GetMenuString(m_hMenu,nI
}
int Ext_Menu::GetMenuString(UI
{
char buffer[2048];
int nReturnValue = ::GetMenuString(m_hMenu,nI
rString = buffer;
return nReturnValue;
}
BOOL Ext_Menu::GetMenuItemInfo(
{
return ::GetMenuItemInfo(m_hMenu,
}
Ext_Menu* Ext_Menu::GetSubMenu(int nPos) const
{
return (Ext_Menu*)::GetSubMenu(m_
}
BOOL Ext_Menu::InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,LPCTSTR lpszNewItem)
{
return ::InsertMenu(m_hMenu,nPosi
}
/*
BOOL Ext_Menu::InsertMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp)
{
}
*/
BOOL Ext_Menu::ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,LPCTSTR lpszNewItem)
{
return ::ModifyMenu(m_hMenu,nPosi
}
/*
BOOL Ext_Menu::ModifyMenu(UINT nPosition, UINT nFlags, UINT nIDNewItem,const CBitmap* pBmp)
{
}
*/
BOOL Ext_Menu::RemoveMenu(UINT nPosition, UINT nFlags)
{
return ::RemoveMenu(m_hMenu,nPosi
}
/*
BOOL Ext_Menu::SetMenuItemBitma
{
}
*/
BOOL Ext_Menu::CheckMenuRadioIt
{
return ::CheckMenuRadioItem(m_hMe
}
/*
BOOL Ext_Menu::SetDefaultItem(U
{
}
UINT Ext_Menu::GetDefaultItem(U
{
}
*/
// Context Help Functions
BOOL Ext_Menu::SetMenuContextHe
{
return ::SetMenuContextHelpId(m_h
}
DWORD Ext_Menu::GetMenuContextHe
{
return ::GetMenuContextHelpId(m_h
}
//************************
//************************
std::map<UINT, CALLBACK_FUNCTION_TYPE1> MainApp::FunctionCallbackL
std::map<UINT, CALLBACK_FUNCTION_TYPE2> MainApp::FunctionCallbackL
std::map<UINT, CALLBACK_FUNCTION_TYPE3> MainApp::FunctionCallbackL
std::map<UINT, CALLBACK_FUNCTION_TYPE4> MainApp::FunctionCallbackL
std::string MainApp::AppName = "";
MainApp::MainApp(const std::vector<WindowPath> &windowpath, bool bCloseAppInDestructor)
: m_ApplicationMainWnd(windo
m_bCloseAppInDestructor(bC
{
}
MainApp::MainApp(const char* ProgramPath, const char* lpCurrentDirectory, char* lpCommandLine, LPVOID lpEnvironment, WORD wShowWindow, bool bCloseAppInDestructor)
: m_ApplicationMainWnd(Progr
m_hiApp(m_ApplicationMainW
{
}
MainApp::~MainApp()
{
if (nHook != NULL)
{
UnhookWindowsHookEx(nHook)
}
if (pWndProc != NULL)
{
::SetClassLong(m_Applicati
}
if (m_bCloseAppInDestructor)
{
m_ApplicationMainWnd.Close
}
}
Ext_Wnd *MainApp::GetApplicationMa
{
return &m_ApplicationMainWnd;
}
HINSTANCE MainApp::GetApplicationHin
{
return m_hiApp;
}
bool MainApp::AddToMainMenu(con
{
Ext_Menu hParentMenu = m_ApplicationMainWnd.GetMe
if (hParentMenu.m_hMenu == NULL) return false;
if (!hParentMenu.AppendMenu(u
FunctionCallbackList1[uIDN
if (nHook == NULL)
{
nHook = ::SetWindowsHookEx(WH_CALL
}
if (pWndProc == NULL)
{
pWndProc = (CALLBACK_FORWARD_FUNC)::S
}
return true;
}
HHOOK MainApp::nHook=NULL;
LRESULT CALLBACK MainApp::HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if (wParam)
{
WORD x = wParam;
}
return ::CallNextHookEx(nHook, nCode, wParam, lParam);
}
CALLBACK_FORWARD_FUNC MainApp::pWndProc=NULL;
LRESULT CALLBACK MainApp::WndProc(HWND hWnd, UINT uCode,WPARAM wParam,LPARAM lParam)
{
PMSG pmsg = (PMSG) lParam;
if (FunctionCallbackList1[wPa
{
FunctionCallbackList1[wPar
}
else if (FunctionCallbackList2[wPa
{
FunctionCallbackList2[wPar
}
else if (FunctionCallbackList3[wPa
{
FunctionCallbackList3[wPar
}
else if (FunctionCallbackList4[wPa
{
FunctionCallbackList4[wPar
}
if (pWndProc != NULL) return pWndProc(hWnd,uCode,wParam
return 0;
}
The following is example on how to select a menu item.
This example works on the windows NotePad program.
It selects the Word Wrap menu item, so that the check mark turns on and off.
std::vector<WindowPath> windowpath_list;
windowpath_list.push_back( WindowPath ("Notepad" ));
Ext_Wnd NotePadApp(windowpath_list );
std::vector<std::string> MenuItem;
MenuItem.push_back("&Edit" );
MenuItem.push_back("&Word Wrap");
UINT Cmd = NotePadApp.GetMenuCommandI D(MenuItem );
if (Cmd) NotePadApp.PostMessage(WM_ COMMAND,Cm d,0);
This example works on the windows NotePad program.
It selects the Word Wrap menu item, so that the check mark turns on and off.
std::vector<WindowPath> windowpath_list;
windowpath_list.push_back(
Ext_Wnd NotePadApp(windowpath_list
std::vector<std::string> MenuItem;
MenuItem.push_back("&Edit"
MenuItem.push_back("&Word Wrap");
UINT Cmd = NotePadApp.GetMenuCommandI
if (Cmd) NotePadApp.PostMessage(WM_
Disregard the hook functions in MainApp class. I'm still working on them, and they're not finish.
In fact, you don't have to use MainApp class at all.
In fact, you don't have to use MainApp class at all.
ASKER
thats interesting, looks like you have done a lot of work. It will take me some time to understand exactly what is going on.
Is there a way to hook an the WINPROC for a HMENU?
Is there a way to hook an the WINPROC for a HMENU?
That's the part I've been stuck at.
I can get the handle to the menu, and I can add a menu item with no problem. But I can't seem to get a hook on it.
I haven't tried using a system hook, so you might have some luck with that.
I can get the handle to the menu, and I can add a menu item with no problem. But I can't seem to get a hook on it.
I haven't tried using a system hook, so you might have some luck with that.
ASKER
I am using a system wide hook. It seems that the system has a section reserved for menus scroll bars and dialog boxes. I am not sure whats up with that.
listening......
If this proposed answer has helped you, please accept it, grade it and close this question. If it is not, reject it and add comments as to your progress and what else is needed.
Thanks,
Moondancer
Community Support Moderator @ Experts Exchange
Thanks,
Moondancer
Community Support Moderator @ Experts Exchange
ASKER
Axter, did you ever manager to hook the menus? I am working on a theory that you actually have to make a kernel hook to hook menu functions, however, I am still working on it.
Jason
Jason
>>Axter, did you ever manager to hook the menus?
No.
Please let me know if you have any luck.
No.
Please let me know if you have any luck.
SHOULD I FORCE ACCEPT THIS TO CLOSE IT? Again months later.
Moondancer
Community Support Moderator @ Experts Exchange
Moondancer
Community Support Moderator @ Experts Exchange
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes, use the FindWindow() function.