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

📄 gridctrldemodlg.cpp

📁 MFC 打印效果 超好的学习例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CATCH (CMemoryException, e)
	{
		e->ReportError();
		e->Delete();
		return;
	}
    END_CATCH

	m_nRows = m_Grid.GetRowCount();
	m_nFixRows = m_Grid.GetFixedRowCount();
	UpdateData(FALSE);
}

void CGridCtrlDemoDlg::OnUpdateEditRows() 
{	
	if (!::IsWindow(m_Grid.m_hWnd)) return;
	UpdateData();

    int nOldNumRows = m_Grid.GetRowCount();

	TRY {
        m_Grid.SetRowCount(m_nRows); 
    }
	CATCH (CMemoryException, e)
	{
		e->ReportError();
		e->Delete();
		return;
	}
    END_CATCH

	m_nRows = m_Grid.GetRowCount();
	m_nFixRows = m_Grid.GetFixedRowCount();
	UpdateData(FALSE);

    CString str;
    for (int i = nOldNumRows; i < m_nRows; i++)
    {
        str.Format(_T("Row %d"), i);
        m_Grid.SetItemText(i,0,str);
    }
}

void CGridCtrlDemoDlg::OnGridLines() 
{
	UpdateData();

	if (!m_bHorzLines && !m_bVertLines)
		m_Grid.SetGridLines(GVL_NONE);
	else if (m_bHorzLines && !m_bVertLines)
		m_Grid.SetGridLines(GVL_HORZ);
	else if (!m_bHorzLines && m_bVertLines)
		m_Grid.SetGridLines(GVL_VERT);
	else 
		m_Grid.SetGridLines(GVL_BOTH);
}

void CGridCtrlDemoDlg::OnListmode() 
{
	UpdateData();
	m_Grid.SetListMode(m_bListMode);

	CWnd* pButton = GetDlgItem(IDC_HEADERSORT);
	if (pButton) 
	{
		pButton->ModifyStyle(m_bListMode?WS_DISABLED:0, m_bListMode? 0:WS_DISABLED);
		pButton->Invalidate();
	}
	pButton = GetDlgItem(IDC_SINGLESELMODE);
	if (pButton) 
	{
		pButton->ModifyStyle(m_bListMode?WS_DISABLED:0, m_bListMode? 0:WS_DISABLED);
		pButton->Invalidate();
	}
}

void CGridCtrlDemoDlg::OnHeaderSort() 
{
	UpdateData();
	m_Grid.SetHeaderSort(m_bHeaderSort);
}

void CGridCtrlDemoDlg::OnSingleselmode() 
{
	UpdateData();
	m_Grid.SetSingleRowSelection(m_bSingleSelMode);
}

void CGridCtrlDemoDlg::OnEditable() 
{
	UpdateData();
	m_Grid.SetEditable(m_bEditable);
}

void CGridCtrlDemoDlg::OnAllowSelection() 
{
	UpdateData();
	m_Grid.EnableSelection(m_bSelectable);
}

void CGridCtrlDemoDlg::OnRowResize() 
{
	UpdateData();
	m_Grid.SetRowResize(m_bAllowRowResize);
}

void CGridCtrlDemoDlg::OnColResize() 
{
	UpdateData();
	m_Grid.SetColumnResize(m_bAllowColumnResize);
}

void CGridCtrlDemoDlg::OnPrintButton() 
{
#if !defined(WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
	m_Grid.Print();
#endif
}

void CGridCtrlDemoDlg::OnFontButton() 
{
#ifndef _WIN32_WCE
	LOGFONT lf;
	m_Grid.GetFont()->GetLogFont(&lf);

	CFontDialog dlg(&lf);
	if (dlg.DoModal() == IDOK) {
		dlg.GetCurrentFont(&lf);

		CFont Font;
		Font.CreateFontIndirect(&lf);
		m_Grid.SetFont(&Font);
        OnItalics();	
		m_Grid.AutoSize();
		Font.DeleteObject();	
	}
#endif
}

BOOL CALLBACK EnumProc(HWND hwnd, LPARAM lParam)
{
	CWnd* pWnd = CWnd::FromHandle(hwnd);
	CSize* pTranslate = (CSize*) lParam;

	CGridCtrlDemoDlg* pDlg = (CGridCtrlDemoDlg*) pWnd->GetParent();
	if (!pDlg) return FALSE;

	CRect rect;
	pWnd->GetWindowRect(rect);
	if (hwnd == pDlg->m_Grid.GetSafeHwnd())
		TRACE(_T("Wnd rect: %d,%d - %d,%d\n"),rect.left,rect.top, rect.right, rect.bottom);
	pDlg->ScreenToClient(rect);
	if (hwnd == pDlg->m_Grid.GetSafeHwnd())
		TRACE(_T("Scr rect: %d,%d - %d,%d\n"),rect.left,rect.top, rect.right, rect.bottom);


	if (hwnd == pDlg->m_Grid.GetSafeHwnd())
	{
		if (  ((rect.top >= 7 && pTranslate->cy > 0) || rect.Height() > 20) &&
			  ((rect.left >= 7 && pTranslate->cx > 0) || rect.Width() > 20)   )
			pDlg->m_Grid.MoveWindow(rect.left, rect.top, 
									rect.Width()+pTranslate->cx, 
									rect.Height()+pTranslate->cy, FALSE);
		else
			pWnd->MoveWindow(rect.left+pTranslate->cx, rect.top+pTranslate->cy, 
							 rect.Width(), rect.Height(), FALSE);
	}
	else 
	{
		pWnd->MoveWindow(rect.left+pTranslate->cx, rect.top+pTranslate->cy, 
						 rect.Width(), rect.Height(), FALSE);
	}
	pDlg->Invalidate();

	return TRUE;
}

void CGridCtrlDemoDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if (cx <= 1 || cy <= 1 ) 
        return;

#ifdef _WIN32_WCE
    m_Grid.MoveWindow(0,0, cx,cy, FALSE);
#else
	CSize Translate(cx - m_OldSize.cx, cy - m_OldSize.cy);

	::EnumChildWindows(GetSafeHwnd(), EnumProc, (LPARAM)&Translate);
	m_OldSize = CSize(cx,cy);
#endif

    //if (::IsWindow(m_Grid.m_hWnd))
    //    m_Grid.ExpandToFit();
}

#ifndef GRIDCONTROL_NO_CLIPBOARD
void CGridCtrlDemoDlg::OnEditCopy() 
{
	m_Grid.OnEditCopy();	
}

void CGridCtrlDemoDlg::OnEditCut() 
{
	m_Grid.OnEditCut();	
}

void CGridCtrlDemoDlg::OnEditPaste() 
{
	m_Grid.OnEditPaste();	
}

void CGridCtrlDemoDlg::OnUpdateEditCopyOrCut(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_Grid.GetSelectedCount() > 0);
}

void CGridCtrlDemoDlg::OnUpdateEditPaste(CCmdUI* pCmdUI) 
{
    // Attach a COleDataObject to the clipboard see if there is any data
    COleDataObject obj;
    pCmdUI->Enable(obj.AttachClipboard() && obj.IsDataAvailable(CF_TEXT)); 
}
#endif

void CGridCtrlDemoDlg::OnEditSelectall() 
{
	m_Grid.OnEditSelectAll();
}

void CGridCtrlDemoDlg::OnAppAbout() 
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CGridCtrlDemoDlg::OnReadOnly() 
{
	UpdateData();
    if (m_bReadOnly)
        m_Grid.SetItemState(1,1, m_Grid.GetItemState(1,1) | GVIS_READONLY);
    else
        m_Grid.SetItemState(1,1, m_Grid.GetItemState(1,1) & ~GVIS_READONLY);
}

void CGridCtrlDemoDlg::OnItalics() 
{
    UpdateData();
    
    // Set fixed cell fonts as italics
	for (int row = 0; row < m_Grid.GetRowCount(); row++)
		for (int col = 0; col < m_Grid.GetColumnCount(); col++)
		{ 
		    if (row < m_Grid.GetFixedRowCount() || col < m_Grid.GetFixedColumnCount())
		    {
		        LOGFONT* pLF = m_Grid.GetItemFont(row ,col);
		        if (!pLF) continue;
		        
		        LOGFONT lf;
		        memcpy(&lf, pLF, sizeof(LOGFONT));
		        lf.lfItalic = (BYTE) m_bItalics;
		        
		        m_Grid.SetItemFont(row, col, &lf);
		    }
		}
	
	m_Grid.Invalidate();
}

void CGridCtrlDemoDlg::OnTitletips() 
{
    UpdateData();
    m_Grid.EnableTitleTips(m_btitleTips);
}

void CGridCtrlDemoDlg::OnInsertRow() 
{
	//m_Grid.SetSelectedRange(-1,-1,-1,-1);
	int nRow = m_Grid.GetFocusCell().row;
    if (nRow >= 0)
    {
	    m_Grid.InsertRow(_T("Newest Row"), nRow);	
	    m_Grid.Invalidate();
    }
	//m_Grid.SetSelectedRange(-1,-1,-1,-1);
}

void CGridCtrlDemoDlg::OnDeleteRow() 
{
	int nRow = m_Grid.GetFocusCell().row;
    if (nRow >= 0)
    {
	    m_Grid.DeleteRow(nRow);	
	    m_Grid.Invalidate();
    }
}

// (Thanks to Koay Kah Hoe for this)
BOOL CGridCtrlDemoDlg::PreTranslateMessage(MSG* pMsg) 
{
    if( pMsg->message == WM_KEYDOWN )
    {
        if(pMsg->wParam == VK_RETURN
            || pMsg->wParam == VK_ESCAPE )
        {
            ::TranslateMessage(pMsg);
            ::DispatchMessage(pMsg);
            return TRUE;                    // DO NOT process further
        }
    }
    return CDialog::PreTranslateMessage(pMsg);
}	

// Added by Kah Hoe
void CGridCtrlDemoDlg::OnPrintpreviewButton() 
{
#if !defined(WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
	m_Grid.PrintPreview();
#endif
}

#if !defined(WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
BOOL CGridCtrlDemoDlg::ContinueModal()
{
	if (m_Grid.m_bPrintPreview)
		// send WM_IDLEUPDATECMDUI message to update toolbar state
		// This is normally called by OnIdle function in SDI or MSI applications.
		// Dialog based applications don't call OnIdle, so send the message from here instead
		AfxGetApp()->m_pMainWnd->SendMessageToDescendants(WM_IDLEUPDATECMDUI,
			(WPARAM)TRUE, 0, TRUE, TRUE);

	return CDialog::ContinueModal();
}
#endif

⌨️ 快捷键说明

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