// ZmmScrollWnd.cpp : 实现文件
//
#include "stdafx.h"
#include "ZmmEditorCtrl.h"
#include "ZmmEditorDefs.h"
#include "ZmmMemoryStream.h"
#define S_MAKE_DWORD(p) (((p)[3]<<24)|((p)[2]<<16)|((p)[1]<<8)|((p)[0]))
#define S_MAKE_WORD(p) (((p)[1]<<8)|((p)[0]))
// CZmmEditorCtrl
IMPLEMENT_DYNAMIC(CZmmEditorCtrl, CWnd)
void CZmmEditorCtrl::DrawBitmap(HDC hdc, const void* pData,
short x, short y, short cx, short cy, short bx, short by)
{
BITMAPFILEHEADER bmfh;
const BYTE* pBits;
BITMAPINFO* pbmi;
DWORD* colTab;
int colNum;
int dibWidth, dibHigh, dibWidth4;
const BYTE* pB;
pB = (const BYTE*)pData;
bmfh.bfType = S_MAKE_WORD(pB); pB+=2;
bmfh.bfSize = S_MAKE_DWORD(pB); pB+=4;
pB+=2; pB+=2;
bmfh.bfOffBits = S_MAKE_DWORD(pB); pB+=4;
pBits = ((const BYTE*)pData) + bmfh.bfOffBits;
pbmi = new BITMAPINFO;
memcpy(pbmi, pB, pBits-pB);
colTab = (DWORD*)pbmi->bmiColors;
colNum = ((pBits-pB) - sizeof(BITMAPINFOHEADER)) / sizeof(DWORD);
dibWidth = pbmi->bmiHeader.biWidth;
dibHigh = pbmi->bmiHeader.biHeight;
dibWidth4 = (dibWidth+3)/4*4;
if(cx<1 || cx>dibWidth) cx=dibWidth;
if(cy<1 || cy>dibHigh ) cy=dibHigh;
SetDIBitsToDevice(hdc, x, y, cx, cy, bx, dibHigh-by-cy,
0, dibHigh, pBits, pbmi, DIB_RGB_COLORS);
delete pbmi;
}
CZmmEditorCtrl::CZmmEditorCtrl()
{
m_bMouseDown = FALSE;
m_bCtrlDown =FALSE;
m_nBeginIndex = -1;
m_nEndIndex = -1;
m_nDownIndex = 0;
m_nCaretIndex = 0;
m_nCaretPos = 0;
m_dirDown = LEFT;
m_nScrollPos = 0;
m_nScrollIndex = 0;
m_pSelectedObject = NULL;
m_bNeedSetFont = TRUE;
m_CurrentFont.bBold = FALSE;
m_CurrentFont.bItalic = FALSE;
m_CurrentFont.bUnderline = FALSE;
m_CurrentFont.color = 0;
m_CurrentFont.dwSize = 12;
strcpy(m_CurrentFont.szFontName, "宋体");
m_nCaretCY = m_CurrentFont.dwSize;
}
CZmmEditorCtrl::~CZmmEditorCtrl()
{
}
BEGIN_MESSAGE_MAP(CZmmEditorCtrl, CWnd)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_VSCROLL()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_SIZE()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_CHAR()
ON_WM_SETCURSOR()
ON_WM_KEYDOWN()
ON_WM_KEYUP()
ON_WM_TIMER()
END_MESSAGE_MAP()
void CZmmEditorCtrl::Clear()
{
m_CharArray.Delete(0, m_CharArray.GetCount());
ComposeLines();
m_nScrollIndex = 0;
SetCaretPos(0);
Invalidate(FALSE);
}
void CZmmEditorCtrl::SaveToFile(CString fileName)
{
CZmmMemoryStream mmStream;
if(m_CharArray.GetCount()>0)
{
CHARINFO *pCharInfo;
char buffer[1024];
strcpy(buffer, "\\F");
m_CharArray.GetItem(0, &pCharInfo);
mmStream.Write(buffer, strlen(buffer));
TEXTFONT font = pCharInfo->font;
mmStream.Write((char*)&font, sizeof(font));
for(int i = 0; i<m_CharArray.GetCount(); ++i)
{
m_CharArray.GetItem(i, &pCharInfo);
if(!CompareFont(font, pCharInfo->font))
{
strcpy(buffer, "\\F");
mmStream.Write(buffer, strlen(buffer));
font = pCharInfo->font;
mmStream.Write((char*)&font, sizeof(font));
}
if(pCharInfo->c[0]==-1 && pCharInfo->c[1]>0)
{
if(pCharInfo->c[1]==1)//图片
strcpy(buffer, "\\P");
else if(pCharInfo->c[1]==2)//链接
strcpy(buffer, "\\L");
else if(pCharInfo->c[1]==3)//按钮
strcpy(buffer, "\\B");
mmStream.Write(buffer, strlen(buffer));
mmStream.Write((char*)&pCharInfo->nDataSize, 4);
mmStream.Write(pCharInfo->lpData, pCharInfo->nDataSize);
}
else//字符
{
if(pCharInfo->c[0]=='\\')
{
mmStream.Write(pCharInfo->c, (int)strlen(pCharInfo->c));
}
mmStream.Write(pCharInfo->c, (int)strlen(pCharInfo->c));
}
}
}
mmStream.SaveToFile(fileName);
}
void CZmmEditorCtrl::LoadFile(CString fileName)
{
static int k = 0;
Clear();
CZmmMemoryStream mmStream;
mmStream.LoadFromFile(fileName);
BOOL bSpcialChar = FALSE;
char *pBuffer = mmStream.GetBuffer();
CHARINFO charinfo;
TEXTFONT font = m_CurrentFont;
int i=0;
while(i<mmStream.GetSize())
{
charinfo.bSelected = FALSE;
charinfo.font = font;
if(!bSpcialChar && pBuffer[i]=='\\')
{
bSpcialChar = TRUE;
++i;
continue;
}
if(bSpcialChar)
{
switch(pBuffer[i])
{
case '\\':
charinfo.c[0] = pBuffer[i];
charinfo.c[1] = NULL;
SetCharSize(&charinfo);
m_CharArray.Add(charinfo);
break;
case 'P': //图片
{
++i;
DWORD nSize;
memcpy(&nSize, pBuffer+i,sizeof(DWORD));
i += sizeof(DWORD);
InsertBitmap(m_CharArray.GetCount(), pBuffer+i, nSize, font);
i += nSize;
break;
}
case 'B': //按钮
{
++i;
DWORD nSize;
memcpy(&nSize, pBuffer+i,sizeof(DWORD));
i += sizeof(DWORD);
BMPBTNDATA bmpData;
memcpy(&bmpData, pBuffer+i, sizeof(BMPBTNDATA));
bmpData.pBmp = pBuffer + i + sizeof(BMPBTNDATA);
InsertBmpBtn(m_CharArray.GetCount(), bmpData, font);
i += nSize;
break;
}
case 'L': //链接
{
++i;
DWORD nSize;
memcpy(&nSize, pBuffer+i,sizeof(DWORD));
i += sizeof(DWORD);
URLLABELDATA urlData;
memcpy(&urlData, pBuffer+i, sizeof(URLLABELDATA));
InsertURLLabel(m_CharArray.GetCount(), urlData, font);
i += nSize;
break;
}
case 'F':
++i;
memcpy(&font, pBuffer+i,sizeof(TEXTFONT));
i += sizeof(TEXTFONT);
break;
}
bSpcialChar = FALSE;
}
else
{
if(pBuffer[i]<0)
{
charinfo.c[k] = pBuffer[i];
if(k==1)
{
charinfo.c[2] = NULL;
SetCharSize(&charinfo);
m_CharArray.Add(charinfo);
k = 0;
}
else
{
++k;
}
}
else
{
charinfo.c[0] = pBuffer[i];
charinfo.c[1] = NULL;
SetCharSize(&charinfo);
m_CharArray.Add(charinfo);
}
++i;
}
}
ComposeLines();
Invalidate(FALSE);
SetCaretPos(0);
m_nScrollIndex = 0;
m_nBeginIndex = 0;
m_nEndIndex = 0;
}
void CZmmEditorCtrl::SetCharSize(LPCHARINFO pCharInfo)
{
HDC hdc = ::GetDC(m_hWnd);
HFONT hFont = CreateFont(pCharInfo->font.dwSize,0,0,0,pCharInfo->font.bBold?FW_BOLD:FW_NORMAL,
pCharInfo->font.bItalic, pCharInfo->font.bUnderline,FALSE,
GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,FIXED_PITCH|FF_MODERN, pCharInfo->font.szFontName);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
SIZE size;
GetTextExtentPoint32(hdc, pCharInfo->c, (int)strlen(pCharInfo->c), &size);
pCharInfo->cx = size.cx;
pCharInfo->cy = size.cy;
::SelectObject(hdc, hOldFont);
DeleteObject(hFont);
::ReleaseDC(m_hWnd, hdc);
}
BOOL CZmmEditorCtrl::IsSymbol(char* c)
{
char str[]=",.;!。,;!";
return (strstr(str, c)!=NULL);
}
BOOL CZmmEditorCtrl::CompareFont(const TEXTFONT &a, const TEXTFONT &b)
{
return (a.bBold == b.bBold &&
a.bItalic == b.bItalic &&
a.bUnderline == b.bUnderline&&
a.color == b.color&&
a.dwSize == b.dwSize&&
strcmp(a.szFontName, b.szFontName)==0);
}
void CZmmEditorCtrl::ComposeLines() //将字符组成行,并算出x, cx, cy
{
CRect rect;
GetClientRect(&rect);
rect.DeflateRect(2, 2, 0, 0);
int x = rect.left, y = rect.top, nRow = 0;
int nScrollMax = 0;
CHARINFO *pCharInfo;
for(int i = 0; i<m_CharArray.GetCount(); ++i)
{
m_CharArray.GetItem(i, &pCharInfo);
if(x+pCharInfo->cx > rect.right &&i>0) //如果是第二个字符开始超宽了
{
x = rect.left;
++nRow;
CHARINFO *pLastChar;
m_CharArray.GetItem(i-1, &pLastChar);
int nLastRow = pLastChar->nRow;
int nReturnPos = i; //换行位置
for(int j = i-1; j>=0; --j)
{
CHARINFO *p;
m_CharArray.GetItem(j, &p);
if(p->nRow < nLastRow) //上一行了
break;
if(p->c[0] == ' ' || p->c[0]<0 || IsSymbol(p->c))//在本行找着空格或是中文了或是标点符号
{
nReturnPos = j+1;
break;
}
}
for(int j = nReturnPos; j<i; ++j)
{
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ZmmEditor.rar (30个子文件)
ZmmEditor
ZmmEditorCtrl.cpp 40KB
ZmmEditorCtrl.h 3KB
main.zmm 160KB
ZmmMemoryStream.h 698B
ZmmEditor.exe 76KB
MyDlg.h 341B
MyDlg.cpp 440B
stdafx.h 1KB
ZmmMemoryStream.cpp 2KB
CharArray.h 451B
InputURLDlg.h 477B
ZmmEditor.aps 58KB
ZmmEditor.vcproj 5KB
ZmmEditor.sln 907B
CharArray.cpp 2KB
InputURLDlg.cpp 775B
btn.bmp 13KB
stdafx.cpp 138B
ZmmEditorDlg.cpp 8KB
ZmmEditorDefs.h 706B
Resource.h 1KB
ZmmEditor.rc 5KB
res
ZmmEditor.ico 21KB
ZmmEditor.manifest 698B
ZmmEditor.rc2 365B
toolbar1.bmp 1KB
ZmmEditorDlg.h 1KB
ZmmEditor.cpp 1KB
ZmmEditor.suo 13KB
ZmmEditor.h 477B
共 30 条
- 1
zhouming2000
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页