📄 pkidroptarget.cpp
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
PKIDropTarget.cpp - implements OLE IDropTarget for drag and drop
$Id: PKIDropTarget.cpp,v 1.6 2002/08/06 20:09:46 dallen Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"
// system header files
#include <windows.h>
#include <commctrl.h>
// project header files
#include "PKIDropTarget.h"
#include "PKIDataObject.h"
extern "C" {
typedef struct _PK *PPGPKEYSSTRUCT;
typedef struct _PSKS *PSKS;
BOOL PKDropKeys (PPGPKEYSSTRUCT ppks, BOOL bKeySet, HANDLE hMem);
BOOL PKInputFile (PPGPKEYSSTRUCT ppks, HANDLE hMem);
BOOL PKSplitDropKeys (PSKS psks, HANDLE hMem);
typedef struct _GM *PGROUPMAN;
BOOL GMDropKeys (PGROUPMAN pGM, HANDLE hMem);
BOOL GMImportGroups (PGROUPMAN pGM, LPSTR pszFile, HDROP hDrop);
BOOL GMSelectGroup (PGROUPMAN pGM, POINTL pt);
}
// ________________________________________
//
// IUnknown Methods
STDMETHODIMP CDropTarget::QueryInterface (
REFIID iid,
void FAR* FAR* ppv)
{
if ((iid == IID_IUnknown) || (iid == IID_IDropTarget))
{
*ppv = this;
++m_refs;
return NOERROR;
}
*ppv = NULL;
return E_NOINTERFACE;
}
STDMETHODIMP_(ULONG) CDropTarget::AddRef (void)
{
return ++m_refs;
}
STDMETHODIMP_(ULONG) CDropTarget::Release (void)
{
if (--m_refs == 0) {
delete this;
return 0;
}
return m_refs;
}
// ________________________________________
//
// CDropTarget Constructor
CDropTarget::CDropTarget (
HWND hwnd,
ULONG uWindowType,
VOID* pUserStruct)
{
m_refs = 1;
m_hwnd = hwnd;
m_bAcceptFmt = FALSE;
m_fmtDrop = 0;
m_bEnabled = FALSE;
m_uWindowType = uWindowType;
m_pUserStruct = pUserStruct;
}
// ________________________________________
//
// Enable/disable dropping. Used when dragging a key out
// of key manager.
void CDropTarget::Enable (BOOL bEnable)
{
m_bEnabled = bEnable;
}
// ________________________________________
//
// IDropTarget Methods
STDMETHODIMP CDropTarget::DragEnter (
LPDATAOBJECT pDataObj,
DWORD grfKeyState,
POINTL pt,
LPDWORD pdwEffect)
{
FORMATETC fmtetc;
// default to not allowing drop
m_bAcceptFmt = FALSE;
*pdwEffect = DROPEFFECT_NONE;
if (!m_bEnabled)
return NOERROR;
// Does the drag source provide CF_TEXT?
fmtetc.cfFormat = CF_TEXT;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
if (pDataObj->QueryGetData (&fmtetc) == NOERROR)
{
m_bAcceptFmt = TRUE;
m_fmtDrop = CF_TEXT;
*pdwEffect = DROPEFFECT_COPY;
}
if ((m_uWindowType == PK_IDROP_KEYLIST) ||
(m_uWindowType == PK_IDROP_GROUPLIST))
{
// Does the drag source provide CF_HDROP?
fmtetc.cfFormat = CF_HDROP;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
if (pDataObj->QueryGetData (&fmtetc) == NOERROR)
{
m_bAcceptFmt = TRUE;
m_fmtDrop = CF_HDROP;
*pdwEffect = DROPEFFECT_COPY;
}
}
return NOERROR;
}
STDMETHODIMP CDropTarget::DragOver (
DWORD grfKeyState,
POINTL pt,
LPDWORD pdwEffect)
{
*pdwEffect = DROPEFFECT_NONE;
if (m_bAcceptFmt && m_bEnabled)
{
switch (m_uWindowType) {
case PK_IDROP_KEYLIST :
case PK_IDROP_SPLITLIST :
*pdwEffect = DROPEFFECT_COPY;
break;
case PK_IDROP_GROUPLIST :
if (m_fmtDrop == CF_HDROP)
*pdwEffect = DROPEFFECT_COPY;
else
{
if (GMSelectGroup ((PGROUPMAN)m_pUserStruct, pt))
*pdwEffect = DROPEFFECT_COPY;
}
break;
}
}
return NOERROR;
}
STDMETHODIMP CDropTarget::DragLeave ()
{
m_bAcceptFmt = FALSE;
return NOERROR;
}
STDMETHODIMP CDropTarget::Drop (
LPDATAOBJECT pDataObj,
DWORD grfKeyState,
POINTL pt,
LPDWORD pdwEffect)
{
FORMATETC fmtetc;
STGMEDIUM medium;
HGLOBAL hGlobal;
HRESULT hr;
*pdwEffect = DROPEFFECT_NONE;
hr = NOERROR;
if (m_bAcceptFmt && m_bEnabled)
{
switch (m_uWindowType) {
case PK_IDROP_KEYLIST :
// User has dropped on us. First, try getting data in the
// private PGP format which is always a complete representation
// of the key(s)
fmtetc.cfFormat = RegisterClipboardFormat (CFSTR_PGPKEYSETFORMAT);
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
hr = pDataObj->GetData (&fmtetc, &medium);
if (!FAILED(hr))
{
// Import the data and release it.
hGlobal = medium.hGlobal;
PKDropKeys ((PPGPKEYSSTRUCT)m_pUserStruct, TRUE, hGlobal);
ReleaseStgMedium (&medium);
*pdwEffect = DROPEFFECT_COPY;
return NOERROR;
}
// Next try getting CF_TEXT data from drag source
fmtetc.cfFormat = CF_TEXT;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
hr = pDataObj->GetData (&fmtetc, &medium);
if (!FAILED(hr))
{
// Import the data and release it.
hGlobal = medium.hGlobal;
PKDropKeys ((PPGPKEYSSTRUCT)m_pUserStruct, FALSE, hGlobal);
ReleaseStgMedium (&medium);
*pdwEffect = DROPEFFECT_COPY;
return NOERROR;
}
// finally try getting CF_HDROP data from drag source
fmtetc.cfFormat = CF_HDROP;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
hr = pDataObj->GetData (&fmtetc, &medium);
if (!FAILED(hr))
{
// Import the data and release it.
hGlobal = medium.hGlobal;
PKInputFile ((PPGPKEYSSTRUCT)m_pUserStruct, hGlobal);
ReleaseStgMedium (&medium);
*pdwEffect = DROPEFFECT_COPY;
return NOERROR;
}
break;
case PK_IDROP_SPLITLIST :
// User has dropped on us. First, try getting data in text format
fmtetc.cfFormat = CF_TEXT;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
hr = pDataObj->GetData (&fmtetc, &medium);
if (!FAILED(hr))
{
// Import the data and release it.
hGlobal = medium.hGlobal;
PKSplitDropKeys ((PSKS)m_pUserStruct, hGlobal);
ReleaseStgMedium (&medium);
*pdwEffect = DROPEFFECT_COPY;
return NOERROR;
}
break;
case PK_IDROP_GROUPLIST :
// User has dropped on us. Try getting CF_TEXT data from drag source
fmtetc.cfFormat = CF_TEXT;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
hr = pDataObj->GetData (&fmtetc, &medium);
if (!FAILED(hr))
{
// Import the data and release it.
hGlobal = medium.hGlobal;
GMDropKeys ((PGROUPMAN)m_pUserStruct, hGlobal);
ReleaseStgMedium (&medium);
*pdwEffect = DROPEFFECT_COPY;
return NOERROR;
}
// no CF_TEXT, try getting CF_HDROP data from drag source
fmtetc.cfFormat = CF_HDROP;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
hr = pDataObj->GetData (&fmtetc, &medium);
if (!FAILED(hr))
{
// Import the data and release it.
hGlobal = medium.hGlobal;
GMImportGroups ((PGROUPMAN)m_pUserStruct, NULL, (HDROP)hGlobal);
ReleaseStgMedium (&medium);
*pdwEffect = DROPEFFECT_COPY;
return NOERROR;
}
break;
}
}
return hr;
}
// ________________________________________
//
// Interface to C code
extern "C" {
LPDROPTARGET PKCreateDropTarget (
HWND hwnd,
ULONG uWindowType,
VOID* pUserStruct)
{
return ((LPDROPTARGET) new CDropTarget (hwnd, uWindowType, pUserStruct));
}
void PKReleaseDropTarget (LPDROPTARGET pDropTarget)
{
pDropTarget->Release ();
}
void PKEnableDropTarget (CDropTarget* pDropTarget, BOOL bEnable)
{
pDropTarget->Enable (bEnable);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -