Wednesday, January 31, 2007

Office Programming-- How to make office file readonly when automating them.


Solution:


1. Find the handle of edit area window:


2. Disable the window using Win32 API: EnableWindow();



Sample code:


CFindWnd fwndWord(this->GetSafeHwnd(), _T("_WwG")); //Use Spy++ to hook it.
CWnd * m_pWndWwG = CWnd::FromHandle(fwndWord.m_hWnd);
if( m_pWndWwG)
{
m_pWndWwG->EnableWindow(FALSE); //Disable the editable of word.
}



// This class encapsulates the process of finding a window with a given class name
// as a descendant of a given window. To use it, instantiate like so:
//
// CFindWnd fw(hwndParent,classname);
//
// fw.m_hWnd will be the HWND of the desired window, if found.
//
class CFindWnd {
private:
//////////////////
// This private function is used with EnumChildWindows to find the child
// with a given class name. Returns FALSE if found (to stop enumerating).
//
static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam)
{
CFindWnd *pfw = (CFindWnd*)lParam;
HWND hwnd = FindWindowEx(hwndParent,NULL, pfw->m_classname, NULL/*, pfw->m_classname*/);


if (hwnd) {
pfw->m_hWnd = hwnd; // found: save it
return FALSE; // stop enumerating
}
EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
return TRUE; // keep looking
}
public:
LPCTSTR m_classname; // class name to look for
HWND m_hWnd; // HWND if found
// ctor does the work--just instantiate and go
CFindWnd(HWND hwndParent, LPCTSTR classname)
: m_hWnd(NULL), m_classname(classname)
{
FindChildClassHwnd(hwndParent, (LPARAM)this);
}
};




Technorati :
Del.icio.us :

No comments: