WinFinderCtl.cpp
上传用户:lwzh1970
上传日期:2007-01-01
资源大小:33k
文件大小:11k
- // WinFinderCtl.cpp : Implementation of the CWinFinderCtrl OLE control class.
- #include "stdafx.h"
- #include "WinFinder.h"
- #include "WinFinderCtl.h"
- #include "WinFinderPpg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- IMPLEMENT_DYNCREATE(CWinFinderCtrl, COleControl)
- /////////////////////////////////////////////////////////////////////////////
- // Message map
- BEGIN_MESSAGE_MAP(CWinFinderCtrl, COleControl)
- //{{AFX_MSG_MAP(CWinFinderCtrl)
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_MESSAGE(WM_SETWFCALLBACK, OnSetWFCallback)
- //}}AFX_MSG_MAP
- ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit)
- ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // Dispatch map
- BEGIN_DISPATCH_MAP(CWinFinderCtrl, COleControl)
- //{{AFX_DISPATCH_MAP(CWinFinderCtrl)
- DISP_PROPERTY_NOTIFY(CWinFinderCtrl, "dragging", m_bDragging, OnDraggingChanged, VT_BOOL)
- DISP_PROPERTY_NOTIFY(CWinFinderCtrl, "TargetWindow", m_targetWindow, OnTargetWindowChanged, VT_HANDLE)
- DISP_PROPERTY_EX_ID(CWinFinderCtrl, "hwnd", DISPID_HWND, GetHwnd, SetHwnd, VT_HANDLE)
- //}}AFX_DISPATCH_MAP
- DISP_FUNCTION_ID(CWinFinderCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
- END_DISPATCH_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // Event map
- BEGIN_EVENT_MAP(CWinFinderCtrl, COleControl)
- //{{AFX_EVENT_MAP(CWinFinderCtrl)
- // NOTE - ClassWizard will add and remove event map entries
- // DO NOT EDIT what you see in these blocks of generated code !
- //}}AFX_EVENT_MAP
- END_EVENT_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // Property pages
- // TODO: Add more property pages as needed. Remember to increase the count!
- BEGIN_PROPPAGEIDS(CWinFinderCtrl, 1)
- PROPPAGEID(CWinFinderPropPage::guid)
- END_PROPPAGEIDS(CWinFinderCtrl)
- /////////////////////////////////////////////////////////////////////////////
- // Initialize class factory and guid
- IMPLEMENT_OLECREATE_EX(CWinFinderCtrl, "WINFINDER.WinFinderCtrl.1",
- 0xdca2aa3, 0xc50f, 0x11d2, 0x95, 0x80, 0x20, 0xb3, 0x4e, 0xc1, 0, 0)
- /////////////////////////////////////////////////////////////////////////////
- // Type library ID and version
- IMPLEMENT_OLETYPELIB(CWinFinderCtrl, _tlid, _wVerMajor, _wVerMinor)
- /////////////////////////////////////////////////////////////////////////////
- // Interface IDs
- const IID BASED_CODE IID_DWinFinder =
- { 0xdca2aa1, 0xc50f, 0x11d2, { 0x95, 0x80, 0x20, 0xb3, 0x4e, 0xc1, 0, 0 } };
- const IID BASED_CODE IID_DWinFinderEvents =
- { 0xdca2aa2, 0xc50f, 0x11d2, { 0x95, 0x80, 0x20, 0xb3, 0x4e, 0xc1, 0, 0 } };
- /////////////////////////////////////////////////////////////////////////////
- // Control type information
- static const DWORD BASED_CODE _dwWinFinderOleMisc =
- OLEMISC_ACTIVATEWHENVISIBLE |
- OLEMISC_SETCLIENTSITEFIRST |
- OLEMISC_INSIDEOUT |
- OLEMISC_CANTLINKINSIDE |
- OLEMISC_RECOMPOSEONRESIZE;
- IMPLEMENT_OLECTLTYPE(CWinFinderCtrl, IDS_WINFINDER, _dwWinFinderOleMisc)
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::CWinFinderCtrlFactory::UpdateRegistry -
- // Adds or removes system registry entries for CWinFinderCtrl
- BOOL CWinFinderCtrl::CWinFinderCtrlFactory::UpdateRegistry(BOOL bRegister)
- {
- if (bRegister)
- return AfxOleRegisterControlClass(
- AfxGetInstanceHandle(),
- m_clsid,
- m_lpszProgID,
- IDS_WINFINDER,
- IDB_WINFINDER,
- TRUE, // Insertable
- _dwWinFinderOleMisc,
- _tlid,
- _wVerMajor,
- _wVerMinor);
- else
- return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
- }
- /////////////////////////////////////////////////////////////////////////////
- // Licensing strings
- static const TCHAR BASED_CODE _szLicFileName[] = _T("WinFinder.lic");
- static const WCHAR BASED_CODE _szLicString[] =
- L"Copyright (c) 1999 ";
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::CWinFinderCtrlFactory::VerifyUserLicense -
- // Checks for existence of a user license
- BOOL CWinFinderCtrl::CWinFinderCtrlFactory::VerifyUserLicense()
- {
- return AfxVerifyLicFile(AfxGetInstanceHandle(), _szLicFileName,
- _szLicString);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::CWinFinderCtrlFactory::GetLicenseKey -
- // Returns a runtime licensing key
- BOOL CWinFinderCtrl::CWinFinderCtrlFactory::GetLicenseKey(DWORD dwReserved,
- BSTR FAR* pbstrKey)
- {
- if (pbstrKey == NULL)
- return FALSE;
- *pbstrKey = SysAllocString(_szLicString);
- return (*pbstrKey != NULL);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::CWinFinderCtrl - Constructor
- // Default callback function, does nothing
- int DefaultWindowFinderSelectedCallback(HWND hWnd)
- {
- return 0;
- }
- CWinFinderCtrl::CWinFinderCtrl()
- {
- InitializeIIDs(&IID_DWinFinder, &IID_DWinFinderEvents);
- // TODO: Initialize your control's instance data here.
- m_bDragging = FALSE;
- m_targetWindow = 0;
- m_pfnWindowSelectedCallback = DefaultWindowFinderSelectedCallback;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::~CWinFinderCtrl - Destructor
- CWinFinderCtrl::~CWinFinderCtrl()
- {
- // TODO: Cleanup your control's instance data here.
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::OnDraw - Drawing function
- void CWinFinderCtrl::OnDraw(
- CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
- {
- // TODO: Replace the following code with your own drawing code.
- // pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
- // pdc->Ellipse(rcBounds);
- HICON icon;
- if (!m_bDragging)
- icon = AfxGetApp()->LoadIcon(IDI_WINFINDER);
- else
- // If the user is dragging the wheel thingy, don't show it
- icon = AfxGetApp()->LoadIcon(IDI_WINFINDER_DRAG);
- pdc->DrawIcon(0, 0, icon);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::DoPropExchange - Persistence support
- void CWinFinderCtrl::DoPropExchange(CPropExchange* pPX)
- {
- ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
- COleControl::DoPropExchange(pPX);
- // TODO: Call PX_ functions for each persistent custom property.
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::OnResetState - Reset control to default state
- void CWinFinderCtrl::OnResetState()
- {
- COleControl::OnResetState(); // Resets defaults found in DoPropExchange
- // TODO: Reset any other control state here.
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl::AboutBox - Display an "About" box to the user
- void CWinFinderCtrl::AboutBox()
- {
- CDialog dlgAbout(IDD_ABOUTBOX_WINFINDER);
- dlgAbout.DoModal();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CWinFinderCtrl message handlers
- void CWinFinderCtrl::OnDraggingChanged()
- {
- SetModifiedFlag();
- }
- // Left button down, start capturing
- void CWinFinderCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- SetCapture();
- m_bDragging = TRUE;
- SetCursor(AfxGetApp()->LoadCursor(IDC_DRAGGING));
- Invalidate();
- COleControl::OnLButtonDown(nFlags, point);
- }
- // Left button up, stop capturing
- void CWinFinderCtrl::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CRect rc;
- ReleaseCapture();
- m_bDragging = FALSE;
- Invalidate();
- DrawWindowFrame((HWND)m_targetWindow);
- ::GetWindowRect((HWND)m_targetWindow, &rc);
- ::InvalidateRect((HWND)m_targetWindow, rc, TRUE);
- COleControl::OnLButtonUp(nFlags, point);
- }
- void CWinFinderCtrl::OnTargetWindowChanged()
- {
- // TODO: Add notification handler code
- SetModifiedFlag();
- }
- void CWinFinderCtrl::OnMouseMove(UINT nFlags, CPoint point)
- {
- if (!m_bDragging)
- return; // Not dragging
- SetWindowText("WindowFinderCtrl");
- CDC dc; // Device context of window
- CDC olddc; // DC of previous window
- HDC hDC; // HDC of 'dc'
- CRect rc; // Rectangle of window
- CBrush br // Black brush
- (1, RGB(0xFF,
- 0xFF, 0xFF));
- CPen pen // Pen to draw rectangle with
- (PS_ALTERNATE,
- 10,
- RGB(0xFF, 0xFF, 0xFF));
- HWND hWnd; // HWND of window
- int i;
- static HWND hWndOld = (HWND)-1;
- // Very important! We must convert the client coordinates to
- // screen coordinates because ::WindowFromPoint() requires it
- ClientToScreen(&point);
- // Set 'm_targetWindow' to the window handle of the window that is
- // under the cursor
- m_targetWindow = (LONG)::WindowFromPoint(point);
- hWnd = (HWND)m_targetWindow;
- if (hWnd == m_hWnd)
- return; // Why would we want to target ourselfs?
- // If this is the same window as it was last time, there is no need
- // to do anything
- if (hWnd == hWndOld)
- return;
- // Call the user-defined callback that means that the selected window
- // has changed
- i = m_pfnWindowSelectedCallback(hWnd);
-
- // NEW IN 1.1: Send a WM_USER + 100 message to are parent.
- GetParent()->SendMessage(WM_USER + 100, (UINT)hWnd, (UINT)hWnd);
-
- if (i == 1)
- {
- // A code of 1 returned from the callback function means that
- // the window should be ignored
- return;
- }
- #ifdef _DEBUG
- // If debugging, display the window handle at the point
- CString str;
- TCHAR caption[MAX_PATH];
- ::GetWindowText((HWND)m_targetWindow, caption, MAX_PATH);
- str.Format("%X at (%d, %d) %sn", m_targetWindow, point.x, point.y,
- caption);
- TRACE0(str);
- #endif
- // Get the DC of the window
- hDC = ::GetDC(hWnd);
- dc.Attach(hDC);
- hDC = ::GetDC(hWndOld);
- olddc.Attach(hDC);
- // Invert the window
- DrawWindowFrame(hWnd);
- if (hWndOld != (HWND)-1)
- DrawWindowFrame(hWndOld);
- // Save the previous HWND so we can see if the window is different
- hWndOld = hWnd;
- COleControl::OnMouseMove(nFlags, point);
- }
- #pragma pack(1)
- typedef struct
- {
- WINFINDERCALLBACK pfn;
- } *LPWINFINDERCALLBACKSTRUCT;
- #pragma pack()
- // To set the callback function, post a WM_SETWFCALLBACK or WM_USER + 100
- // message to this control with the LPARAM being the address of the
- // function and WPARAM set to zero. Setting WPARAM to a non-zero value
- // will return the address of the existing callback function and not set
- // it (the return value is always the callback function, but if WPARAM
- // is non-zero the callback function will not be set).
- LRESULT CWinFinderCtrl::OnSetWFCallback(WPARAM wParam, LPARAM lParam)
- {
- WINFINDERCALLBACK pfn;
- LPWINFINDERCALLBACKSTRUCT wfcbs;
- wfcbs = (LPWINFINDERCALLBACKSTRUCT)lParam;
-
- pfn = wfcbs->pfn;
- if (wParam != 0)
- return (long)m_pfnWindowSelectedCallback;
- else
- m_pfnWindowSelectedCallback = pfn;
- return (long)m_pfnWindowSelectedCallback;
- }
- OLE_HANDLE CWinFinderCtrl::GetHwnd()
- {
- return (unsigned int)m_hWnd;
- return NULL;
- }
- void CWinFinderCtrl::SetHwnd(OLE_HANDLE nNewValue)
- {
- // TODO: Add your property handler here
- SetModifiedFlag();
- }