📄 adddate.cpp
字号:
#include "stdafx.h"
#include "ControlCommands.h"
#include "AddDate.h"
// Font descriptor used to create the IFont object
static FONTDESC s_fontDesc =
{ sizeof(FONTDESC), OLESTR("Arial"), FONTSIZE(25), FW_BOLD,
DEFAULT_CHARSET, FALSE, FALSE, FALSE };
HRESULT AddDateTool::FinalConstruct()
{
// Create the button bitmap
m_hBitmap = ::LoadBitmap(_Module.m_hInst, MAKEINTRESOURCE(IDB_DATE));
// Create the custom font
::OleCreateFontIndirect(&s_fontDesc, IID_IFont, (void**)&m_ipFont);
return m_ipHookHelper.CreateInstance(CLSID_HookHelper);
}
void AddDateTool::FinalRelease()
{
::DeleteObject(m_hBitmap);
}
/////////////////////////////////////////////////////////////////////////////
// ICommand
STDMETHODIMP AddDateTool::get_Enabled(VARIANT_BOOL *Enabled)
{
if (Enabled == NULL)
return E_POINTER;
IActiveViewPtr ipActiveView;
m_ipHookHelper->get_ActiveView(&ipActiveView);
*Enabled = (ipActiveView != 0) ? VARIANT_TRUE : VARIANT_FALSE;
return S_OK;
}
STDMETHODIMP AddDateTool::get_Checked(VARIANT_BOOL *Checked)
{
if (Checked == NULL)
return E_POINTER;
*Checked = VARIANT_FALSE;
return S_OK;
}
STDMETHODIMP AddDateTool::get_Name(BSTR *Name)
{
if (Name == NULL)
return E_POINTER;
// Get the bstr from the string table
CComBSTR bstrTooltip;
bstrTooltip.LoadString(IDS_NAME);
*Name = bstrTooltip.Detach();
return S_OK;
}
STDMETHODIMP AddDateTool::get_Caption(BSTR *Caption)
{
if (Caption == NULL)
return E_POINTER;
// Get the bstr from the string table
CComBSTR bstrTooltip;
bstrTooltip.LoadString(IDS_CAPTION);
*Caption = bstrTooltip.Detach();
return S_OK;
}
STDMETHODIMP AddDateTool::get_Tooltip(BSTR *Tooltip)
{
if (Tooltip == NULL)
return E_POINTER;
// Get the bstr from the string table
CComBSTR bstrTooltip;
bstrTooltip.LoadString(IDS_TOOLTIP);
*Tooltip = bstrTooltip.Detach();
return S_OK;
}
STDMETHODIMP AddDateTool::get_Message(BSTR *Message)
{
if (Message == NULL)
return E_POINTER;
// Get the bstr from the string table
CComBSTR bstrTooltip;
bstrTooltip.LoadString(IDS_MESSAGE);
*Message = bstrTooltip.Detach();
return S_OK;
}
STDMETHODIMP AddDateTool::get_HelpFile(BSTR *HelpFile)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::get_HelpContextID(LONG *helpID)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::get_Bitmap(OLE_HANDLE *Bitmap)
{
if (Bitmap == NULL)
return E_POINTER;
if (m_hBitmap == 0)
return E_NOTIMPL;
*Bitmap = (OLE_HANDLE) m_hBitmap;
return S_OK;
}
STDMETHODIMP AddDateTool::get_Category(BSTR *categoryName)
{
if (categoryName == NULL)
return E_POINTER;
// Get the bstr from the string table
CComBSTR bstrTooltip;
bstrTooltip.LoadString(IDS_CATEGORY);
*categoryName = bstrTooltip.Detach();
return S_OK;
}
STDMETHODIMP AddDateTool::OnCreate(IDispatch *hook)
{
return m_ipHookHelper->putref_Hook(hook);
}
STDMETHODIMP AddDateTool::OnClick()
{
// Not implemented
return E_NOTIMPL;
}
/////////////////////////////////////////////////////////////////////////////
// ITool
STDMETHODIMP AddDateTool::get_Cursor(OLE_HANDLE * Cursor)
{
if (Cursor == NULL)
return E_POINTER;
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::OnMouseDown(LONG button, LONG shift, LONG x, LONG y)
{
// Get the active view
IActiveViewPtr ipActiveView;
HRESULT hr = m_ipHookHelper->get_ActiveView(&ipActiveView);
if (FAILED(hr)) return hr;
// Create a new text element
ITextElementPtr ipTextElement(CLSID_TextElement);
// Create a text symbol
ITextSymbolPtr ipTextSymbol(CLSID_TextSymbol);
// Set the symbol properties
ipTextSymbol->put_Font(m_ipFont);
ipTextElement->put_Symbol(ipTextSymbol);
// Set the current date into the text element
SYSTEMTIME SystemTime;
GetLocalTime(&SystemTime);
TCHAR tDate[256];
GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE, &SystemTime, 0, tDate, 256);
CComBSTR bstrDate(tDate);
ipTextElement->put_Text(bstrDate);
// Create a page point
IScreenDisplayPtr ipScreenDisplay;
ipActiveView->get_ScreenDisplay(&ipScreenDisplay);
IDisplayTransformationPtr ipDisplayTransformation;
ipScreenDisplay->get_DisplayTransformation(&ipDisplayTransformation);
IPointPtr ipPoint;
ipDisplayTransformation->ToMapPoint(x, y, &ipPoint);
// QI for IElement and set its geometry
IElementPtr ipElement = ipTextElement;
ipElement->put_Geometry(ipPoint);
// Add the element to the graphics container
IGraphicsContainerPtr ipGraphicsContainer;
ipActiveView->get_GraphicsContainer(&ipGraphicsContainer);
ipGraphicsContainer->AddElement(ipElement, 0);
// Refresh the graphics
ipActiveView->PartialRefresh(esriViewGraphics, 0, 0);
return S_OK;
}
STDMETHODIMP AddDateTool::OnMouseMove(LONG button, LONG shift, LONG x, LONG y)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::OnMouseUp(LONG button, LONG shift, LONG x, LONG y)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::OnDblClick()
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::OnKeyDown(LONG keyCode, LONG shift)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::OnKeyUp(LONG keyCode, LONG shift)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::OnContextMenu(LONG x, LONG y, VARIANT_BOOL *handled)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::Refresh(OLE_HANDLE hdc)
{
// Not implemented
return E_NOTIMPL;
}
STDMETHODIMP AddDateTool::Deactivate(VARIANT_BOOL *complete)
{
if (complete == NULL)
return E_POINTER;
*complete = VARIANT_TRUE;
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -