| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | | | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:32 评论数量:44 留言数量:0 访问次数:180565 建立时间:2005年1月4日 |

| |
|
[WINDOWS]MFC开发常见问题的回答 1 2 3 文章收藏
eaglebetter 发表于 2006/7/11 21:56:54 |
| 1,怎样改变视图的大小?
一般,你能改变试图大小依靠 MoveWindow()MFC应用程序,视图是所围绕其框架的子窗口,获取其框架的指针GetParentFrame(),然后依靠MoveWindow()改变框架大小,视图大小自动跟随框架大小改变。
2,如何改变一个CFormView的大小?
首先,你要在你的CFormView中重载OnInitialUpdate()函数,该函数声明如下:virtual void OnInitialUpdate();在OnInitialUpdate()添加如下代码:void ClikethisView::OnInitialUpdate()//ClikethisView从CFormView继承而来{ // Make the window the size of the main dialog. CFormView::OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit( /*FALSE*/ );}
3,如何改变一个视图的背景?
想改变CView、CFrameWnd或者CWnd的背景,需要处理WM_ERASEBKGND 消息。像下面演示的这样:BOOL CSampleView::OnEraseBkgnd(CDC* pDC){ // 设置背景画刷颜色 CBrush backBrush(RGB(255, 128, 128)); // 保存旧画刷 CBrush* pOldBrush = pDC->SelectObject(&backBrush); CRect rect; pDC->GetClipBox(&rect); // 擦掉所要画的区域 pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); pDC->SelectObject(pOldBrush); return TRUE;}
4,改变对话框背景颜色后,如何使控件的背景颜色和对话框背景颜色统一?
请看下面代码: 重载画控件的消息。HBRUSH dlgtest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { switch (nCtlColor) { case CTLCOLOR_BTN://按钮? case CTLCOLOR_STATIC://我们所需要改画的静态 { pDC->SetBkMode(TRANSPARENT); } case CTLCOLOR_DLG: { CBrush* back_brush; COLORREF color; color = (COLORREF) GetSysColor(COLOR_BTNFACE); back_brush = new CBrush(color); return (HBRUSH) (back_brush->m_hObject); } } return(CFormView::OnCtlColor(pDC, pWnd, nCtlColor));}
5,如何获得当前视图的指针?
((CFrameWnd*) AfxGetApp()->m_pMainWnd))->GetActiveDocument();
或者
((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
6,如何获得MDI程序的所有视图?
下面函数对你有用:CDocument::GetFirstViewPosition(); // DOCCORE.CPPCDocument::GetNextView(); // DOCCORE.CPPCMultiDocTemplate::GetFirstDocPosition(); // DOCMULTI.CPPCMultiDocTemplate::GetNextDoc(); // DOCMULTI.CPP
文档精彩问答:
1,如何获得当前文档指针?
参照:上面“如何获得当前视图指针?”
2,文档什么时候被破坏?
单文档程序的文档当程序退出时被破坏。多文档程序在最后一个视图关闭时被破坏。
3,如何获得当前打开文档的列表?
下面代码,MyApp从CWinApp继承而来,MyApp的成员变量:CPtrList m_templateList
void CMyApp::GetDocumentList(CObList * pDocList){ ASSERT(pDocList->IsEmpty()); POSITION pos = m_templateList.GetHeadPosition(); while (pos) { CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos); POSITION pos2 = pTemplate->GetFirstDocPosition(); while (pos2) { CDocument * pDocument; if ((pDocument=pTemplate->GetNextDoc(pos2)) != NULL) pDocList->AddHead(pDocument); } }}
4,如何不叫我的程序自动打开文档?
在InitInstance()函数中cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing
*************************************************************************
1.如何让我的程序运行的时候最大化?(1)在appwizard第4步选择“advanced"从中选择Mainframe的Maximized(2)对于MDI程序,在CWinApp::InitInstance() 中做下面改动// Create main MDI Frame window.CMainFrame* pMainFrame = new CMainFrame;if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE;m_nCmdShow = SW_SHOWMAXIMIZED; // 注意添加此行!!!pMainFrame->ShowWindow(m_nCmdShow);pMainFrame->UpdateWindow();m_pMainWnd = pMainFrame;(3)对于SDI程序,在CWinApp::InitInstance() 中的OnFileNew()调用之前m_nCmdShow = SW_SHOWMAXIMIZED;// 下面创建空文档OnFileNew();2,如何给其他线程发消息?用SendNotifyMessage() 函数。3,如何让我的程序只运行一次?const char* MyMainWndClassName = "MyMainWndXQW"BOOL CMyApp::InitApplication(){ // Call base class. Default version does nothing. CWinApp::InitApplication(); WNDCLASS wndcls; // Start with NULL defaults. memset(&wndcls, 0, sizeof(WNDCLASS)); // Get class information for default window class. ::GetClassInfo(AfxGetInstanceHandle(),"AfxFrameOrView",&wndcls); // Substitute unique class name for new class. wndcls.lpszClassName = MyMainWndClassName; // Register new class and return the result code. return ::RegisterClass(&wndcls);}BOOL CMyApp::FirstInstance(){ CWnd *PrevCWnd, *ChildCWnd; // Determine if another window with our class name exists. PrevCWnd = CWnd::FindWindow(MyMainWndClassName, NULL); if (PrevCWnd != NULL) { // If so, does it have any pop-ups? ChildCWnd=PrevCWnd->GetLastActivePopup(); // Bring the main window to the top. PrevCWnd->BringWindowToTop(); // If iconic, restore the main window. if (PrevCWnd->IsIconic()) PrevCWnd->ShowWindow(SW_RESTORE); // If there are pop-ups, bring them along too! if (PrevCWnd != ChildCWnd) ChildCWnd->BringWindowToTop(); // Return FALSE. This isn't the first instance // and we are done activating the previous one. return FALSE; } else // First instance. Proceed as normal. return TRUE;}CMyApp::InitInstance(){ if (!FirstInstance()) return FALSE; // ...}4,MDI程序,关闭子窗口同时关闭父窗口,该如何做?在子窗口的OnClose函数里添加ASSERT(AfxGetMainWnd() != NULL);AfxGetMainWnd()->SendMessage(WM_CLOSE);菜单问题:1,我在程序中用了MenuBar,结果找不到菜单了,我的方法是:CMenu *menu;menu = GetMenu()->GetSubMenu(0);答:AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(0);2,如何动态修改MainFrame的菜单?CMenu newMenu;newMenu.LoadMenu (IDR_MENU1);AfxGetMainWnd()->SetMenu( &newMenu );AfxGetMainWnd()->DrawMenuBar();newMenu.Detach ();
*************************************************************************
问:MFC程序如何处理WM_SETTEXT消息?答:MFC没有提供WM_SETTEXT消息的映射函数,所以需要我们自己定义在message map里面OnMessage(WM_SETTEXT, OnSetText) 在.h里面声明void LRESULT CMyClass::OnSetText(wParam, lParam);//bool...等等函数类型自己决定
问:如何注册自己的消息?答:在message map里面#define ON_WM_CHKTBLTOGGLE(){WM_CHKTBLTOGGLE, 0, AfxSig_vwp, (AFX_PMSG)(AFX_PMSGW)(BOOL (AFX_MSG_CALL CWnd::*)(BYTE, BYTE))OnChkTblToggle },ON_MESSAGE(WM_CHKTBLTOGGLE, OnChkTblToggle)如此声明消息函数afx_msg LRESULT OnChkTblToggle(WPARAM wParam, LPARAM lParam);LRESULT CMyView::OnChkTblToggle(WPARAM wParam, LPARAM lParam){ // TODO: write your code here.}void CMYView::OnChkTblToggle(UINT, CPoint)
问:如何使我的窗口总位于屏幕顶端?SetWindowPos(&wndTopMost,NULL,NULL,NULL,NULL,SWP_NOMOVE¦SWP_NOSIZE);
问:怎么弄一个HDC创建一个CDC?有时候window API会给你一个DC的句柄,而你想利用其CDC,比如自画ComBox、按钮、tabctrl等等下面有两种方法从一个hdc创建到CDC,你能把它们利用到任何MFC应用程序中!方法一:void MyODList::DrawItem(LPDRAWITEMSTRUCT lpDrawItem) { CDC myDC; myDC.Attach(lpDrawItem->hDC); //在这里做你想做的! //I如果你不做下面的 detach, window会很不高兴哦:) myDC.Detach();} 方法二:CDC* pDC = CDC:FromHandle(lpDrawItem->hDC);没法说出上面良种方法在效率资源上的差异,至少下面的很简单,并且,你不会因忘记detach出错....
如何去掉窗口的标题条?答:int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){LONG dwStyle = ::GetWindowLong(GetSafeHwnd(), GWL_STYLE):dwStyle &= ~(WS_CAPTION¦WS_BORDER ):::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle)://..... |
|
|