vc有用的命令

                        一、改变视图背景色:
                           在CView的OnDraw函数中添写如下一段程序代码:
                           void CFileNameView::OnDraw(CDC* pDC)
                           {
                        CFileNameDoc* pDoc = GetDocument();
                        ASSERT_VALID(pDoc);
                        CRect rectClient;
                        CBrush brushBkColor;
                        GetClientRect(rectClient);
                        brushBkColor.CreateSolidBrush(RGB(255,0,0)); //颜色设置
                        pDC->DPtoLP(rectClient);
                        pDC->FillRect(rectClient,&brushBkColor);
                        …
                           }
                        二、往基于对话框的程序添加菜单:
                        [1] 先添加菜单(IDR_MENU1)资源,并加上需要的菜单项。
                        [2]
                        编辑对话框资源IDD_DLGMENUTOOLBAR_DIALOG的属性,在属性对话框中选择IDR_MENU1即可。

                          
                        [3] 假如您不希望在对话框属性中直接设置菜单,而通过代码在程序中动态生成可以采用如下方法:
                        在CFileNameDlg类声名中添加成员变量CMenu
                        m_menu,再在CFileNameDlg::OnInitDialog() 中添加如下代码:
                        //加载菜单
                        m_menu.LoadMenu(IDR_MENU1);
                        //设置当前菜单
                        SetMenu(&m_menu);
                        //当你不需要菜单时可以用 SetMenu(NULL);来取消当前菜单
                        三、往基于Dialog的程序添加工具栏:
                        [1] 先添加工具栏(IDR_TOOLBAR1)资源,并画好各个按钮。
                        [2] 在CFileNameDlg类声名中添加成员变量 CToolBar m_wndtoolbar;
                        [3] 在CFileNameDlg::OnInitDialog() 中添加如下代码
                        //添加一个平面工具条
                        if (!m_wndtoolbar.CreateEx( this,TBSTYLE_FLAT , 
                        WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER |
                        CBRS_TOOLTIPS, CRect(4,4,0,0)) ||
                        !m_wndtoolbar.LoadToolBar(IDR_TOOLBAR1) )
                        {
                        TRACE0("failed to create toolbar\n");
                        return FALSE;
                        }
                        m_wndtoolbar.ShowWindow(SW_SHOW);
                        RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
                        AFX_IDW_CONTROLBAR_LAST, 0);


                        四、改变对话框背景色:
                        在CDlgMenuToolbarDlg::OnPaint()中修改代码实现Dialog 填充颜色:
                        CPaintDC dc(this);
                        CRect rect;
                        GetClientRect(rect);
                        dc.FillSolidRect(rect, RGB(60,110,170));
                        方法二、在InitInstance()(不是OnInitDialog())中加入:
                        SetDialogBkColor(RGB(255,0,0),RGB(0,255,0));
                        注意:要放在InitInstance函数的最前面!
                        五、为dialog的工具栏添加工具提示:
                        [1] 在CFileNameDlg类定义中手工添加消息映射函数的定义,如下黑体部分
                          //{{AFX_MSG(CFileNameDlg)
                        virtual BOOL OnInitDialog();
                        afx_msg void OnPaint();
                        afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT*
                        pResult);
                        //}}AFX_MSG
                        DECLARE_MESSAGE_MAP()
                        [2] 在CFileNameDlg.cpp添加函数的实现代码
                        //工具栏提示
                        BOOL CFileNameDlg::OnToolTipText(UINT, NMHDR* pNMHDR,
                        LRESULT* pResult)
                        {
                        ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code ==
                        TTN_NEEDTEXTW);  
                        // UNICODE消息
                        TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
                        TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
                        //TCHAR szFullText[512];
                        CString strTipText;
                        UINT nID = pNMHDR->idFrom;
                         
                        if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags &
                        TTF_IDISHWND) ||
                         pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags &
                        TTF_IDISHWND))
                        {
                         // idFrom为工具条的HWND
                         nID = ::GetDlgCtrlID((HWND)nID);
                        }
                         
                        if (nID != 0) //不为分隔符
                        {
                         strTipText.LoadString(nID);
                         strTipText = strTipText.Mid(strTipText.Find(’\n’,0)+1);

                          
                        #ifndef _UNICODE
                         if (pNMHDR->code == TTN_NEEDTEXTA)
                         {
                          lstrcpyn(pTTTA->szText, strTipText,
                        sizeof(pTTTA->szText));
                         }
                         else
                         {
                          _mbstowcsz(pTTTW->szText, strTipText,
                        sizeof(pTTTW->szText));
                         }
                        #else
                         if (pNMHDR->code == TTN_NEEDTEXTA)
                         {
                          _wcstombsz(pTTTA->szText,
                        strTipText,sizeof(pTTTA->szText));
                         }
                         else
                         {
                          lstrcpyn(pTTTW->szText, strTipText,
                        sizeof(pTTTW->szText));
                         }
                        #endif   
                         *pResult = 0;   
                         // 使工具条提示窗口在最上面
                         ::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0,
                        0,SWP_NOACTIVATE|
                          SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER);
                         return TRUE;
                        }
                        return TRUE;
                        }
                        [3] 在CFileNameDlg.cpp中添加消息映射,请看如下代码中的黑体部分
                        BEGIN_MESSAGE_MAP(CFileNameDlg, CDialog)
                        //{{AFX_MSG_MAP(CFileNameDlg)
                        ON_WM_PAINT()
                        ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipText )
                        //}}AFX_MSG_MAP
                        END_MESSAGE_MAP()
                        [4] 在CFileNameDlg.h中添加声明:
                        BOOL CFileNameDlg::OnToolTipText(UINT, NMHDR* pNMHDR,
                        LRESULT* pResult);
                        六、给没有工具栏的窗口添加工具栏:
                        在资源管理器中编辑工具栏,并将其属性改为IDR_MAINFRAME,然后在MainFrm.h中声明:
                        CToolBar m_wndToolBar;
                        在MainFrm.cpp中添加:
                        int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
                        {
                        m_wndToolBar.Create(this);
                        m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
                        ……;
                        }
                        停靠工具栏:在刚才添加的后面加入下面代码:
                        m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle()|CBRS_TOOLTIPS|CBRS_SIZE_DYNAMIC);

                        m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
                        EnableDocking(CBRS_ALIGN_ANY);
                        DockControlBar(&m_wndToolBar);//控制是否开启任意停靠
                        完善一下功能:
                        在菜单中添加一项“工具栏”,ID设为ID_VIEW_TOOLBAR,一切OK,试试吧!
                        七、创建分隔窗口:
                        只有框架类可以创建分隔,分隔可以嵌套。
                        在.h文件中声明 CSplitterWnd
                        m_wndSplitter;并且包含COneView.h(新建视图类)和CWinFrame.h(新建框架类)文件;

                        然后在.cpp文件中加入:
                        BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs,
                        CCreateContext* pContext)
                        {
                        if(!m_wndSplitter.CreateStatic(this,1,2))
                           return FALSE;
                        if(!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(COneView),CSize(240,420),pContext))

                           return FALSE;
                        if(!m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CWinFrame),CSize(300,500),pContext))

                           return FALSE;
                        return TRUE;
                        }
                        当用户创建好分割窗口后,有时并不希望通过拖动切分条来调节窗口的大小。这时就必须锁定切分条。锁定切分条的最简单的方法莫过于不让CSplitterWnd来处理WM_LBUTTONDOWN,WM_MOUSEMOVE,WM_SETCURSOR消息,而是将这些消息交给CWnd窗口进行处理,从而屏蔽掉这些消息。拿WM_LBUTTONDOWN处理过程来说。修改为如下:

                        void CXXSplitterWnd::OnLButtonDown(UINT nFlags,CPoint
                        point)
                        {
                         CWnd::OnLButtonDown(nFlags,point);
                        }
                        其余的处理方法类似。
                        八、“打开”按钮的设置:
                           用类向导创建该按钮的click函数,选择默认值OnOpen,加入以下代码:
                        void CYourDlg::OnOpen()
                        {
                        char szFileFilter[]=
                              "BIN File(*.bin)|*.bin|"
                              "All File(*.*)|*.*||";//文件类型过滤
                        CFileDialog
                        dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFileFilter);
                        /* CFileDialog dlg(FALSE);
                          dlg.m_ofn .lpstrFilter
                        =_T("文本文件(*.txt)*.txt所有文件(*.*)*.*");
                          dlg.m_ofn.lpstrDefExt=_T("txt"); */
                        if(dlg.DoModal()==IDOK)
                        {
                         m_path =
                        dlg.GetPathName();//将显示路径的Edit控件命名为m_path,并增加CString变量m_path

                         UpdateData(FALSE);  
                        }  
                        }
                        九、窗口居中:
                        在初始化(OnInit)函数中增加:CenterWindow();即可
                        十、对话框加状态条:
                           UINT
                        indicators[]={ID_INITMESSAGE,ID_SEPARATOR,ID_TIMEMESSAGE,ID_PROGRESS};

                           m_statusbar.CreateEx(this,0,WS_CHILD | WS_VISIBLE |
                        CBRS_BOTTOM);
                           m_statusbar.SetIndicators(indicators,4);
                           m_statusbar.ShowWindow (SW_SHOW);
                           RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
                        AFX_IDW_CONTROLBAR_LAST, 0);
                        十一、设置初始窗口状态:
                        BOOL CObjectNameApp::InitInstance()
                        {
                        m_pMainWnd->SetWindowText(""); //设置初始窗口标题文本
                        m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);//设置初始窗口为最大化
                        m_pMainWnd->UpdateWindow();
                        }
                        对于MDI函数SetWindowText无效,主窗口的标题只能在资源列表中修改,子窗口标题在**doc.cpp中重载OnNewDocument(),调用SetTitle("LGdownload中文版");来修改。

                        设置初始窗口最大化的另一优化方法:
                        void CMainFrame::ActivateFrame(int nCmdShow)
                        {
                           // TODO: Add your specialized code here and/or call
                        the base class
                           nCmdShow=SW_MAXIMIZE;
                           CFrameWnd::ActivateFrame(nCmdShow);
                        }
                        十二、对话框透明特效:
                        在OnInitDialog()中加入以下代码:
                        //加入WS_EX_LAYERED扩展属性
                        SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
                        GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);

                        HINSTANCE hInst = LoadLibrary("User32.DLL");
                        if(hInst)
                        {
                        typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);

                        MYFUNC fun = NULL;
                        //取得SetLayeredWindowAttributes函数指针
                        fun=(MYFUNC)GetProcAddress(hInst,
                        "SetLayeredWindowAttributes");
                        if(fun)fun(this->GetSafeHwnd(),0,128,2);
                        FreeLibrary(hInst);
                        }
                        注意:fun的参数128不能太小,否则就完全透明了!
                        十三、设置对话框里的STATIC控件颜色属性:
                        在该对话框增加WM_CTLCOLOR事件,加入以下代码:
                        if( nCtlColor==CTLCOLOR_STATIC )
                        {
                           pDC->SetTextColor(RGB(255,255,255));
                           pDC->SetBkColor(RGB(91,145,244));
                         pDC->SetBkMode(TRANSPARENT); //设置透明
                        }
                        设置STATIC控件背景透明:
                        if( nCtlColor==CTLCOLOR_STATIC )
                        {
                        pDC->SetBkMode(TRANSPARENT); //设置透明
                        return (HBRUSH)::GetStockObject(NULL_BRUSH);
                        }
                        十四、使窗口的最大化和最小化按钮消失:
                        在PreCreateWindow函数中添加以下代码即可:
                           int xSize = ::GetSystemMetrics( SM_CXSCREEN );
                           int ySize = ::GetSystemMetrics( SM_CYSCREEN );
                           cs.cx = xSize * 6 / 10;
                           cs.cy = ySize * 6 / 10;
                           cs.x = ( xSize - cs.cx ) / 2;
                           cs.y = ( ySize - cs.cy ) / 2;
                           cs.style &= ~WS_THICKFRAME;
                           cs.style &= ~( WS_MAXIMIZEBOX | WS_MINIMIZEBOX );
                           cs.dwExStyle |= WS_EX_TOOLWINDOW;
                        十五、设置控件字体颜色:(例如STATIC控件)
                        在OnCtlColor函数中添加如下代码:(可能需要选择STATIC的简单属性)
                        if(nCtlColor==CTLCOLOR_STATIC)   
                        {  pDC->SetTextColor(RGB(255,0,0));
                          pDC->SetBkColor(RGB(128,128,128));//设置文本背景色
                        pDC->SetBkMode(TRANSPARENT);//设置背景透明
                        }
                        其他控件的宏定义为:
                         .CTLCOLOR_BTN       按钮控件
                         .CTLCOLOR_DLG       对话框
                         .CTLCOLOR_EDIT      编辑框
                         .CTLCOLOR_LISTBOX   列表控件
                         .CTLCOLOR_MSGBOX    消息控件
                         .CTLCOLOR_SCROLLBAR 滚动条控件
                         .CTLCOLOR_STATIC    静态控件   
                        十六、将字符转换为数字:
                        int i = atoi("12345"); 或 sscanf("12345","%d",&i);   
                        十七、调用外部应用程序可使用的函数:
                         CreateProcess、WinExec、ShellExecute。
                        例:ShellExecute(pWnd->m_wnd, "open", "my.exe", NULL,
                        NULL, SW_NORMAL)
                        一、父窗体句柄,二、命令"open",三、文件路径,四、参数,五、运行路径,六、显示方式
                        十八、经典安装程序制作软件:InstallShield for Microsoft Visual C++6.0
                        release 方式是在build菜单中的Set Active configuration中改
                        在project菜单里面,选Add to Project的component and
                        control来加入ocx控件
                        十九、控件的注册:
                        1.注册
                        regsvr32 x:\xxx\demo.ocx 不一定非得在 Windows 系统目录
                        2.卸载
                        regsvr32 /u x:\xxx\demo.ocx
                        二十、获取当前时间:
                        CTime m_time=CTime::GetCurrentTime();
                        char szText[100];
                        memset(szText,0,100);
                        sprintf(szText,"%d_%d_%d",m_time.GetHour(),m_time.GetMinite(),m_time.GetSecond());

                         // 如何得到当前时间日期
                           CTime time = CTime::GetCurrentTime();   
                           CString m_strTime = time.Format("%Y-%m-%d %H:%M:%S");

                           // 方法二
                           SYSTEMTIME ti;
                           GetSystemTime(&ti); // 如何得到当前时间日期
                           ti.wMilliseconds; // 得到毫秒时间
                        SYSTEMTIME time;
                        CString str;
                        GetLocalTime( &time );
                        str.Format(
                        "%04d:%02d:%02d",time.wYear,time.wMonth.....);
                        二一、修改单文档程序的标题:
                        OnCreat()中加入  SetWindowText("weichao");
                        CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中加入 
                        cs.style =WS_OVERLAPPEDWINDOW;
                        二二、隐藏程序在任务栏的图标:
                        对话框程序放OnInitDialog()函数:
                        SetWindowLong(this->m_hWnd,GWL_EXSTYLE,WS_EX_TOOLWINDOW);//隐藏任务拦按钮

                        二三、读取编辑框内容:
                           GetDlgItemText(IDC_EDIT_TXDATA,m_strTXData);
                        二四、自绘菜单宽度不对,高度是对的,解决办法:
                        在ClassWizard中的ClassName下,选CMianFrame,在Messages下选WM_CONTEXTMENU并生成相应的函数,如下:

                        void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point)

                        {
                         CMenu menu;
                            menu.LoadMenu(IDR_MENU1);//IDR_MENU1是你要弹出菜单的ID号.
                            CMenu *popup=menu.GetSubMenu(0);
                           
                        popup->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);

                        }
                        二五、重启计算机:
                        调用一个API函数ExitWindowsEx就可以了,
                        两个参数:UFlag,标记,可以选择EWX_REBOOT,EWX_SHUTDOWN,EWX_POWEROFF再或上EWX_FORCE第二个参数就是0了

                        二六、无title对话框的移动:
                        void CScreenSnapDlg::OnLButtonDown(UINT nFlags, CPoint
                        point)
                        {
                           //实现窗体无标题移动
                          
                        PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));

                            
                           CDialog::OnLButtonDown(nFlags, point);
                        }
                        二七、获取操作系统版本:
                        OSVERSIONINFO OsVersionInfo;//包含操作系统版本信息的数据结构
                        OsVersionInfo.dwOSVersionInfoSize=
                        sizeof(OSVERSIONINFO);
                        GetVersionEx(&OsVersionInfo);//获取操作系统版本信息
                        二八、设置对话框为最顶层:(在OnInitDialog中添加)
                        SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

                        二九、对话框程序不在任务栏显示:(在OnInitDialog中添加)
                        ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);
                        三十、向对话框窗口添加右键菜单:
                        CMenu  menu,*pmenu;   
                        menu.LoadMenu(IDR_MENU1);   
                        pmenu=menu.GetSubMenu(0);   
                         
                        CPoint  ptScreen(point);   
                        ClientToScreen(&ptScreen);   
                         
                        pmenu->TrackPopupMenu(TPM_RIGHTBUTTON,ptScreen.x,ptScreen.y,this);
                          
                        三一、文件查找:(例查找连续的换行符)
                        FILE *fp,*fp1;
                        int flag=0;
                        int ch;
                        fp=fopen("c:\test.txt","r");
                        fp1=fopen("c:\wrttest.txt","w");
                        while(!feof(fp))
                        {
                         ch=fgetc(fp);
                         if(feof(fp))
                          break;
                         if(ch==’\n’&&flag==1)
                          continue;
                         else if(ch==’\n’&&flag==0)
                          flag=1;
                         else
                          flag=0;
                         fputc(ch,fp1);
                        }
                        fclose(fp1);
                        fclose(fp);
                        三二、托盘菜单不点击不能消失的解决办法:
                        在菜单之后使用下述代码:
                               CPoint pt;
                               GetCursorPos(&pt);
                                
                               SetForegroundWindow();
                              
                        NotifyMenu.TrackPopupMenu(TPM_RIGHTBUTTON,pt.x,pt.y,this);

                               PostMessage(WM_NULL,0,0);
                        三三、对话框由小到大显示的动画效果:
                        在InitDialog中增加:
                        ShowWindow(SW_HIDE);
                        CRect dlgRect;
                        GetClientRect(&dlgRect);
                        CPoint centerPoint;
                        centerPoint.x=dlgRect.Width()/2;
                        centerPoint.y=dlgRect.Height()/2;//得到对话框的中点坐标
                        CRgn testrgn;
                        this->ShowWindow(SW_HIDE);   
                        int m=GetSystemMetrics(SM_CYSIZEFRAME);
                         
                        //以下代码实现对话框的动态弹出
                         
                        for (int i=10;i<dlgRect.Width()/2+m;i+=1)
                        {    
                        
                        testrgn.CreateRectRgn(centerPoint.x-i,centerPoint.y-i,centerPoint.x+i,centerPoint.y+i);

                         SetWindowRgn((HRGN) testrgn,TRUE);
                         ShowWindow(SW_SHOW);
                         CenterWindow();
                         testrgn.DeleteObject();
                        }
                        三四、按行读出文本文件:
                        下面的例子演示了一行一行取,直到取完。
                        CStdioFile myFile;
                          CString ReadFileString;
                        if(myFile.Open("C:\Readme.txt", Cfile::modeRead) ==
                        TRUE)
                        {
                         while(myFile.ReadString(ReadFileString) != FALSE)
                         {
                                             //... 处理代码
                                          }
                                  }
                        三五、使用IDC_HAND时提示未定义,加入以下代码:
                        #if(WINVER >= 0x0500)
                        #define IDC_HAND            MAKEINTRESOURCE(32649)
                        #endif /* WINVER >= 0x0500 */
                        三六、使应用程序启动时不自动创建新文档
                        在默认情况下,用AppWizard开发的SDI/MDI应用程序在启动时创建一个新的文档。如果要使应用程序在启动时不创建新文档,只需在应用类CmyApp::InitInstance()函数的ProcessShellCommand调用前加上下面的语句就可以了:

                        cmdInfo.m_nShellCommand = CComandLineInfo::FileNothing;
                        三七、播放mp3:
                           CFileDialog file(true);
                           if(file.DoModal()==IDOK)
                           {
                               CString filename=file.GetFileName();
                               if(hwnd!=NULL)
                               {
                                   MCIWndDestroy(hwnd);
                               }
                              
                        hwnd=MCIWndCreate(this->m_hWnd,NULL,MCIWNDF_NOPLAYBAR,filename);

                               ::ShowWindow(hwnd,SW_HIDE);
                               MCIWndSetVolume(hwnd,1000);
                               MCIWndPlay(hwnd);
                           }
                        三八、获取屏幕RGB值:OnTimer中添加
                        CPoint pos;
                        GetCursorPos(&pos);//获取鼠标坐标
                        HDC hDC = ::GetDC(NULL);
                        COLORREF clr = ::GetPixel(hDC, pos.x, pos.y);
                            
                        CString ClrText;
                        ClrText.Format("R:%d G:%d
                        B:%d",GetRValue(clr),GetGValue(clr),GetBValue(clr));

                        三九、打开一个网址:
                        打开http://www.sina.com.cn这个站点如下:
                        ShellExecute(NULL, "open",
                        "http://www.sina.com.cn",NULL, NULL, SW_MAXIMIZE );
                        此命令将以默认浏览器打开http://www.sina.com.cn,并将加开后的窗口最大化。
                        又例:
                        ShellExecute(NULL, "open", "IEXPLORE.exe
                        http://www.sina.com.cn",NULL, NULL, SW_MAXIMIZE );
                        此命令将直接用IE打开一个sina的站点。不过将开一个新的窗口。
                        四十、位图按钮:
                        CButton *pRadio = (CButton*)GetDlgItem(IDC_RADIO);
                        pRadio->SetBitmap(::LoadBitmap(AfxGetInstanceHandle(),
                        MAKEINTRESOURCE(IDB_BITMAP)));


                     

posted @ 2006-12-16 21:52  ahuo  阅读(462)  评论(0编辑  收藏  举报