现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

vc++经典技巧总结(二)

2012-08-10 22:17 工业·编程 ⁄ 共 5472字 ⁄ 字号 暂无评论

6. ①怎样在程序开始的时候让它最大化?
②vc++做出来的exe文件在窗体的右上方是没有最大化和最小化按钮的,怎样实现这一功能? 
③如何在显示窗口时,使最大化按钮变灰?
①在App类里的C…App::InitInstance()中把m_pMainWnd->ShowWindow(SW_SHOW)改成m_pMainWnd->ShowWindow(SW_MAXIMIZE);
②在CreateWidnow时用WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX 风格.
③ 第一种方法:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) 

if( !CFrameWnd::PreCreateWindow(cs) ) 
return FALSE; 
// TODO: Modify the Window class or styles here by modifying 
// the CREATESTRUCT cs 
// disable the maxmini box 
cs.style &= ~WS_MAXIMIZEBOX; 
return TRUE; 
}
第二种方法:
CMenu *pMenu=AfxGetApp()->m_pMainWnd->GetSystemMenu(FALSE); 
  int x=pMenu->GetMenuItemCount( ); 
  UINT pID=pMenu->GetMenuItemID(x-1); 
  pMenu->EnableMenuItem(pID, MF_DISABLED);
第三种方法:
ModifyStyle(WS_MAXIMIZEBOX, 0); 
这个函数也可以是最大化按钮失效! 
并且可以在程序中动态的改变窗口的风格
7. 更改属性页标题
void CProSheet::SetPageTitle(int nPage, int nImage, CString strTitle)
{
TC_ITEM item;
//item.mask = TCIF_TEXT|TCIF_IMAGE; //设置图标+文字
item.mask = TCIF_IMAGE; //只设置图标
item.iImage = nImage;
// item.pszText = strTitle.GetBuffer(0); //设置文字
GetTabControl ()->SetItem (nPage, &item);
//要设置文字时就将上面2行有注释符的代码前的注释符去掉
}
8. 创建动态菜单
void CMainFrame::OnSelectState(NMTOOLBAR* pnmtb, LRESULT *plr) 

"CMenu menu; 
"if(!menu.CreateMenu()) 
"return; 
"menu.AppendMenu(MF_STRING,0,"开始"); 
"menu.AppendMenu(MF_STRING,0,"结束"); 
"CRect rc; 
"m_wndToolBar.SendMessage(TB_GETRECT, pnmtb->iItem, (LPARAM)&rc);
"m_wndToolBar.ClientToScreen(&rc); 
"menu.TrackMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL, 
"""rc.left, rc.bottom, this, &rc); 
//"menu.DestroyMenu(); 
"menu.detach();
}
9.关于打印
1.要打印哪个视就 
((CMainFrame*)AfxGetMainWnd())->m_wndSplitter.SetActivePane(...) 
//要打印的那个视对应的Pane
2.有一个单文档工程,文档窗口被切分:左视图由CTreeView 的派生类管理,右视图由CListView 的派生类CMyListView(其为风格为LVS_REPORT)管理,我想为右视图添加打印和打印预览,我在MyListView.cpp中添加了 
ON_COMMAND(ID_FILE_PRINT,CListView::OnFilePrint) 
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CListView::OnFilePrintPreview)还有 
BOOL CMyListView::OnPreparePrinting(CPrintInfo* pInfo) 

  // TODO: call DoPreparePrinting to invoke the Print dialog box 
  // return CListView::OnPreparePrinting(pInfo); 
  pInfo->SetMaxPage(2); 
  BOOL bret=DoPreparePrinting(pInfo); 
  pInfo->m_nNumPreviewPages=2; 
  return bret; 
}
3. 下面是从MSDN中摘出来的一段,是用来改变消息路由的。用了这段代码之后,CView中的消息(菜单,控件,子窗口)将先被CMyShape类来处理。不知道你要的是不是这样的效果。 
// 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); 
}

给我留言

留言无头像?