⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.h

📁 Windows Mobile平台上使用GDI+。GDI+功能很强大
💻 H
字号:
// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#if !defined(_TESTLIBGDIPLUSCE_MAINFRM_H_)
#define _TESTLIBGDIPLUSCE_MAINFRM_H_

#include "STScreenBuffer.h"
#include "dotnetlike.h"
#include "gdiplushelper.h"






/////////////////////
// CMainFrame : Application main window

class CMainFrame : 
		public CFrameWindowImpl<CMainFrame>, 
		public CUpdateUI<CMainFrame>,
		public CMessageFilter, public CIdleHandler,
		public CAppWindow<CMainFrame>
{
	CRect ClientRectangle;


	typedef std::vector<Gdiplus::PointF> PointFList;
	typedef std::vector<Gdiplus::PointF>::iterator PointFListIt;
	typedef std::vector<GraphicsPathPtr> GraphicsPathList;
	typedef std::vector<GraphicsPathPtr>::iterator GraphicsPathListIt;

	bool m_bSaveFile;
	IImagingFactory* m_pImgFactory;
	PenPtr penWrite;
	PointFList allPoints;
	GraphicsPathList allPaths;
	GraphicsPathPtr path;
	BitmapPtr bmp;

	//::CStreamOnFile stream;
	//CStreamOnFile stream( _T("\\My Documents\\My Pictures\\Spring.jpg") );

	

// Data and declarations
public:
	DECLARE_APP_FRAME_CLASS(NULL, IDR_MAINFRAME, L"Software\\WTL\\TestLibGDIPlusCE")

	CMainFrame():
	m_pImgFactory(NULL),
	m_bSaveFile(false)
	{
	}

// CMessageFilter pure virtual definition
	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
		return CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg);
	}
	
// UpdateUI operations and map
	virtual BOOL OnIdle()
	{	
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CMainFrame)
	END_UPDATE_UI_MAP()

// Message map and handlers
	BEGIN_MSG_MAP(CMainFrame)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_LBUTTONDOWN,	OnLButtonDown )
		MESSAGE_HANDLER(WM_LBUTTONUP,	OnLButtonUp )
		MESSAGE_HANDLER(WM_MOUSEMOVE,	OnMouseMove )
		MESSAGE_HANDLER(WM_DESTROY,	OnDestroy )
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
		COMMAND_ID_HANDLER(ID_FILE_CLEAR, OnMenuClear)
		COMMAND_ID_HANDLER(ID_FILE_SAVE, OnFileSave)
		COMMAND_ID_HANDLER(ID_MORE_PENDEMO, OnMenuExercise)
		COMMAND_ID_HANDLER(ID_MORE_BRUSHDEMO, OnMenuExercise)
		CHAIN_MSG_MAP(CAppWindow<CMainFrame>)
		CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
		CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
	END_MSG_MAP()

// Creation and destruction
	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		//AtlCreateEmptyMenuBar(m_hWnd);
		AtlCreateMenuBar(m_hWnd, IDR_MAINFRAME);

		::SHDoneButton( m_hWnd, SHDB_SHOWCANCEL );

		// register object for message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->AddMessageFilter(this);
		pLoop->AddIdleHandler(this);
		
		CoCreateInstance(CLSID_ImagingFactory, NULL, 
			CLSCTX_INPROC_SERVER,__uuidof(IImagingFactory),
			(void **)&m_pImgFactory);

		//bmp = Bitmap::FromStream(&stream);
		//bmp = Bitmap::FromFile(_T("\\My Documents\\My Pictures\\Spring.jpg"));
		bmp = Bitmap::FromResource(ModuleHelper::GetResourceInstance(), MAKEINTRESOURCE(IDR_JPEG2));
		//bmp = Bitmap::FromResource(ModuleHelper::GetResourceInstance(), MAKEINTRESOURCE(IDB_BITMAP1));

		penWrite = new Pen(Color::Blue, 3);
		path = new GraphicsPath(FillMode::FillModeAlternate);


		UpdateLayout();

		GetClientRectange();

		return bHandled = FALSE;
	}

	void GetClientRectange()
	{
		GetClientRect(ClientRectangle);

		HWND hWnCmdBar = SHFindMenuBar( m_hWnd );
		if (hWnCmdBar != NULL)
		{
			RECT rect;
			::GetWindowRect(hWnCmdBar, &rect);
			ClientRectangle.bottom -= (rect.bottom - rect.top);
		}
	}
	
	void TestGdiPlus(GraphicsPtr graphics)
	{
		graphics->DrawImage(&*bmp, 0, 0, ClientRectangle.Width(), ClientRectangle.Height());

		PenPtr pen;

		graphics->SetSmoothingMode(SmoothingMode::SmoothingModeAntiAlias);

		pen = new Pen(Color::Black, 15);
		pen->SetEndCap(LineCap::LineCapRound);
		pen->SetStartCap(LineCap::LineCapRound);

		pen->SetWidth(3);

		pen->SetColor(Color(0x7f7f7f7f));
		pen->SetWidth(40);
		graphics->DrawLine(&*pen, 20, 20, ClientRectangle.right - 20, ClientRectangle.bottom - 20);
		graphics->DrawLine(&*pen, ClientRectangle.right - 20, 20, 20, ClientRectangle.bottom - 20);

		SmoothingMode mode = graphics->GetSmoothingMode();
		graphics->SetSmoothingMode(SmoothingMode::SmoothingModeAntiAlias);

		/*foreach(GraphicsPath p in allPaths)
			graphics.DrawPath(penWrite, p);*/
		GraphicsPathListIt it;
		for (it = allPaths.begin(); it != allPaths.end(); ++it) {
			GraphicsPathPtr p = *it;
			graphics->DrawPath(penWrite, p);

		}
		

		graphics->SetSmoothingMode(mode);

		DELETE_OBJ(pen);
	}

	void AddStroke(int x, int y)
	{
		//ATLTRACE(_T("AddStroke(%d,%d) - allPoints size = %d\r\n"), x, y, allPoints.size());
		allPoints.push_back(PointF(x, y));
		if (allPoints.size() == 1)
			return;

		if (path)
		{
			path->AddLine(allPoints[allPoints.size() - 2], allPoints[allPoints.size() - 1]);

			HDC hdc = GetDC();
			if (hdc)
			{
				GraphicsPtr g = new Graphics(hdc);
				SmoothingMode mode = g->GetSmoothingMode();
				g->SetSmoothingMode(SmoothingMode::SmoothingModeAntiAlias);
				g->DrawLine(&*penWrite, allPoints[allPoints.size()- 2], allPoints[allPoints.size() - 1]);
				g->SetSmoothingMode(mode);
				
				DELETE_OBJ(g);
			}
		}
	}

	LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		AddStroke(GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
		return 0;
	}

	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
#if USEMARTPOINTER == 0
		DELETE_OBJ(penWrite);
		DELETE_OBJ(path);
		DELETE_OBJ(bmp);

		GraphicsPathListIt it;
		for (it = allPaths.begin(); it != allPaths.end(); ++it) {
			GraphicsPathPtr pPathPtr = *it;
			DELETE_OBJ(pPathPtr);
		}
#endif

		if (m_pImgFactory)
			m_pImgFactory->Release();

		return 0;
	}

	LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		AddStroke(GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
		return 0;
	}

	LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		DELETE_OBJ(path); 
		path = new GraphicsPath(FillMode::FillModeAlternate);
		path->AddBeziers(&allPoints[0], allPoints.size());
		allPaths.push_back(&*path);
		allPoints.clear();
		path = new GraphicsPath();

		return 0;
	}

	LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
	{
		CPaintDC dc(this->m_hWnd);

		if (::IsWindow(m_hWnd))
		{
			// Construct a memory DC ie declare a Bitmap with same height, width as screen
			BitmapPtr bmp = new Bitmap(ClientRectangle.Width(), ClientRectangle.Height(), PixelFormat24bppRGB);
			GraphicsPtr g = Graphics::FromImage(&*bmp);

			//GraphicsPtr g = Graphics::FromHDC((HDC)dc);
			TestGdiPlus(g);


			//-------------------------------------------------------------
			// With original managed code this part is done with 
			// System.Drawing (.NET GDI) and not with GDI+
			CDC dc2(g->GetHDC());

			CFont font;
			font.CreateFont(-11, 0,0,0,FW_BOLD,0,0,0,1,0,0,0,0, _T("Tahoma"));
			//font.CreatePointFont(120, _T("Tahoma"), dc, true);
			dc2.SetTextColor(RGB(255,0,0));
			dc2.SetBkMode(TRANSPARENT);

			HFONT oldFont = dc2.SelectFont(font);
			dc2.DrawText(L"Use stylus to write on this screen", -1, CRect(0,0, ClientRectangle.Width(), 30), DT_CENTER);
			
			dc2.SelectFont(oldFont);

			g->ReleaseHDC(dc2); 
			//-------------------------------------------------------------
			GraphicsPtr gScreen = Graphics::FromHDC((HDC)dc);
			gScreen->DrawImage(&*bmp, 0, 0, ClientRectangle.Width(), ClientRectangle.Height());
			
			if (m_bSaveFile)
			{
				m_bSaveFile = false;

				CLSID encoderClassId;
				if (GetEncoderClsid(m_pImgFactory, L"image/png", &encoderClassId) == TRUE)
					GdiplusSaveBitmap(m_pImgFactory, &*bmp, _T("testsave.png"), &encoderClassId);

			}

			DELETE_OBJ(g);
			DELETE_OBJ(gScreen);
			DELETE_OBJ(bmp);
		}
		
		return 0;
	}

	LRESULT OnFileSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CLSID encoderClassId;
		HBITMAP hBmp;
		BitmapPtr bmp = NULL;

		m_bSaveFile = true;
		Invalidate();

		//HDC hdc = GetDC();

		//int nWidth = ClientRectangle.Width();
		//int nHeight = ClientRectangle.Height();

		//HDC hdcMem = CreateCompatibleDC(hdc);
		//bmp = new Bitmap(nWidth, nHeight, PixelFormat24bppRGB);
		//bmp->GetHBITMAP(Color::White, &hBmp);

		////hBmp = CreateCompatibleBitmap( hdcMem, nWidth, nHeight);
		//SelectObject( hdcMem,  hBmp);
		//BOOL bRet = BitBlt(hdcMem, 0, 0, nWidth, nHeight, hdc, 0,0,SRCCOPY);

	
		////bmp->GetHBITMAP(Color::White, &hBmp);
		////bmp = new Bitmap(nWidth, nHeight, PixelFormat24bppRGB);
		//BitmapPtr bmp2 = new Bitmap(hBmp, NULL);
		////GraphicsPtr g = Graphics::FromImage(&*bmp);
		////BitmapPtr bmp3 = new Bitmap(hBmp, NULL);

		////g->DrawImage(&*bmp3, 0, 0, ClientRectangle.Width(), ClientRectangle.Height());

		////HDC hdcGdip = g->GetHDC();
		//
		//
		/////*BitmapPtr */bmp = Bitmap::FromHBITMAP(hBmp, NULL); //new Bitmap(nWidth, nHeight, PixelFormat24bppRGB);
		//if (GetEncoderClsid(m_pImgFactory, L"image/png", &encoderClassId) == TRUE)
		//	GdiplusSaveBitmap(m_pImgFactory, &*bmp2, _T("testsave.png"), &encoderClassId);

		////g->ReleaseHDC(hdcGdip);

		//DELETE_OBJ(bmp);
		////DELETE_OBJ(g);

		//if (hdc)
		//	DeleteDC(hdc);
		//if (hBmp)
		//	DeleteObject(hBmp);

		return 0;
	}

	LRESULT OnMenuClear(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		/* allPoints.Clear();
            path.Clear();
            foreach (GraphicsPath p in allPaths)
                p.Clear();
            allPaths.Clear();
            Invalidate();*/

		allPoints.clear();
		
		DELETE_OBJ(path);
		path = new GraphicsPath(FillMode::FillModeAlternate);

		ATLTRACE(_T("OnMenuClear : Releasing %d GraphicsPath\r\n"), allPaths.end() - allPaths.begin());
		GraphicsPathListIt it;
		for (it = allPaths.begin(); it != allPaths.end(); ++it)
			delete *it;

		allPaths.clear();
		Invalidate();

		return 0;
	}

	
	LRESULT OnMenuExercise(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		::MessageBox(m_hWnd, 
			_T("Left as an exercise - see original article http://community.opennetcf.com/articles/cf/archive/2007/10/31/using-gdi-on-windows-mobile.aspx"),
			0, 
			0);

		return 0;
	}

	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		PostMessage(WM_CLOSE);
		return 0;
	}

	LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
	{
		return OnCancel(0,0,0,bHandled);
	}
	
	
};

/////////////////////////////////////////////////////////////////////////////

#endif // !defined(_TESTLIBGDIPLUSCE_MAINFRM_H_)

⌨️ 快捷键说明

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