VC 基本操作

改变窗口大小和标题修改: 

在Cmainframe中的precreatewindow中 

        cs.cx=600; 

cs.cy=600; 

cs.style=cs.style&~FWS_ADDTOTITLE; 

cs.lpszName="jiaotong";

 

光标,背景 

在CMyview中的precreatewindow中 

cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, 

LoadCursor(NULL,IDC_HELP),(HBRUSH)GetStockObject(BLACK_BRUSH),0);

动态图标修改: 

首先在resources中的Icon中插入两个图标然后按照以下步骤 

1.在mainframe类里的m_wndStatusBar中添加HICON的一个数组,大小为2 

public:HICON m_hIcons[2]; 

2.在CMainFrame::OnCreate里加载图标 

m_hIcons[1]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1)); 

m_hIcons[2]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON2)); 

SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[1]);//将开始的图标设置为第一个图标 

SetTimer(1,1000,NULL); 

3.自定义一个定时器(点击右键选择add windows Message。。。在里面选择VM_TIMER) 

void CMainFrame::OnTimer(UINT nIDEvent) 

static int i=1; 

SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[i+1]); 

i=++i%2; 

CFrameWnd::OnTimer(nIDEvent); 

}

工具栏添加属性: 

1。在resources里双击menu,然后在查看上空白处单击右键选择属性ID为ID_TEST,名称对话框,然后在对话框上点击右键选择建立类向导,在Message里双击第一个common,出现对话框后,确定,然后Edit Code 

工具栏显示消息: 

   void Cmainframe::onTest() 

{

   MessageBox("error");

}

 

插入新的工具栏: 

在resources中toolbar插入新的toolbar在CMainframe中Oncreate中复制 

if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP 

| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 

!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) 

TRACE0("Failed to create toolbar/n"); 

return -1;      // fail to create 

然后将!m_wndToolBar改成!m_wndnewToolBar,并且转入m_wndToolBar定义中,添加CToolBar    m_wndnewToolBar;再转入IDR_MAINFRAME定义中,复制IDR_TOOLBAR1并替换新定义的IDR_MAINFRAME 

复制m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);与DockControlBar(&m_wndToolBar);并将其中的m_wndtoolbar都改为m_wndnewtoolbar

工具栏显示和隐藏: 

void Cmainframe::onTest() 

    { 

if(m_NewToolBar,IsWindowVisible()) 

m_NewToolBar.ShowWindow(SW_HIDE); 

else 

m_NewToolBar.ShowWindow(SW_SHOW);

 

状态栏时间显示: 

首先在resources中的string table中添加IDS_TIMER,并且起名时间 

然后在Cmainframe中的onCreate中数组static UINT indicators[]里添加IDS_TIMER,(位置不同下面定义也就随之变化,这里我定义在第四个位置),然后在CMYview中在最上面添加库函数#include "MainFrm.h"

自定义一个定时器(点击右键选择add windows Message。。。在里面选择VM_TIMER)

 

void CMainFrame::OnTimer(UINT nIDEvent) 

// TODO: Add your message handler code here and/or call default 

1。CTime t=CTime::GetCurrentTime(); 

2。CString str=t.Format("%H:%M:%S"); 

3。CClientDC dc(this); 

CSize sz=dc.GetTextExtent(str); 

m_wndStatusBar.SetPaneInfo(3,IDS_TIMER,SBPS_NORMAL,sz.cx); 

    m_wndStatusBar.SetPaneText(3,str);

CFrameWnd::OnTimer(nIDEvent); 

123条为补充块长度

 

状态栏显示鼠标坐标

1首先在resources中的string table中添加IDS_POINT并起名坐标 

2在Cmainframe中的onCreate中数组static UINT indicators[]里添加IDS_POINT, 

3将MainFrm中的成员变量m_wndStatusBar修改为公有变量,并在视图类中添加头文件#include "MainFrm.h"。 

4利用类向导添加鼠标移动消息处理函数WM_MOUSEMOVE(),并添加如下代码为: 

void CEwqView::OnMouseMove(UINT nFlags, CPoint point) 

// TODO: Add your message handler code here and/or call default 

CClientDC   dc(this);    

CMainFrame *pFrame=(CMainFrame *)AfxGetApp()->m_pMainWnd;    

CStatusBar *pStatusBar=(CStatusBar *)&pFrame->m_wndStatusBar;    

CString   str;    

str.Format("X:%d,Y:%d",point.x,point.y);    

   CSize size=dc.GetTextExtent(str);    

int nIndex=pStatusBar->CommandToIndex(ID_INDICATOR_POINT);    

pStatusBar->SetPaneInfo(nIndex,ID_INDICATOR_POINT,SBPS_NORMAL,size.cx);    

pStatusBar->SetPaneText(nIndex,str);   

 

CView::OnMouseMove(nFlags, point); 

}

添加对话框: 

在resources中dialog中插入新的对话框 

然后在建立的对话框上点击右键选择建立类向导,然后起名jiaotong确认,在classname下选择CjiaotongApp然后确定 

在你建立的jiaotong类中复制#include "jiaotong.h"并粘贴到CjiaotongApp中CjiaotongApp{}库函数中,最后在CjiaotongApp{}中修改为 

void CJiaotong1App::OnAppAbout() 

jiaotong aboutDlg; 

aboutDlg.DoModal(); 

用鼠标画线: 

1.在CJiaotongview中定义 

CPoint     m_ptPrev; 

CPoint     m_ptOld; 

2.在CJiaotongview中自定义VM_LButtonDown,VM_LButtonUp,VM_MouseMove,然后如下添加 

void CJiaotong1View::OnLButtonDown(UINT nFlags, CPoint point)

{    

// TODO: Add your message handler code here and/or call default 

     m_ptPrev = point; 

     m_ptOld = point; 

    SetCapture(); 

    CView::OnLButtonDown(nFlags, point);

}

void CJiaotong1View::OnLButtonUp(UINT nFlags, CPoint point) 

// TODO: Add your message handler code here and/or call default 

if (GetCapture() != this) 

       return; 

     CDC *pDC=GetDC();     

     pDC->MoveTo(m_ptPrev);     

     pDC->LineTo(point); 

     ReleaseDC(pDC); 

     ReleaseCapture(); 

}

void CJiaotong1View::OnMouseMove(UINT nFlags, CPoint point) 

// TODO: Add your message handler code here and/or call default 

    if (GetCapture() != this) 

       return; 

    int oldMode; 

     CDC *pDC=GetDC(); 

     oldMode = pDC->GetROP2(); 

     pDC->SetROP2(R2_NOT);     

     pDC->MoveTo(m_ptPrev); 

     pDC->LineTo(m_ptOld);

     pDC->MoveTo(m_ptPrev); 

     pDC->LineTo(point); 

     pDC->SetROP2(oldMode);    

     m_ptOld = point; 

     ReleaseDC(pDC); 

CView::OnMouseMove(nFlags, point); 

如果要改变颜色,那么将如下代码加入void CJiaotong1View::OnMouseMove(UINT nFlags, CPoint point)中 

CPen pen(PS_SOLID,1,RGB(234,23,53));    

     CClientDC dc(this);

     CPen *pOldPen=dc.SelectObject(&pen);

     dc.MoveTo(m_ptPrev);

     dc.LineTo(point);

     dc.SelectObject(pOldPen);

 

1) 在View中获得Doc指针 

2) 在App中获得MainFrame指针 

3) 在View中获得MainFrame指针 

4) 获得View(已建立)指针 

5) 获得当前文档指针 

6) 获得状态栏与工具栏指针 

7) 获得状态栏与工具栏变量 

8) 在Mainframe获得菜单指针 

9) 在任何类中获得应用程序类 

10) 从文档类取得视图类的指针(1)

VC中编程对于刚刚开始学习的同学,最大的障碍和问题就是消息机制和指针获取与 

操作。其实这些内容基本上是每本VC学习工具书上必讲的内容,而且通过MSDN很多 

问题都能解决。下面文字主要是个人在编程中指针使用的一些体会,说的不当的地 

方请指正。一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架, 

无论是多文档还是单文档,都存在指针获取和操作问题。下面这节内容主要是一般 

的框架,然后再讲多线程中的指针使用。使用到的类需要包含响应的头文件。首先 

一般获得本类(视,文档,对话框都支持)实例指针this,用this的目的,主要可以通 

过类中的函数向其他类或者函数中发指针,以便于在非本类中操作和使用本类中的 

功能。

1) 在View中获得Doc指针 CYouSDIDoc *pDoc=GetDocument();一个视只能有一个文 

档。 

2) 在App中获得MainFrame指针 

CWinApp 中的 m_pMainWnd变量就是MainFrame的指针 

也可以: CMainFrame *pMain =(CMainFrame *)AfxGetMainWnd(); 

3) 在View中获得MainFrame指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd; 

4) 获得View(已建立)指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd; 

CyouView *pView=(CyouView *)pMain->GetActiveView(); 

5) 获得当前文档指针 CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument(); 

6) 获得状态栏与工具栏指针 CStatusBar * pStatusBar=(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR); 

CToolBar * pToolBar=(CtoolBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);

7) 如果框架中加入工具栏和状态栏变量还可以这样 

(CMainFrame *)GetParent()->m_wndToolBar; 

(CMainFrame *)GetParent()->m_wndStatusBar;

8) 在Mainframe获得菜单指针 CMenu *pMenu=m_pMainWnd->GetMenu(); 

9) 在任何类中获得应用程序类 

用MFC全局函数AfxGetApp()获得。

10) 从文档类取得视图类的指针 

我是从http://download.cqcnc.com/soft/program/article/vc/vc405.html学到的, 

从文档获得视图类指针目的一般为了控制同一文档的多个视图的定位问题,我的体会 

特别是文字处理CEditView当产生多个视图类时,这个功能是非常需要的。 

CDocument类提供了两个函数用于视图类的定位: 

GetFirstViewPosition()和GetNextView() 

virtual POSITION GetFirstViewPosition() const; 

virtual CView* GetNextView(POSITION& rPosition) const;

三、浏览文件夹对话框

//回调函数

int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)

{

if(uMsg==BFFM_SELCHANGED||uMsg==BFFM_INITIALIZED)

{

     if(uMsg==BFFM_INITIALIZED)

     {

       ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,LPARAM(lpData));

     }

}

return 0;

}

TCHAR chPath[255]; //用来存储路径的字符串

CString strPath     = "";

BROWSEINFO bInfo;

GetModuleFileName(NULL,chPath,MAX_PATH);

strPath             =chPath;

ZeroMemory(&bInfo, sizeof(bInfo));

bInfo.hwndOwner     = m_hWnd;

bInfo.lpszTitle     = _T("请选择路径: ");    

bInfo.ulFlags       = BIF_RETURNONLYFSDIRS|BIF_EDITBOX;

bInfo.lpfn          = BrowseCallbackProc;

bInfo.lParam        = (LPARAM)strPath.GetBuffer(strPath.GetLength());

LPITEMIDLIST lpDlist; //用来保存返回信息的IDList

lpDlist = SHBrowseForFolder(&bInfo) ; //显示选择对话框

if(lpDlist != NULL) //用户按了确定按钮

{

SHGetPathFromIDList(lpDlist, chPath);//把项目标识列表转化成字符串

strPath = chPath; //将TCHAR类型的字符串转换为CString类型的字符串

m_save_path=strPath;

UpdateData(FALSE);

}

 

char name[255];

CString ip;

PHOSTENT hostinfo;

if(gethostname ( name, sizeof(name)) == 0)

{

//如果能够获取计算机主机信息的话,则获取本机IP地址

if ((hostinfo = gethostbyname(name)) != NULL)

{

   //获取本机IP地址

   LPCSTR ip=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);

   //输出IP地址

   AfxMessageBox(ip);

}

}

 

CTime time=CTime::GetCurrentTime();

    CString m_strTime=time.Format("%Y-%m-%d %H:%M:%S");

    CString m_strDefine=("3007-11-25 10:16:00");

    if(m_strTime>=m_strDefine)

    {

        AfxMessageBox("你落后了");

    }

    else

    {

        AfxMessageBox("你超前了");

    }

 

2.加载其它的资源图标

HICON   hIcon   =   LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));   

SetClassLong(m_hWnd,GCL_HICON,(DWORD)hIcon);

其中IDR_MAINFRAME为要加载的图标资源的ID号.

*除非特别说明,本文中所用控件变量类型为Control

一般控件可用/不可用

EnableWindow(TRUE);

EnableWindow(FALSE);

1、Static Text------------静态控件 --类CStatic

取值/赋值(变量类型为Control)

m_lbl.GetWindowText(string);

 

m_lbl.SetWindowText(string);

2、Edit Box---------------编辑控件 --类CEdit

取值/赋值

m_txt.GetWindowText(string);

 

m_txt.SetWindowText(string);

3、Check Box------------复选控件 --类CButton

(1)设置选中/未选中

m_chk.SetCheck(BST_CHECKED);  

m_chk.SetCheck(BST_UNCHECKED);

(2)判断是否选中

int nCur = m_chk.GetCheck();

nCur取值为 BST_CHECKED/BST_UNCHECKED。

 

4、Radio Box------------单选控件 --类 CButton

(1)默认选中第一项

m_radio.SetCheck(BST_CHECKED);

(2)选中组中任一项

CWnd::CheckRadioButton

Selects (adds a check mark to) a given radio button in a group and clears (removes a check mark from) all other radio buttons in the group.

void CheckRadioButton(int nIDFirstButton, int nIDLastButton, int nIDCheckButton);Parameters

nIDFirstButton

Specifies the integer identifier of the first radio button in the group.

nIDLastButton

Specifies the integer identifier of the last radio button in the group.

nIDCheckButton

Specifies the integer identifier of the radio button to be checked.

(3)判断哪一项被选中

CWnd::GetCheckedRadioButton

Retrieves the ID of the currently checked radio button in the specified group.

int GetCheckedRadioButton(int nIDFirstButton, int nIDLastButton);Parameters

nIDFirstButton

Specifies the integer identifier of the first radio button in the group.

nIDLastButton

Specifies the integer identifier of the last radio button in the group.

Return Value

ID of the checked radio button, or 0 if none is selected.

(4)控件变量类型为Value时,可通过给int型变量赋值0、1、2...选中第1、2、3...个选项。

int型变量默认值为-1,是在构造函数中赋的值。

当然也可通过判断int型变量的值,知道哪一个选项被选中。

5、Combo Box-----------组合框控件 --类CComboBox

(1)风格

Simple-象List Box一样显示数据

Dropdown-可以输入,也可以选择

Drop List-只能选择

(2)添加数据

a.属性对话框->Data->Enter listbox items,用Ctrl+Enter换行;

b.m_combo.AddString(string);

c.m_combo.InsertString(index,string);

(3)显示数据

设计页面,点击Combo Box Control右边的下拉箭头,显示的矩形框就是显示数据的区域。

(4)设置当前选项

m_combo.SetCurSel(项索引);

(5)获取当前选项

int nIndex = m_combo.GetCurSel();

CString str;

m_combo.GetLBText(nIndex, str);

注:Combo Box Control会自动排序,数据显示顺序可能与预期不同,建议添加数据时用InsertString(索引,值)。

6、List Box---------------列表框控件 --类CListBox

(1)插入项

m_list.AddString(string);

(2)设置当前选择项

m_list.SetCurSel(项索引);

(3)获取当前选择项

int nIndex = m_list.GetCurSel();

m_list.GetText(nIndex, string);

(4)删除一项

m_list.DeleteString(项索引);

(5)删除所有项

m_list.ResetContent();

(6)获取总项数

m_list.GetCount()

(7)List Box的选项前面加复选框(Check Box)

a.风格

声明时用类CCheckListBox代替CListBox,即CCheckListBox    m_list;而不是CListBox    m_list;

属性对话框->Styles->Owner draw设为Fixed

属性对话框->Styles->勾选Has strings

b.设置选择

void SetCheck(int nIndex, int nCheck);

Parameters

nIndex

Index of the item whose check box is to be set.

nCheck

State of the check box: 0 for clear, 1 for checked, and 2 for indeterminate.

c.获取选择

int GetCheck(int nIndex);

Parameters

nIndex

Index of the item whose check status is to be retrieved.

Return Value

Zero if the item is not checked, 1 if it is checked, and 2 if it is indeterminate.

 

7、List Control----------列表框扩展控件 --类CListCtrl

(1)样式:属性对话框框->Styles->Format有4,分别是Icon/Small Icon/List/Report;

(2)Report格式设置扩展风格

DWORD dwStyle = m_list.GetExtendedStyle();

dwStyle |= LVS_EX_FULLROWSELECT;        // 选中某行使整行高亮(只适用与report风格的listctrl)

dwStyle |= LVS_EX_GRIDLINES;            // 网格线(只适用与report风格的listctrl)

m_list.SetExtendedStyle(dwStyle);

(3)Report格式插入列

m_list.InsertColumn(1, "列一", LVCFMT_RIGHT, 150);

m_list.InsertColumn(2, "列二", LVCFMT_LEFT, 100);

m_list.InsertColumn(3, "列三", LVCFMT_LEFT, 100);

m_list.InsertColumn(4, "列四", LVCFMT_LEFT, 200);

m_list.InsertColumn(5, "ID", LVCFMT_CENTER, 0);

(4)Report格式插入一行数据

int nIndex = m_list.GetItemCount();

m_list.InsertItem(nIndex, s1);

m_list.SetItemText(nIndex, 1, s2);

m_list.SetItemText(nIndex, 2, s3);

m_list.SetItemText(nIndex, 3, s4);

m_list.SetItemText(nIndex, 4, s5);

(5)Report格式删除所有行

m_list.DeleteAllItems();

(6)Report格式获取某行某列数据

CString sID = m_list.GetItemText(行索引, 列索引);

(7)Report格式删除选择行,多选时可用循环。

POSITION pos = m_list.GetFirstSelectedItemPosition();

if (pos != NULL)

{

       int nIndex = m_list.GetNextSelectedItem(pos);

       m_list.DeleteItem(nIndex);

}

 

8、Date Time Picker----日期时间控件--类CDateTimeCtrl

(1)样式:属性对话框框->Styles->Format有3,分别是Short Date/Long Date/Time,分别显示短日期(2009-03-12)/长日期(2009年3月12日)/时间(20:08:06),日期格式默认有一向下箭头,时间格式默认有一Spin Control;

(2)可编程设置其显示格式,例如年4位,月、日、时、分、秒2位,

CString formatStr= _T("yyyy-MM-dd");

m_txtDate.SetFormat(formatStr);

formatStr= _T("HH:mm:ss");

m_txtTime.SetFormat(formatStr);

(3)取值赋给CString

m_txtDate.GetWindowText(sAddDate);

m_txtTime.GetWindowText(sAddTime);

9、Spin------------------旋转按钮控件 --类CSpinButtonCtrl

(1)与Edit控件关联

首先,排列控件的Tab键顺序,要让Spin Control的Tab Order紧跟着Edit Control(就是说,Spin Control的Tab Order是Edit Control的Tab Order加1);设置tab order 的方法是 Ctrl+d,然后用鼠标挨个点击,就是按TAB键时焦点在窗体上的移动顺序;

然后,Spin Control属性对话框中勾选Auto buddy和Set buddy integer。

(2)设置上下限

m_spin.SetRange(1, 60);

(3)设置当前值,可以不用给Edit控件赋值

m_spin.SetPos(3);

(4)获取当前值

int nCur = m_spin.GetPos();

10、Slider-----------------滑动条控件--类CSliderCtrl

(1)设置上下限、最小滑动值

m_slider.SetRange(5,100);

m_slider.SetTicFreq(1);

(2)设置/获取当前值

m_slider.SetPos(nCur);

int nCur = m_slider.GetPos();

(3)背景色:重写OnCtlColor(),虽然不知道Slider属于nCtlColor的哪一类,但试验表明似乎是属于CTLCOLOR_STATIC。

HBRUSH CDlgOptionVideo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 

    // TODO: Change any attributes of the DC here

    // 设置透明背景模式

    pDC->SetBkMode(TRANSPARENT);

 

    // TODO: Return a different brush if the default is not desired

    switch(nCtlColor)

    {

        // 设置背景刷子为空

        case CTLCOLOR_STATIC:    // 静态控件

            if(pWnd->GetDlgCtrlID() == IDC_SLIDER_TIME)

                 return ::CreateSolidBrush(RGB(203, 228, 253));

        case CTLCOLOR_DLG:        // 对话框

            return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);

        default:

            return hbr;

    }

}

 

源文档 <http://hi.baidu.com/honey00/blog/item/b5023eea46411cded439c931.html>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值