MFC-核心类库-CWnd的成员函数介绍(二)

1、CWnd :: FromHandle

CWnd在给定窗口句柄时,返回指向对象的指针。如果CWnd对象未附加到句柄,CWnd则会创建并附加临时对象。

static CWnd * PASCAL FromHandle(HWND hWnd);

2、CWnd::Attach

当我们使用MFC创建一个窗口时,是分两步进行的:
第一步,new一个CWnd对象,这一步是创建C++对象,但是其中的HWND还是非法的,因为对应的Windows对象还没有被创建出来;

第二步,调用CWnd的成员函数Create创建真正的Windows对象,同时,把先前创建的MFC的CWnd对象的HWND成员指向该窗口,这样才算创建完毕一个窗口。

现在你已经有了一个有效窗口句柄(指向(并标识)了一个具体的Windows window),那么你想把这个窗口和一个CWnd对象关联起来怎么办?很简单,用Attach:其实就是让一个C++对象——CWnd对象的HWND成员等于这个窗口句柄。这就是Attach主要完成的任务。 

HWND hWnd = ::GetDlgItem(m_hWnd, IDC_EDIT1);//得到IDC_EDIT1的句柄
m_edit.Attach(hWnd);//将这个句柄附加到m_edit的CWnd对象上。

3、CWnd::Detach

分离 CWnd 对象的一个Windows句柄并返回处理。经常与Attach配合使用,Attach之后如果不使用Detach,程序结束时会报错。

void CTestCWndDlg::OnDestroy()
{
	CDialog::OnDestroy();

	m_edit.Detach();
}

4、CWnd :: GetStyle、CWnd :: GetExStyle、CWnd::ModifyStyle、CWnd::ModifyExStyle

得到当前窗口的样式、扩展样式。修改当前窗口的样式、扩展样式。

DWORD GetStyle()const;
BOOL ModifyStyle(
   DWORD dwRemove,//指定在样式修改期间要删除的窗口样式。
   DWORD dwAdd,//指定在样式修改期间添加的窗口样式。
   UINT nFlags = 0 
);

5、CListCtrl的创建与添加行与列

CListCtrl::Create

virtual BOOL Create(
   DWORD dwStyle,//指定列表控件的样式。
   const RECT& rect,//指定列表控件的大小和位置。
   CWnd* pParentWnd,//指定列表控件的父窗口
   UINT nID //指定列表控件的ID.
);

CListCtrl::CreateEx

 virtual BOOL CreateEx(
   DWORD dwExStyle,//指定创建的控件的扩展样式。
   DWORD dwStyle,
   const RECT& rect,
   CWnd* pParentWnd,
   UINT nID 
);

CListCtrl::InsertColumn

插入新的一列

int InsertColumn(
   int nCol,
   const LVCOLUMN* pColumn 
);
int InsertColumn(
   int nCol,//列号
   LPCTSTR lpszColumnHeading,//标题
   int nFormat = LVCFMT_LEFT,//标题显示形式
   int nWidth = -1,//列的宽度
   int nSubItem = -1 
);

CListCtrl::InsertItem

插入一行到视图中

int InsertItem(const LVITEM* pItem);

 
int InsertItem(
    int nItem,  //要插入项的索引。
    LPCTSTR lpszItem);//包含项的标签的字符串的地址

 
int InsertItem(
    int nItem,  
    LPCTSTR lpszItem,  
    int nImage);//项的图像的索引

 
int InsertItem(
    UINT nMask,  
    int nItem,  
    LPCTSTR lpszItem,  
    UINT nState,  
    UINT nStateMask,  
    int nImage,  
    LPARAM lParam);

CListCtrl::SetItemText

更改列表视图项或子项的文本。

BOOL SetItemText(
    int nItem,  //要设置其文本的项的索引。行(0,1,2,)
    int nSubItem,  //要设置的项目标签子项。列(1,2,3,)
    LPCTSTR lpszText);//包含新的项文本的字符串指针。

例如:

       if(!m_list)
	{
		m_list.CreateEx(WS_EX_CLIENTEDGE,WS_VISIBLE|WS_CHILD|LVS_REPORT,CRect(10,80,300,300),this,1234);
		m_list.InsertColumn(0,_T("name"),LVCFMT_CENTER,100);//文字显示居中
		m_list.InsertColumn(1,_T("age"),LVCFMT_CENTER,100);
		m_list.InsertColumn(2,_T("sex"),LVCFMT_CENTER,100);

		m_list.InsertItem (0,_T("彭涛"));//插入新的一行,设置第一个字段的值,也就是第一列
		m_list.SetItemText(0,1,_T("18"));//其他字段必须由SetItemText进行设置
		m_list.SetItemText(0,2,_T("男"));//其他字段必须由SetItemText进行设置
		
		m_list.InsertItem (1,_T("谭赟"));
		m_list.SetItemText(1,1,_T("18"));//其他字段必须由SetItemText进行设置
		m_list.SetItemText(1,2,_T("女"));//其他字段必须由SetItemText进行设置

		m_list.InsertItem (2,_T("张三"));
		m_list.SetItemText(2,1,_T("18"));//其他字段必须由SetItemText进行设置
		m_list.SetItemText(2,2,_T("男"));//其他字段必须由SetItemText进行设置

	}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC开发过程序所需的ModifyStyle(needDelStyle,needAddStyle,SWP_FRAMECHANGED); Sytel: WS_BORDER Creates a window that has a border. WS_CAPTION Creates a window that has a title bar (implies the WS_BORDER style). Cannot be used with the WS_DLGFRAME style. WS_CHILD Creates a child window. Cannot be used with the WS_POPUP style. WS_CHILDWINDOW Same as the WS_CHILD style. WS_CLIPCHILDREN Excludes the area occupied by child windows when you draw within the parent window. Used when you create the parent window. WS_CLIPSIBLINGS Clips child windows relative to each other; that is, when a particular child window receives a paint message, the WS_CLIPSIBLINGS style clips all other overlapped child windows out of the region of the child window to be updated. (If WS_CLIPSIBLINGS is not given and child windows overlap, when you draw within the client area of a child window, it is possible to draw within the client area of a neighboring child window.) For use with the WS_CHILD style only. WS_DISABLED Creates a window that is initially disabled. WS_DLGFRAME Creates a window with a double border but no title. WS_GROUP Specifies the first control of a group of controls in which the user can move from one control to the next with the arrow keys. All controls defined with the WS_GROUP style FALSE after the first control belong to the same group. The next control with the WS_GROUP style starts the next group (that is, one group ends where the next begins). WS_HSCROLL Creates a window that has a horizontal scroll bar. WS_ICONIC Creates a window that is initially minimized. Same as the WS_MINIMIZE style. WS_MAXIMIZE Creates a window of maximum size. WS_MAXIMIZEBOX Creates a window that has a Maximize button. WS_MINIMIZE Creates a window that is initially minimized. For use with the WS_OVERLAPPED style only. WS_MINIMIZEBOX Creates a window that has a Minimize button. WS_OVERLAPPED Creates an overlapped window. An overlapped window usually has a caption a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值