?? text5.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 + -