utility.cpp

来自「一个完整的IE插件的程序的源代码(有些参考价值)」· C++ 代码 · 共 316 行

CPP
316
字号
// utility.cpp
//
// @author christian oetterli
//

#include "stdafx.h"
#include "utility.h"

CString getLastErrorMessage()
{
	DWORD lastError = GetLastError();
	CString msg;

	if (lastError)
	{
		LPWSTR buf;

		FormatMessage(
			  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
			, 0
			, lastError
			, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
			, (LPWSTR)&buf
			, 0
			, 0);

		msg = buf;
		
		LocalFree(buf);
	}

	return msg;
}

// class CComInit

CComInit::CComInit()
{
	CoInitialize(0);
}

CComInit::~CComInit()
{
	CoUninitialize();
}

// class CPoint

CPoint::CPoint()
{
}

CPoint::CPoint(long newX, long newY)
{
	x = newX;
	y = newY;
}

CPoint::CPoint(const POINTL &p)
{
	x = p.x;
	y = p.y;
}

CPoint::CPoint(const POINT &src)
{
	x = src.x;
	y = src.y;
}

void CPoint::move(int cx, int cy)
{
	x += cx;
	y += cy;
}

// class CRect

CRect::CRect()
{
}

CRect::CRect(long newLeft, long newTop, long newRight, long newBottom)
{
	left = newLeft;
	top = newTop;
	right = newRight;
	bottom = newBottom;
}

CRect::CRect(const RECT &src)
{
	CopyRect(this, &src);
}

CRect::CRect(RECTL src)
{
	left = src.left;
	top = src.top;
	right = src.right;
	bottom = src.bottom;
}

CRect::CRect(POINT point, SIZE size)
{
	left = point.x;
	top = point.y;
	right = left + size.cx;
	bottom = top + size.cy;
}

CRect::CRect(POINT leftTop, POINT rightBottom)
{
	left = leftTop.x;
	top = leftTop.y;
	right = rightBottom.x;
	bottom = rightBottom.y;
}

CRect::CRect(LPCRECT src)
{
	CopyRect(this, src);
}

CRect::operator LPRECT()
{
	return this;
}

CRect::operator LPCRECT() const
{
	return this;
}

const RECT& CRect::operator =(const RECT& src)
{
	return *this = src;
}

long CRect::height() const
{
	return bottom - top;
}

long CRect::width() const
{
	return right - left;
}

CPoint CRect::leftTop() const
{
	return CPoint(left, top);
}

CPoint CRect::rightBottom() const
{
	return CPoint(right, bottom);
}

bool CRect::isPointInMe(CPoint p) const
{
	return 0 != PtInRect(this, p);
}

// class CSize

CSize::CSize()
{
}

CSize::CSize(long newCx, long newCy)
{
	cx = newCx;
	cy = newCy;
}

CSize::CSize(const SIZE &src)
{
	cx = src.cx;
	cy = src.cy;
}

// class CParams

CParams::CParams()
{
}

CParams::CParams(variant p)
{
	push_back(p);
}

CParams::CParams(variant p1, variant p2)
{
	*this = CParams(p1);
	
	push_back(p2);
}

CParams::CParams(variant p1, variant p2, variant p3)
{
	*this = CParams(p1, p2);
	
	push_back(p3);
}

// class CIcons

CIcons::~CIcons()
{
	destroy();
}

void CIcons::destroy()
{
	CIcons::iterator icon;

	for (icon = begin(); icon != end(); icon++)
	{
		DestroyIcon(*icon);
	}

	clear();
}

// class CDC

CDC::CDC()
	: dc(0)
{
}

CDC::CDC(HDC newDC)
	: dc(newDC)
{
}

CDC::CDC(const CDC &src)
{
	dc = src.dc;
}

CDC::operator HDC() const
{
	return getDC();
}

HDC CDC::getDC() const
{
	return dc;
}

void CDC::fillSolidRect(LPCRECT rect, COLORREF color)
{
	HDC dc = getDC();

	SetBkColor(dc, color);
	ExtTextOut(dc, 0, 0, ETO_OPAQUE, rect, 0, 0, 0);
}

void CDC::draw3dRect(LPCRECT rect, COLORREF colorLeftTop, COLORREF colorRightBottom)
{
	HDC dc = getDC();

	CRect rc = rect;
	CPoint leftTop = rc.leftTop();
	int
		  cx = rc.width()
		, cy = rc.height();

	fillSolidRect(CRect(leftTop, CSize(cx - 1, 1)), colorLeftTop);
	fillSolidRect(CRect(leftTop, CSize(1, cy - 1)), colorLeftTop);
	fillSolidRect(CRect(CPoint(rc.left + cx, rc.top), CSize(-1, cy)), colorRightBottom);
	fillSolidRect(CRect(CPoint(rc.left, rc.top + cy), CSize(cx, -1)), colorRightBottom);
}

void registerToolbarBand(const CLSID &clsid, bool doRegister)
{
	ICatRegisterPtr cat(CLSID_StdComponentCategoriesMgr);

	CLSID catIds[1];
	catIds[0] = CATID_DeskBand;

	if (doRegister)
	{
		cat->RegisterClassImplCategories(clsid, 1, catIds);
	}
	else
	{
		cat->UnRegisterClassImplCategories(clsid, 1, catIds);
	}
	
	// create or delete internet explorer toolbar entry
	CRegKey key;
	
	if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Internet Explorer\\Toolbar"))
	{
		LPOLESTR clazz;
		
		if (SUCCEEDED(StringFromCLSID(clsid, &clazz)))
		{
			if (doRegister)
			{
				key.SetValue(L"", clazz);
			}
			else
			{
				key.DeleteValue(clazz);
			}

			CoTaskMemFree(clazz);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?