十种VC编程技巧

来源:互联网 发布:福鼎广电网络客服电话 编辑:程序博客网 时间:2024/04/28 15:45

(-). 常见的Afx全局函数:
    AfxFormatString1:类似printf一般地将字符串格式化
    AfxFormatString2:类似printf一般地将字符串格式化
    AfxMessageBox:类似Windows API 函数 MessageBox
    AfxOuputDebugString:将字符串输往除错装置
    AfxGetApp:获得application object (CwinApp派生对象)的指针
    AfxGetMainWnd:获得程序主窗口的指针
    AfxGetInstance:获得程序的instance handle

(二). CString 与char []之间的转换.  
    char str[10] = ”str”;
    CString sstr = “sstr”;
    sstr.Format(“%s”,str);
    strcpy(str,(LPCTSTR)sstr);
 
(三). 关闭程序:
    PostQuitMessage(WM_CLOSE);
    或者PostQuitMessage(WM_DESTROY);
    更绝的是关闭所有的程序:::ExitWindows ();

(四). 在关闭窗口时,当要对文件进行保存时,可在这里添加函数:
    1.)在CMainFrame里的OnClose()里,用MessageBox("内容","标题",组合形式);组合形式可以查看MSDN的MESSAGEBOX( ) Function
    2.)在CXXXDoc::SaveModified() 里,只能用AfxMessageBox(""); 不能用MessageBox()函数
 
(五). 如何修改窗体的标题:
    1.)修改主窗口的标题:m_pMainWnd->SetWindowText("你的标题");
    2.)如果在你的document类中进行改,则直接调用SetTitle("..."),如果在你的view类中改,则GetDocument()->SetTitle("...")
    3.)如果想使窗口的标题全部替换,则用:AfxGetMainWnd()->SetWindowText("你的标题");
 
(六). 得到窗体的标题:
    1.)AfxGetMainWnd()->GetWindowText();  
    2.)先FindWindow()找到窗口的HWND,在GetWindowText();

(七). 在多文档/视图中:
    1.)子窗口的最大化:
        void CChildFrame::ActivateFrame(int nCmdShow)
        {
            // TODO: Add your specialized code here and/or call the base class
            nCmdShow=SW_MAXIMIZE;
            CMDIChildWnd::ActivateFrame(nCmdShow);
        }
    

    2.)屏蔽子对话框:在APP类里把这两句话屏蔽掉
        if (!ProcessShellCommand(cmdInfo))
            return FALSE;
    3.)关闭子窗口:
        ::SendMessage(::AfxGetMainWnd()->m_hWnd, WM_COMMAND,ID_FILE_CLOSE,0);
 
(八). 在装进自定义的光标后,在移动的过程中,鼠标的形状总是在自定义和默认的光标之间晃动,可以这样解决,在视中的PreCreateWindow()中加入如下几句:
    BOOL CXXXXView::PreCreateWindow(CREATESTRUCT& cs)
    {
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs
        cs.lpszClass =AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,0,
                             (HBRUSH)::GetStockObject (WHITE_BRUSH),0);
        return CView::PreCreateWindow(cs);
    }

(九). 怎样禁止改变窗口的大小和不能移动的窗口:
    在 CMainFrame的OnCreate函数中加入:
    CMenu *pTopMenu=GetSystemMenu(false);
    pTopMenu->RemoveMenu(4,MF_BYPOSITION);//最大化窗口不可用
    pTopMenu->RemoveMenu(2,MF_BYPOSITION);//size
    pTopMenu->RemoveMenu(1,MF_BYPOSITION);//使不可移动
 
(十).使窗口始终在最前方:
只要在App类中的InitInstance()函数中加入以下代码就可以了:
BOOL CwindowOnTopApp:: InitInstance()
{
      //此处略去了VC自动生成的代码
      m_pMainWnd->showWindow(SW_SHOW);
      m_pMainWnd->UpdateWindow();
      m_pMainWnd->SetWindowPos(&CWnd::WndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
      Return true;
}

 
原创粉丝点击