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

📄 text5.cpp

📁 基于WTL框架
💻 CPP
字号:
// text5.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "text5.h"
#include <windows.h>
#include <commctrl.h>
#include "resourceppc.h"

//LPCTSTR g_lpStrDraw = L"Windows CE是一个实时操作系统,广泛用于小尺寸设备,包括,工业控制,通讯控制,销售终端,消费电子等。"
//L"QP智能工作室是国内最早从事智能手机软件解决方案的实力团队之一,研发了一系列深受用户喜爱的智能软件。"
//L"QP智能工作室致力于创造价值,装扮智能生活。"
//L"创建Microsoft Excel移动版表格,浏览PowerPoint演示等。...\
// ...sdfsdfgsdgsdfg g g g g g g g dfgdfhhhhhhhhhh34534661234567890你好的dfs三扥所读。";

LPTSTR g_lpStrDraw;
TCHAR* g_pStrDraw;

#define LEN_STRINGSTEP 60 //每次装载的字符数

HBITMAP g_hBitmap;

// 全局变量:
HINSTANCE			g_hInst;			// 当前实例
HWND				g_hWndMenuBar;		// 菜单栏句柄
#define  MAX_LOADSTRING 1024
// 此代码模块中包含的函数的前向声明:
ATOM			MyRegisterClass(HINSTANCE, LPTSTR);
BOOL			InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPTSTR    lpCmdLine,
				   int       nCmdShow)
{
	MSG msg;

	// 执行应用程序初始化:
	if (!InitInstance(hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	HACCEL hAccelTable;
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEXT5));

	// 主消息循环:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEXT5));
	wc.hCursor       = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = szWindowClass;

	return RegisterClass(&wc);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	//TCHAR* strPath = _T("\\Program Files\\text5\\Picture.png");
	//g_hBitmap = ::SHLoadImageFile(strPath);
	//TCHAR* strPath = _T("\\Program Files\\text5\\Picture.png");
	g_hBitmap = ::LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));

	HWND hWnd;
	TCHAR szTitle[MAX_LOADSTRING];		// 标题栏文本
	TCHAR szWindowClass[MAX_LOADSTRING];	// 主窗口类名

	g_hInst = hInstance; // 将实例句柄存储在全局变量中

	// 在应用程序初始化期间,应调用一次 SHInitExtraControls 以初始化
	// 所有设备特定控件,例如,CAPEDIT 和 SIPPREF。
	SHInitExtraControls();

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
	LoadString(hInstance, IDC_TEXT5, szWindowClass, MAX_LOADSTRING);

	//如果它已经在运行,则将焦点置于窗口上,然后退出
	hWnd = FindWindow(szWindowClass, szTitle);	
	if (hWnd) 
	{
		// 将焦点置于最前面的子窗口
		// “| 0x00000001”用于将所有附属窗口置于前台并
		// 激活这些窗口。
		SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
		return 0;
	} 

	if (!MyRegisterClass(hInstance, szWindowClass))
	{
		return FALSE;
	}

	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	// 使用 CW_USEDEFAULT 创建主窗口时,将不会考虑菜单栏的高度(如果创建了一个
	// 菜单栏)。因此,我们要在创建窗口后调整其大小
	// 如果菜单栏存在
	if (g_hWndMenuBar)
	{
		RECT rc;
		RECT rcMenuBar;

		GetWindowRect(hWnd, &rc);
		GetWindowRect(g_hWndMenuBar, &rcMenuBar);
		rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

		MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);


	return TRUE;
}

//! 获取指定字体的行高
int GetFontHeight(IN HDC hdc) 
{

	TEXTMETRIC	tm;
	GetTextMetrics(hdc,&tm);
	return (tm.tmAscent+tm.tmDescent+tm.tmExternalLeading);

}
//每次计算的字符长度
#define MAXLEN_LDSTRING 10 



BOOL DrawParagraph(IN HDC hdc,IN RECT * lpRect,IN LPCTSTR  szText, IN OUT int & nLen ,IN BOOL bForeSpace, IN int  &nLineSpace, OUT int &nHeight)
{

	int oldMode = SetBkMode(hdc, TRANSPARENT);

	BOOL bRet = false;
	nHeight = 0;

	if( !szText || !hdc || ! lpRect )
		return bRet;

	if (-1 == nLen)
		nLen = _tcslen(szText);

	if(!nLineSpace)
	{
		nLineSpace =  GetFontHeight (hdc);
	}


	//- 如果是空文本,直接空行
	if( !nLen)
	{	
		nHeight += nLineSpace;
		return bRet;
	}

	//int nCounted = 0;//已经计算长度的字符

	int alpDx[LEN_STRINGSTEP];//存放字符宽度

	int nMaxExtent = lpRect->right - lpRect->left;

	//int nline = 0;//绘制的行数

	int nCharsDrawed = 0; //

	int alpSegText[LEN_STRINGSTEP+1];
	// int nSeg;

	while (nCharsDrawed < nLen && nHeight < (lpRect->bottom -lpRect->top))
	{

		//宽度自动匹配用
		int nFillSpace = 0;
		int nPreCharCounted =0;


		////////绘制每行
		SIZE size;
		int  nFit;

		bool bLastWords = false;

		int  nSpaceLeft  = nMaxExtent;
		int  nCharCounted = 0 ;//已经计算的字符数	

		while(nSpaceLeft >0 )
		{
			int nToCount =  LEN_STRINGSTEP;//将计算的字符串长度

			if( nToCount + nCharCounted +nCharsDrawed > nLen )//最后一行
			{
				nToCount =  nLen -  nCharCounted -nCharsDrawed;
			}

			if(! GetTextExtentExPoint(hdc, szText + nCharsDrawed + nCharCounted, nToCount, nSpaceLeft ,&nFit,alpDx, &size))
				return false;

			const LPCTSTR p = szText + nCharsDrawed + nFit;
			if ( wcsstr(p, L",") == p || wcsstr( p, L"。") == p )
			{
				-- nFit;
			}

			if( nToCount <= nFit /*&& nSpaceLeft < size.cx */)//可以容纳全部 ,有剩余空间			 
			{    
				if( nToCount ==  nLen -  nCharCounted - nCharsDrawed) //最后的几个字			
				{
					nCharCounted +=  nFit;
					bLastWords = true;       
					break;
				}
				else
				{
					nSpaceLeft   -=  size.cx;
					nCharCounted +=  nFit;
				}
			}
			else
			{
				//计算宽度进行边界对齐
				nFillSpace =  nSpaceLeft - alpDx[nFit -1] ;
				nPreCharCounted = nCharCounted;		  
				nCharCounted +=  nFit;
				break;
			}
		}


		//如超过高度,退出。
		if( nHeight + nLineSpace > lpRect->bottom - lpRect->top)
		{	
			break;
		}

		RECT rc = *lpRect;
		rc.top += nHeight;
		rc.bottom = rc.top + nLineSpace;
		nHeight +=  nLineSpace;

		if(nFillSpace)
		{
			int ndrawed = 0;
			ExtTextOut(hdc,rc.left,rc.top,0,0,szText + nCharsDrawed +ndrawed ,nPreCharCounted + 1,0);
			ndrawed += nPreCharCounted + 1;
			int nAccum = 0;
			int xadd = nMaxExtent - nSpaceLeft + alpDx [0];
			int i = 1;
			while (ndrawed < nCharCounted && nAccum < nFillSpace && i< nFit ) 
			{

				TCHAR * sz= (TCHAR*)szText + nCharsDrawed + ndrawed;
				if (*sz> 0xff)
				{ 
					++nAccum; 
					++ xadd ;
				}
				ExtTextOut(hdc,rc.left + xadd ,rc.top,0,0,sz,1,0);

				++ndrawed;
				xadd += alpDx[i] - alpDx[i-1];
				++i;
			}

			if(ndrawed < nCharCounted)
			{
				TCHAR *sz = (TCHAR*)szText + nCharsDrawed + ndrawed;
				ExtTextOut(hdc,rc.left + xadd ,rc.top,0,0,sz, nCharCounted - ndrawed , 0);
			}
		}
		else
		{
			ExtTextOut(hdc,rc.left,rc.top,0,0,szText + nCharsDrawed,nCharCounted,0);
		}
		nCharsDrawed += nCharCounted ; 
	}

	nLen = nCharsDrawed ;

	SetBkMode(hdc, oldMode);
	return true;
}




BOOL ReadFile(HWND hWnd)
{
	BOOL bResult;
	DWORD nBytesRead;
	DWORD dwSize;
	HANDLE hFile;

	TCHAR    szFile[MAX_PATH];

	OPENFILENAMEEX  ofnex = {0};
	ofnex.lStructSize     = sizeof(ofnex);
	ofnex.hwndOwner       = hWnd;
	ofnex.lpstrFile       = szFile;
	ofnex.nMaxFile        = sizeof(szFile)/sizeof(szFile[0]);
	ofnex.lpstrFilter     = TEXT("All Files (*.*)\0*.*\0");
	ofnex.lpstrTitle      = TEXT("Thumbnail View");

	// Show thumbnails of files that are not DRM protected
	ofnex.ExFlags         = OFN_EXFLAG_THUMBNAILVIEW | OFN_EXFLAG_HIDEDRMPROTECTED;
	ofnex.lpstrInitialDir = NULL;

	GetOpenFileNameEx(&ofnex);
	hFile = CreateFile(ofnex.lpstrFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFile);
		return FALSE;
	}

	dwSize = GetFileSize (hFile, NULL) ; 

	// Result on failure. 
	if (dwSize == 0xFFFFFFFF)
	{
		//dwError = GetLastError() ; 
		CloseHandle(hFile);
		return FALSE;//break;
	}



	//TCHAR * inBuffer = new TCHAR(dwSize);
	//char * inBuffer = new char(dwSize);
	char * inBuffer = new char[dwSize];
	bResult = ReadFile(hFile, inBuffer, dwSize, &nBytesRead, NULL) ; 

	//g_lpStrDraw = new TCHAR(dwSize);
	//bResult = ReadFile(hFile, &g_lpStrDraw, dwSize, &nBytesRead, NULL) ; 

	if (bResult &&  (nBytesRead == 0) ) 
	{ 
		// you are at the end of the file. 
		//read file success
		//g_lpStrDraw = inBuffer;
	}

	//int nSize = MultiByteToWideChar(CP_ACP|CP_OEMCP|CP_UTF7|CP_UTF8, MB_COMPOSITE, inBuffer, dwSize, NULL, 0);
	int nSize = MultiByteToWideChar(CP_ACP, 0, inBuffer, dwSize, NULL, 0);
	//TCHAR* strDraw = new TCHAR[nSize];
	g_pStrDraw =  new TCHAR[nSize];
	nSize = MultiByteToWideChar(CP_ACP, 0, inBuffer, dwSize, g_pStrDraw, nSize);
	delete[] inBuffer;
	CloseHandle(hFile);
	//InvalidateRect(hWnd)
	return TRUE;

}
//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND	- 处理应用程序菜单
//  WM_PAINT	- 绘制主窗口
//  WM_DESTROY	- 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	static SHACTIVATEINFO s_sai;


	BITMAP bm;
	HBITMAP hOldObj;
	RECT rc;
	HDC hMemDc;


	switch (message) 
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// 分析菜单选择:
		switch (wmId)
		{
		case IDM_HELP_ABOUT:
			{
				HDC hdc = GetDC(hWnd);
				RECT rc;
				GetClientRect(hWnd,&rc);
				rc.left  += 20;
				rc.right -= 20;
				rc.top   += 20;
				rc.bottom -= 20;
				int len = -1;
				int nLineSpace = 0;
				int nHeight =0 ;
				//DrawParagraph(hdc,&rc,g_lpStrDraw, len ,0, nLineSpace, nHeight);
				DrawParagraph(hdc,&rc,g_pStrDraw, len ,0, nLineSpace, nHeight);
				ReleaseDC(hWnd,hdc);

			}
			//	DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
			break;
		case IDM_OPEN_FILE:
			if(ReadFile(hWnd))
			{
				//RECT rc;
				//GetClientRect(hWnd, &rc);
				//InvalidateRect(hWnd, &rc, FALSE);
				//SendMessage(hWnd, WM_COMMAND, MAKELPARAM(IDM_HELP_ABOUT,0), 0);
			}
			break;


		case IDM_OK:
			SendMessage (hWnd, WM_CLOSE, 0, 0);				
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_CREATE:
		SHMENUBARINFO mbi;

		memset(&mbi, 0, sizeof(SHMENUBARINFO));
		mbi.cbSize     = sizeof(SHMENUBARINFO);
		mbi.hwndParent = hWnd;
		mbi.nToolBarId = IDR_MENU;
		mbi.hInstRes   = g_hInst;

		if (!SHCreateMenuBar(&mbi)) 
		{
			g_hWndMenuBar = NULL;
		}
		else
		{
			g_hWndMenuBar = mbi.hwndMB;
		}

		// 初始化外壳程序激活信息结构
		memset(&s_sai, 0, sizeof (s_sai));
		s_sai.cbSize = sizeof (s_sai);
		break;
	case WM_PAINT:
		{

			hdc = BeginPaint(hWnd, &ps);

			GetClientRect(hWnd, &rc);
			GetObject(g_hBitmap,sizeof(bm),&bm);
			hMemDc = CreateCompatibleDC(hdc);
			hOldObj = (HBITMAP)SelectObject(hMemDc, g_hBitmap);

			StretchBlt(hdc, 0, 0, rc.right-rc.left, rc.bottom-rc.top, hMemDc, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);

			SelectObject(hMemDc, hOldObj);
			DeleteDC(hMemDc);

			RECT rc;
			GetClientRect(hWnd,&rc);
			rc.left  += 20;
			rc.right -= 20;
			rc.top   += 20;
			rc.bottom -= 20;
			int len = -1;
			int nLineSpace = 0;
			int nHeight =0 ;
			//DrawParagraph(hdc,&rc,g_lpStrDraw, len ,0, nLineSpace, nHeight);
			DrawParagraph(hdc,&rc,g_pStrDraw, len ,0, nLineSpace, nHeight);

			EndPaint(hWnd, &ps);
		}

		break;
	case WM_DESTROY:
		CommandBar_Destroy(g_hWndMenuBar);
		PostQuitMessage(0);
		break;

	case WM_ACTIVATE:
		// 向外壳程序通知我们的激活消息
		SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
		break;
	case WM_SETTINGCHANGE:
		SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		{
			// 创建一个“完成”按钮并调整其大小。
			SHINITDLGINFO shidi;
			shidi.dwMask = SHIDIM_FLAGS;
			shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
			shidi.hDlg = hDlg;
			SHInitDialog(&shidi);
		}
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;

	case WM_CLOSE:
		EndDialog(hDlg, message);
		return TRUE;

	}
	return (INT_PTR)FALSE;
}

⌨️ 快捷键说明

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