📄 mfc.txt
字号:
// This example illustrates extending the framework's standard command
// route from the view to objects managed by the view. This example
// is from an object-oriented drawing application, similar to the
// DRAWCLI sample application, which draws and edits "shapes".
BOOL CMyView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo)
{
// Extend the framework's command route from the view to
// the application-specific CMyShape that is currently selected
// in the view. m_pActiveShape is NULL if no shape object
// is currently selected in the view.
if ((m_pActiveShape != NULL)
&& m_pActiveShape->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
// If the object(s) in the extended command route don't handle
// the command, then let the base class OnCmdMsg handle it.
return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
// The command handler for ID_SHAPE_COLOR (menu command to change
// the color of the currently selected shape) was added to
// the message map of CMyShape (note, not CMyView) using ClassWizard.
// The menu item will be automatically enabled or disabled, depending
// on whether a CMyShape is currently selected in the view, that is,
// depending on whether CMyView::m_pActiveView is NULL. It is not
// necessary to implement an ON_UPDATE_COMMAND_UI handler to enable
// or disable the menu item.
BEGIN_MESSAGE_MAP(CMyShape, CCmdTarget)
//{{AFX_MSG_MAP(CMyShape)
ON_COMMAND(ID_SHAPE_COLOR, OnShapeColor)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
如果你只是想调用OnFilePrint( )函数,可以试一试下面的代码,就和调用其它类中的函数一样。
CMDIFrameWnd *pFrame =
(CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
// Get the active MDI child window.
CMDIChildWnd *pChild =
(CMDIChildWnd *) pFrame->GetActiveFrame();
// or CMDIChildWnd *pChild = pFrame->MDIGetActive();
// Get the active view attached to the active MDI child
// window.
CMyView *pView = (CMyView *) pChild->GetActiveView();
pView->OnFilePrint( );
4.打开文档
void CMyReportView::OnFileOpen()
{
char Filter[] = "Crystal Report files(*.rpt)|*.rpt|All files(*.*)|*.*||";
CRect rect;
CFileDialog OpenDlg(TRUE,0,0,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,(LPCTSTR)Filter,NULL);
if(OpenDlg.DoModal()!=IDOK) ///显示文件对话框
return;
CString m_fName=OpenDlg.GetPathName(); ///取得文件名
if(m_CrystalReport)
m_CrystalReport.DestroyWindow();
GetClientRect(rect);
///////////////////创建控件///////////////
if (!m_CrystalReport.Create(AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),WS_CHILD|WS_VISIBLE,rect,this,IDC_CRYSTALREPORT1))
{
AfxMessageBox("控件创建失败!");
return ;
}
m_CrystalReport.SetWindowParentHandle((long)(this->m_hWnd));///设置父窗口
m_CrystalReport.SetWindowBorderStyle(0); ///设置为没有边框
m_CrystalReport.SetWindowLeft(0); ///左空间
m_CrystalReport.SetWindowTop(0); ///顶部空间
m_CrystalReport.SetWindowControls(FALSE); ///不显示工具条
m_CrystalReport.SetReportFileName(m_fName); ///设置报表文件
m_CrystalReport.SetWindowWidth(rect.Width()); ///设置窗口宽度
m_CrystalReport.SetWindowHeight(rect.Height()); ///设置窗口高度
m_CrystalReport.SetFormulas(0, "Company=\"VC知识库\""); ///将报表中的Company变量的值设置为VC知识库
m_CrystalReport.SetDestination(0); ///设置输出对象是屏幕
m_CrystalReport.PrintReport(); ///显示报表
}
void CMyReportView::OnFilePrint()
{
if(m_CrystalReport && m_CrystalReport.GetReportFileName() != "")
{
m_CrystalReport.SetDestination(1); ///设置输出对象是打印机
m_CrystalReport.PrintReport(); ///打印
}
10. Scroll
创建一个基于CScrollview的SDI Project(在第6步中选CScrollview)
若你已创建了,这步可以省略。
然后:
改为如
void CTestView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = 1024; //改这两个
sizeTotal.cy = 768; //
SetScrollSizes(MM_TEXT, sizeTotal);
}
11. 修改主窗口风格
AppWizard生成的应用程序框架的主窗口具有缺省的窗口风格,比如在窗口标题条中自动添加文档名、窗口是叠加型的、可改变窗口大小等。要修改窗口的缺省风格,需要重载CWnd::PreCreateWindow(CREATESTRUCT& cs)函数,并在其中修改CREATESTRUCT型参数cs。
CWnd::PreCreateWindow 函数先于窗口创建函数执行。如果该函数被重载,则窗口创建函数将使用CWnd::PreCreateWindow 函数返回的CREATESTRUCT cs参数所定义的窗口风格来创建窗口;否则使用预定义的窗口风格。
CREATESTRUCT结构定义了创建函数创建窗口所用的初始参数,其定义如下:
typedef struct tagCREATESTRUCT {
LPVOID lpCreateParams; // 创建窗口的基本参数
HANDLE hInstance; // 拥有将创建的窗口的模块实例句柄
HMENU hMenu; // 新窗口的菜单句柄
HWND hwndParent; // 新窗口的父窗口句柄
int cy; // 新窗口的高度
int cx; // 新窗口的宽度
int y; // 新窗口的左上角Y坐标
int x; // 新窗口的左上角X坐标
LONG style; // 新窗口的风格
LPCSTR lpszName; // 新窗口的名称
LPCSTR lpszClass; // 新窗口的窗口类名
DWORD dwExStyle; // 新窗口的扩展参数
} CREATESTRUCT;
CREATESTRUCT结构的style域定义了窗口的风格。比如,缺省的MDI主窗口的风格中就包括FWS_ADDTOTITLE(在标题条中显示当前的工作文档名)、FWS_PREFIXTITLE(把文档名放在程序标题的前面)、WS_THICKFRAME(窗口具有可缩放的边框)等风格。由于多种风格参数由逻辑或(“|”)组合在一起的,因此添加某种风格,就只需用“|”把对应的参数加到CREATESTRUCT结构的style域中;删除已有的风格,则需用“&”连接CREATESTRUCT结构的style域与该风格的逻辑非值。
CREATESTRUCT结构的x、y、cx、cy域分别定义了窗口的初始位置和大小,因此,在CWnd::PreCreateWindow 函数中给它们赋值,将能定义窗口的初始显示位置和大小。
下例中的代码将主框窗口的大小将固定为1/4屏幕,标题条中仅显示窗口名,不显示文档名。
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
// 修改主窗风格
cs.style &= ~FWS_ADDTOTITLE; //去除标题条中的文档名
cs.style &= ~WS_THICKFRAME; //去除可改变大小的边框
cs.style |= WS_DLGFRAME; //增加不能改变大小的边框
// 确定主窗的大小和初始位置
int cxScreen = ::GetSystemMetrics(SM_CXSCREEN);//获得屏幕宽
int cyScreen = ::GetSystemMetrics(SM_CYSCREEN); //获得屏幕高
cs.x = 0; // 主窗位于左上角
cs.y = 0;
cs.cx = cxScreen/2; // 主窗宽为1/2屏幕宽
cs.cy = cxScreen/2; // 主窗高为1/2屏幕高
return CMDIFrameWnd::PreCreateWindow(cs);
}
12. 控制滚动条
BOOL CDiagramShowView::PreTranslateMessage(MSG* pMsg)
{
"CFileTreeDoc* pDoc = (CFileTreeDoc*)GetDocument();
"CPoint point = GetScrollPosition();
"
"if(pMsg->message == WM_KEYDOWN)
"{
""switch(pMsg->wParam)
""{
""case VK_LEFT:
"""if( point.x > 10)
"""{
""""EndPoint.x = EndPoint.x - 10;
""""EndPoint.y = EndPoint.y;
"""}
"""else
"""{
""""EndPoint.x = 0;
""""EndPoint.y = EndPoint.y;
"""}
"""ScrollToPosition(EndPoint);
"""InvalidateRect(NULL,TRUE);
"""break;
""case VK_RIGHT:
"""if( point.x < pDoc->intDiagramColumnCount * pDoc->intColumnWidth - 10 )
"""{
""""EndPoint.x = EndPoint.x + 10;
""""EndPoint.y = EndPoint.y;
"""}
"""else
"""{
""""EndPoint.y = pDoc->intDiagramColumnCount * pDoc->intColumnWidth;
""""EndPoint.x = EndPoint.x;
"""}
"""ScrollToPosition(EndPoint);
"""InvalidateRect(NULL,TRUE);
"""break;
""case VK_UP:
"""if( point.y > 10)
"""{
""""EndPoint.y = EndPoint.y - 10;
""""EndPoint.x = EndPoint.x;
"""}
"""else
"""{
""""EndPoint.y = 0;
""""EndPoint.x = EndPoint.x;
"""}
"""ScrollToPosition(EndPoint);
"""InvalidateRect(NULL,TRUE);
"""break;
""case VK_DOWN:
"""if( point.y < pDoc->intDiagramRowCount * pDoc->intRowHeight - 10 )
"""{
""""EndPoint.y = EndPoint.y + 10;
""""EndPoint.x = EndPoint.x;
"""}
"""else
"""{
""""EndPoint.y = pDoc->intDiagramRowCount * pDoc->intRowHeight;
""""EndPoint.x = EndPoint.x;
"""}
"""ScrollToPosition(EndPoint);
"""InvalidateRect(NULL,TRUE);
"""break;
""default:
"""break;
""}
"}
"return FALSE;
}
// 通过正负号判断是向上还是向下滚动
if(zDelta==120)
向上滚动
if(zDelta==-120)
向下滚动
BOOL CDiagramShowView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
"CFileTreeDoc* pDoc = (CFileTreeDoc*)GetDocument();
"CPoint point = GetScrollPosition();
"
"if(zDelta==120)
"{
""if( point.y >= 20 )
""{
"""EndPoint.x = point.x;
"""EndPoint.y = point.y;
"""
"""EndPoint.x = EndPoint.x;
"""EndPoint.y = EndPoint.y - 20;
""}
""else
""{
"""EndPoint.x = EndPoint.x;
"""EndPoint.y = 0;
""}
"}
"
"if(zDelta==-120)
"{
""if( point.y <= pDoc->intDiagramRowCount * pDoc->intRowHeight - 20 )
""{
"""EndPoint.x = point.x;
"""EndPoint.y = point.y;
"""
"""EndPoint.x = EndPoint.x;
"""EndPoint.y = EndPoint.y + 20;
""}
""else
""{
"""EndPoint.x = EndPoint.x;
"""EndPoint.y = EndPoint.y;
""}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -