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

📄 dhtmleditdemodlg.cpp

📁 用visual c++写的一个超文本编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include "stdafx.h"
#include "DHtmlEditDemo.h"
#include "DHtmlEditDemoDlg.h"
#include "TableProperties.h"
#include "HrProperties.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// forward declaration
int CALLBACK EnumFontNameProc(LOGFONT *plf, TEXTMETRIC* ptm, int nFontType, LPARAM lParam);

CDHtmlEditDemoDlg::CDHtmlEditDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDHtmlEditDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDHtmlEditDemoDlg)
	//}}AFX_DATA_INIT

	mk_OrgMain.left  = 0;
	mk_OrgMain.right = 0;

	pi_Editor = 0;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	// Set checkbox status to FALSE 
	// (for ms_ComboStatus not required as the CString constructor sets them to "")
	memset(mb_CheckStatus, 0, _CheckBoxCount *4);
}

void CDHtmlEditDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDHtmlEditDemoDlg)
	DDX_Control(pDX, IDC_STATIC_URL, mCtrl_StaticUrl);
	DDX_Control(pDX, IDC_BUTTON_GOFORWARD, mCtrl_GoForward);
	DDX_Control(pDX, IDC_BUTTON_GOBACK, mCtrl_GoBack);
	DDX_Control(pDX, IDC_BUTTON_LOAD, mCtrl_Load);
	DDX_Control(pDX, IDC_EDIT_URL, mCtrl_EditUrl);
	DDX_Control(pDX, IDC_BUTTON_SAVE, mCtrl_Save);
	DDX_Control(pDX, IDC_BUTTON_HELP, mCtrl_Help);
	DDX_Control(pDX, IDC_BUTTON_BACKCOLOR, mCtrl_BgText);
	DDX_Control(pDX, IDC_BUTTON_BGCOLOR, mCtrl_BgBody);
	DDX_Control(pDX, IDC_BUTTON_BGCOLOR_CELL, mCtrl_BgCell);
	DDX_Control(pDX, IDC_BUTTON_CLEAR, mCtrl_Clear);
	DDX_Control(pDX, IDC_BUTTON_COL_LEFT, mCtrl_ColLeft);
	DDX_Control(pDX, IDC_BUTTON_COL_RIGHT, mCtrl_ColRight);
	DDX_Control(pDX, IDC_BUTTON_COMBINE, mCtrl_Combine);
	DDX_Control(pDX, IDC_BUTTON_DELETE_COL, mCtrl_DeleteCol);
	DDX_Control(pDX, IDC_BUTTON_DELETE_ROW, mCtrl_DeleteRow);
	DDX_Control(pDX, IDC_BUTTON_FORECOLOR, mCtrl_ForeColor);
	DDX_Control(pDX, IDC_BUTTON_HR, mCtrl_Hr);
	DDX_Control(pDX, IDC_BUTTON_HRPROP, mCtrl_HrProp);
	DDX_Control(pDX, IDC_BUTTON_IMAGE, mCtrl_Image);
	DDX_Control(pDX, IDC_BUTTON_LINK, mCtrl_Link);
	DDX_Control(pDX, IDC_BUTTON_NOBR, mCtrl_NoBr);
	DDX_Control(pDX, IDC_BUTTON_REMOVEFORMAT, mCtrl_RemoveFormat);
	DDX_Control(pDX, IDC_BUTTON_ROW_ABOVE, mCtrl_RowAbove);
	DDX_Control(pDX, IDC_BUTTON_ROW_BELOW, mCtrl_RowBelow);
	DDX_Control(pDX, IDC_BUTTON_SPLIT, mCtrl_Split);
	DDX_Control(pDX, IDC_BUTTON_TABLE, mCtrl_Table);
	DDX_Control(pDX, IDC_BUTTON_TABLEPROP, mCtrl_TableProp);
	DDX_Control(pDX, IDC_STATIC_EDITOR, mCtrl_StaticEditor);
	DDX_Control(pDX, IDC_CHECK_SOURCE, mCtrl_CheckSource);
	DDX_Control(pDX, IDC_CHECK_DESIGN, mCtrl_CheckDesign);
	DDX_Control(pDX, IDC_CHECK_BOLD, mCtrl_CheckBold);
	DDX_Control(pDX, IDC_CHECK_ITALIC, mCtrl_CheckItalic);
	DDX_Control(pDX, IDC_CHECK_UNDERLINE, mCtrl_CheckUnderline);
	DDX_Control(pDX, IDC_CHECK_LEFTALIGN, mCtrl_CheckLeftAlign);
	DDX_Control(pDX, IDC_CHECK_CENTERALIGN, mCtrl_CheckCenterAlign);
	DDX_Control(pDX, IDC_CHECK_RIGHTALIGN, mCtrl_CheckRightAlign);
	DDX_Control(pDX, IDC_CHECK_ORDERLIST, mCtrl_CheckOrderList);
	DDX_Control(pDX, IDC_CHECK_UNORDERLIST, mCtrl_CheckUnOrderList);
	DDX_Control(pDX, IDC_COMBO_FONTNAME, mCtrl_ComboFontName);
	DDX_Control(pDX, IDC_COMBO_FONTSIZE, mCtrl_ComboFontSize);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDHtmlEditDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDHtmlEditDemoDlg)
	ON_WM_PAINT()
	ON_WM_DESTROY()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO_FONTNAME, OnComboSelchangeComboFontname)
	ON_CBN_SELCHANGE(IDC_COMBO_FONTSIZE, OnSelchangeComboFontsize)
	ON_WM_TIMER()
	ON_CBN_EDITCHANGE(IDC_COMBO_FONTSIZE, OnEditchangeComboFontsize)
	ON_CBN_CLOSEUP(IDC_COMBO_FONTSIZE, OnCloseupComboFontsize)
	ON_WM_GETMINMAXINFO()
	ON_MESSAGE(WM_EXITSIZEMOVE, OnExitSizeMove)
	ON_WM_WINDOWPOSCHANGED()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CDHtmlEditDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	mCtrl_CheckDesign.SetCheck(TRUE);

	// ############## RESIZING STUFF ###################
	
	// retrieve the origional size of Internet Explorer Control and main window
	// before any resizing took place (BEFORE assigning pi_Editor !!)
	mCtrl_StaticEditor.GetWindowRect(mk_OrgMsie);
	GetWindowRect(mk_OrgMain);

	// ############# END RESIZING STUFF ################


	pi_Editor = new CHtmlEditor();
	if (!pi_Editor->CreateEditor(&mCtrl_StaticEditor, this, FALSE, TRUE))
	{
		MessageBox(_T("Error creating CHtmlEditor"), _T("Error"), MB_ICONSTOP);
		CDialog::EndDialog(0);
		return TRUE;
	}

	// Fill the Font combo box
	EnumFontFamilies(GetDC()->m_hDC, 0, (FONTENUMPROC)EnumFontNameProc, (LPARAM)&mCtrl_ComboFontName);

	CString s_Title;
	GetWindowText(s_Title);
	#ifdef _UNICODE
		SetWindowText(s_Title + _T(" - Unicode"));
	#else
		SetWindowText(s_Title + _T(" - MultiByte"));
	#endif
	return TRUE;
}

// ##################### RESIZING STUFF ############################

// Handler for Windows Message WM_WINDOWPOSCHANGED
// Called multiple times WHILE the user resizes or moves or maximizes the main window
// Do nothing here while resizing to avoid flickering
void CDHtmlEditDemoDlg::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos) 
{
	// If main window was maximized or normalized --> resize MSIE
	static BOOL b_Zoomed = -1;
	if (b_Zoomed != IsZoomed())
	{
		b_Zoomed  = IsZoomed();

		if (!IsIconic()) OnExitSizeMove(0,0);
	}

	CDialog::OnWindowPosChanged(lpwndpos);
}

// Handler for Windows Message WM_EXITSIZEMOVE
// Called after the user has FINISHED to move or resize the main window
LRESULT CDHtmlEditDemoDlg::OnExitSizeMove(WPARAM wParam, LPARAM lParam)
{
	if (!pi_Editor)
		return 0;

	CRect k_NewMain;
	GetWindowRect(k_NewMain);

	// IMPORTANT: Resize MSIE only if size has changed !!
	// Otherwise (afer a Move) MSIE would stay blank !
	static CSize k_Size(0,0);
	if (k_Size == k_NewMain.Size()) return 0;
	k_Size = k_NewMain.Size();


	CSize k_Diff = k_NewMain.Size() - (mk_OrgMain.Size() - mk_OrgMsie.Size());

	pi_Editor->MoveWindow(0, 0, k_Diff.cx, k_Diff.cy, SWP_NOMOVE);
	Invalidate(FALSE);
	return 0;
}

// Handler for Windows Message WM_GETMINMAXINFO
// Main Window is about to be resized --> prevent illegal sizes
// Don't allow main window to become smaller than the origional size defined in Resources
// ptMinTrackSize requires WINDOW size
void CDHtmlEditDemoDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
	// ATTENTION:
	// This function is called once BEFORE OnInitDialog() is called !!!
	// At the first call of this function mk_OrgMain is not yet valid
	if (mk_OrgMain.Width() > 0)
	{
		lpMMI->ptMinTrackSize.x = mk_OrgMain.Width();
		lpMMI->ptMinTrackSize.y = mk_OrgMain.Height() *2 /3; // allow decreasing the height a little
	}	
	CDialog::OnGetMinMaxInfo(lpMMI);
}

// ##################### END RESIZING STUFF #########################

void CDHtmlEditDemoDlg::OnDestroy(void)
{
	if (pi_Editor) pi_Editor->DestroyWindow();
	// delete pi_Editor is not required as DestroyWindow() does the job !
}

void CDHtmlEditDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this);
		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width()  - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else CDialog::OnPaint();
}

HCURSOR CDHtmlEditDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CDHtmlEditDemoDlg::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent == ID_TIMER_COMBO_FONTSIZE)
	{
		KillTimer(nIDEvent);
		OnUserChangedFontsize();
	}	
	
	CDialog::OnTimer(nIDEvent);
}

// ################ FONTS ##################

// List all installed fonts into the combobox
int CALLBACK EnumFontNameProc(LOGFONT *plf, TEXTMETRIC* ptm, int nFontType, LPARAM lParam)
{
	CComboBox* i_Combo = (CComboBox*) lParam;
	i_Combo->AddString(plf->lfFaceName);
	return TRUE;
}

void CDHtmlEditDemoDlg::OnComboSelchangeComboFontname()
{
	CString s_Font;
	mCtrl_ComboFontName.GetWindowText(s_Font);

	if (!pi_Editor->ExecSetCommand(IDM_FONTNAME, s_Font.AllocSysString()))
		MessageBox(_T("The selection or cursor position is invalid!"), _T("Error"), MB_ICONSTOP);
}

void CDHtmlEditDemoDlg::OnSelchangeComboFontsize()
{
	SetTimer(ID_TIMER_COMBO_FONTSIZE, 200, 0);
}

void CDHtmlEditDemoDlg::OnEditchangeComboFontsize() 
{
	SetTimer(ID_TIMER_COMBO_FONTSIZE, 200, 0);
}

void CDHtmlEditDemoDlg::OnCloseupComboFontsize() 
{
	SetTimer(ID_TIMER_COMBO_FONTSIZE, 200, 0);
}

// called from ID_TIMER_COMBO_FONTSIZE
void CDHtmlEditDemoDlg::OnUserChangedFontsize()
{
	CString s_Size;
	mCtrl_ComboFontSize.GetWindowText(s_Size);

	UINT u32_Size = _ttol(s_Size);
	// Do nothing after entering the first digit of a 2 digit number (1..3)
	if (u32_Size < 4 || u32_Size > 99) 
		return;

	// Do not use IDM_FONTSIZE here as it is very gross
	if (!pi_Editor->ExecSetCommand(IDM_FONTSIZE_STYLE, (int)u32_Size))
		MessageBox(_T("The selection or cursor position is invalid!"), _T("Error"), MB_ICONSTOP);
}

// ##################  BUTTON & CHECKBOX CLICKS ####################

// Receives all clicks on buttons and checkboxes
LRESULT CDHtmlEditDemoDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	/*
	CString s_Debug;
	s_Debug.Format(_T("Msg 0x%04X, wParam 0x%08X, lParam 0x%08X"), message, wParam, lParam);
	OutputDebugString(s_Debug);
	*/

	if (message == WM_COMMAND)
	{
		// IDOK is sent when the user hits the Enter key 
		// (except if the cursor is in the Internet Explorer control)
		// Per default this would close the dialog, the application would shut down
		if (wParam == IDOK) 
		{
			// Did the user hit the Enter key while the cursor is in the URL Edit box ?
			if (::GetFocus() == mCtrl_EditUrl.m_hWnd)
			{
				OnWmCommand(IDC_BUTTON_LOAD); // Surf to the URL
			}
			return 0; // prevent this message from getting to CDialog::WindowProc()
		}

		OnWmCommand(wParam);
	}

	if (message == WM_IDLEUPDATECMDUI) OnUpdateUI (wParam);

	return CDialog::WindowProc(message, wParam, lParam);
}

// received WM_COMMAND
void CDHtmlEditDemoDlg::OnWmCommand(WPARAM wParam)
{
	// this table is just for demonstration
	CString s_NewTable = _T("<table border=1 bordercolor=#996633><tr><td colspan=2><div>1AB</div></td><td><div>1C</div></td><td><div>1D</div></td></tr><tr><td><div>2A</div></td><td><div>2B</div></td><td colspan=2><div>2CD</div></td></tr><tr><td><div>3A</div></td><td colspan=2><div>3BC</div></td><td><div>3D</div></td></tr></table>");

	cHtmlDocument *pi_Doc  = pi_Editor->GetDocument();
	cHtmlTableCell  i_Cell  = 0;
	cHtmlTable      i_Table = 0;
	BOOL            b_SelOK = TRUE;
	CString         s_Text;

	// ######## GET CELL #########

	switch (LOWORD(wParam))
	{
	case IDC_BUTTON_BGCOLOR_CELL:
	case IDC_BUTTON_COMBINE:
	case IDC_BUTTON_SPLIT:
	case IDC_BUTTON_DELETE_ROW:
	case IDC_BUTTON_DELETE_COL:
		i_Cell  = pi_Doc->GetSelectedCell(); // outputs error
		i_Table = i_Cell.GetParentTable();
		if (!i_Cell.Valid() || !i_Table.Valid())
			return;
	}

	switch (LOWORD(wParam))
	{
	
	// ##################  Control  ##################
	
	case IDC_BUTTON_CLEAR:
		pi_Editor->Clear();
		break;
	case IDC_BUTTON_SAVE:
		pi_Editor->GetDocument()->CleanUpTags(); // remove empty tags

⌨️ 快捷键说明

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