📄 jpgshowctlctl.cpp
字号:
// JpgShowCtlCtl.cpp : Implementation of the CJpgShowCtlCtrl ActiveX Control class.
#include "stdafx.h"
#include "JpgShowCtl.h"
#include "JpgShowCtlCtl.h"
#include "JpgShowCtlPpg.h"
#include <afxpriv.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CJpgShowCtlCtrl, COleControl)
/////////////////////////////////////////////////////////////////////////////
// Message map
BEGIN_MESSAGE_MAP(CJpgShowCtlCtrl, COleControl)
//{{AFX_MSG_MAP(CJpgShowCtlCtrl)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(CJpgShowCtlCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CJpgShowCtlCtrl)
DISP_FUNCTION(CJpgShowCtlCtrl, "ShowFileName", ShowFileName, VT_EMPTY, VTS_BSTR)
DISP_FUNCTION(CJpgShowCtlCtrl, "Erase", Erase, VT_EMPTY, VTS_NONE)
DISP_FUNCTION(CJpgShowCtlCtrl, "SetBorderColor", SetBorderColor, VT_EMPTY, VTS_COLOR)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
// Event map
BEGIN_EVENT_MAP(CJpgShowCtlCtrl, COleControl)
//{{AFX_EVENT_MAP(CJpgShowCtlCtrl)
EVENT_CUSTOM("DoubleMouse", FireDoubleMouse, VTS_NONE)
//}}AFX_EVENT_MAP
END_EVENT_MAP()
/////////////////////////////////////////////////////////////////////////////
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CJpgShowCtlCtrl, 1)
PROPPAGEID(CJpgShowCtlPropPage::guid)
END_PROPPAGEIDS(CJpgShowCtlCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CJpgShowCtlCtrl, "JPGSHOWCTL.JpgShowCtlCtrl.1",
0x8ba321d6, 0xeaea, 0x4b51, 0xa5, 0x97, 0x5f, 0xe5, 0x2f, 0xa2, 0x2e, 0x85)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CJpgShowCtlCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DJpgShowCtl =
{ 0xcccfafd1, 0x6b70, 0x4523, { 0x84, 0x8e, 0x74, 0x4e, 0x31, 0xcc, 0x22, 0x7b } };
const IID BASED_CODE IID_DJpgShowCtlEvents =
{ 0xfb22d2aa, 0x235b, 0x451c, { 0x9c, 0xa6, 0x5, 0x4f, 0x2d, 0x40, 0x6, 0x5b } };
/////////////////////////////////////////////////////////////////////////////
// Control type information
static const DWORD BASED_CODE _dwJpgShowCtlOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CJpgShowCtlCtrl, IDS_JPGSHOWCTL, _dwJpgShowCtlOleMisc)
/////////////////////////////////////////////////////////////////////////////
// CJpgShowCtlCtrl::CJpgShowCtlCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CJpgShowCtlCtrl
BOOL CJpgShowCtlCtrl::CJpgShowCtlCtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: Verify that your control follows apartment-model threading rules.
// Refer to MFC TechNote 64 for more information.
// If your control does not conform to the apartment-model rules, then
// you must modify the code below, changing the 6th parameter from
// afxRegApartmentThreading to 0.
if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_JPGSHOWCTL,
IDB_JPGSHOWCTL,
afxRegApartmentThreading,
_dwJpgShowCtlOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
/////////////////////////////////////////////////////////////////////////////
// CJpgShowCtlCtrl::CJpgShowCtlCtrl - Constructor
CJpgShowCtlCtrl::CJpgShowCtlCtrl()
{
InitializeIIDs(&IID_DJpgShowCtl, &IID_DJpgShowCtlEvents);
// TODO: Initialize your control's instance data here.
m_jpgFileName=_T("");
m_pPicture = NULL;
m_crBackground = ::GetSysColor(COLOR_3DFACE);
m_borderColor=RGB(192,192,0);
}
/////////////////////////////////////////////////////////////////////////////
// CJpgShowCtlCtrl::~CJpgShowCtlCtrl - Destructor
CJpgShowCtlCtrl::~CJpgShowCtlCtrl()
{
// TODO: Cleanup your control's instance data here.
if(m_pPicture != NULL)
{
m_pPicture->Release();
m_pPicture = NULL;
}
}
/////////////////////////////////////////////////////////////////////////////
// CJpgShowCtlCtrl::OnDraw - Drawing function
void CJpgShowCtlCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
CRect rcClient;
rcClient=rcBounds;
//涂上底色,并画框架
CBrush Brush;
Brush.CreateSolidBrush(m_borderColor);
pdc->FillRect(&rcClient, &Brush);
Brush.DeleteObject();
Brush.CreateSolidBrush(m_crBackground);
rcClient.top=rcBounds.top+4;
rcClient.bottom=rcClient.bottom-4;
rcClient.left=rcClient.left+4;
rcClient.right=rcClient.right-4;
pdc->FillRect(&rcClient, &Brush);
Brush.DeleteObject();
if(m_pPicture != NULL)
{
//获取调色板
HPALETTE hPal;
m_pPicture->get_hPal(reinterpret_cast<OLE_HANDLE *>(&hPal));
//图像尺寸
SIZE sizeHiMetric;
m_pPicture->get_Width(reinterpret_cast<OLE_XSIZE_HIMETRIC *>(&sizeHiMetric.cx));
m_pPicture->get_Height(reinterpret_cast<OLE_YSIZE_HIMETRIC *>(&sizeHiMetric.cy));
//转换为像素单位
const int HIMETRIC_PER_INCH = 2540;
const int nPixelsPerInchX = ::GetDeviceCaps(pdc->GetSafeHdc(), LOGPIXELSX);
const int nPixelsPerInchY = ::GetDeviceCaps(pdc->GetSafeHdc(), LOGPIXELSY);
SIZE sizePixel;
sizePixel.cx = (nPixelsPerInchX * sizeHiMetric.cx + HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;
sizePixel.cy = (nPixelsPerInchY * sizeHiMetric.cy + HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;
//设置调色板
CPalette *pPalOld = NULL;
if (hPal != NULL){
TRACE(_T("CPicViewCtrl::OnPaint - RealizePalette\n"));
pPalOld = pdc->SelectPalette(CPalette::FromHandle(hPal), FALSE);
pdc->RealizePalette();
}
//实际绘图区域
CRect rcDraw = rcClient;
//实际显示图形范围
SIZE sizeGraphRegion = sizeHiMetric;
//透明?
DWORD dwAttr;
if (FAILED(m_pPicture->get_Attributes(&dwAttr)) || (dwAttr & PICTURE_TRANSPARENT))
{
CDC MemDC;
MemDC.CreateCompatibleDC(pdc);
CBitmap Bmp;
Bmp.CreateCompatibleBitmap(pdc, rcDraw.Width(), rcDraw.Height());
CBitmap *pBmpOld = MemDC.SelectObject(&Bmp);
CPalette *pPalMemOld = NULL;
if (hPal != NULL){
pPalMemOld = MemDC.SelectPalette(CPalette::FromHandle(hPal), FALSE);
MemDC.RealizePalette();
}
CRect rc(0, 0, rcDraw.Width(), rcDraw.Height());
MemDC.FillSolidRect(&rc, m_crBackground);
m_pPicture->Render(
MemDC.GetSafeHdc(),
0, 0, rc.Width(), rc.Height(),
0, sizeHiMetric.cy, sizeGraphRegion.cx, -sizeGraphRegion.cy,
&rc
);
pdc->BitBlt(
rcDraw.left, rcDraw.top, rcDraw.Width(), rcDraw.Height(),
&MemDC,
0, 0,
SRCCOPY
);
MemDC.SelectObject(pBmpOld);
if (pPalMemOld != NULL){
ASSERT(hPal != NULL);
MemDC.SelectPalette(pPalMemOld, FALSE);
}
}
else
{
//调用IPicture的Render绘图
m_pPicture->Render(
pdc->GetSafeHdc(),
rcDraw.left, rcDraw.top, rcDraw.Width(), rcDraw.Height(),//+zoom,
0,sizeHiMetric.cy, sizeGraphRegion.cx, -sizeGraphRegion.cy,
&rcDraw
);
}
if (pPalOld != NULL)
{
ASSERT(hPal != NULL);
pdc->SelectPalette(pPalOld, FALSE);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CJpgShowCtlCtrl::DoPropExchange - Persistence support
void CJpgShowCtlCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
/////////////////////////////////////////////////////////////////////////////
// CJpgShowCtlCtrl::OnResetState - Reset control to default state
void CJpgShowCtlCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
/////////////////////////////////////////////////////////////////////////////
// CJpgShowCtlCtrl message handlers
void CJpgShowCtlCtrl::ShowFileName(LPCTSTR strFileName)
{
// TODO: Add your dispatch handler code here
m_borderColor=RGB(192,192,0);
if(m_pPicture != NULL)
{
m_pPicture->Release();
m_pPicture = NULL;
}
USES_CONVERSION;
HRESULT hr = ::OleLoadPicturePath(
const_cast<LPOLESTR>(T2COLE(strFileName)),
NULL,
0,
0,
IID_IPicture,
reinterpret_cast<LPVOID *>(&m_pPicture)
);
if(GetSafeHwnd())
Invalidate();
}
void CJpgShowCtlCtrl::Erase()
{
// TODO: Add your dispatch handler code here
if(m_pPicture != NULL)
{
m_pPicture->Release();
m_pPicture = NULL;
}
m_borderColor=m_crBackground;
if(GetSafeHwnd())
Invalidate();
}
void CJpgShowCtlCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetClientRect(rect);
double a=(rect.right-rect.left)/2;
double b=(rect.bottom-rect.top)/2;
double x=point.x-(rect.left+rect.right)/2;
double y=point.y-(rect.top+rect.bottom)/2;
if((x*x)/(a*a)+(y*y)/(b*b)<=1)
{
FireDoubleMouse();
}
COleControl::OnLButtonDown(nFlags, point);
}
void CJpgShowCtlCtrl::SetBorderColor(OLE_COLOR borderColor)
{
// TODO: Add your dispatch handler code here
m_borderColor=borderColor;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -