控件的学习

来源:互联网 发布:数据采集供应商 编辑:程序博客网 时间:2024/06/06 02:11

1 编辑控件(CEDIT)

 ATTENTION:创建一个编辑框,两步就可以了,首先构造一个CEdit的对象,然后调用CEdit::Create函数来创建,当然可以直接在对话框里面直接拖,不过动态地还是比较方便的

在初始化函数里面添加即可==

BOOL C控件使用Dlg::OnInitDialog(){CDialog::OnInitDialog();// 将“关于...”菜单项添加到系统菜单中。// IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动//  执行此操作SetIcon(m_hIcon, TRUE);// 设置大图标SetIcon(m_hIcon, FALSE);// 设置小图标// TODO: 在此添加额外的初始化代码   //增加一个EDIT控件CEdit *m_edit_new=new CEdit();m_edit_new->Create(WS_CHILD|WS_VISIBLE|WS_TABSTOP| WS_BORDER, CRect(50,50,200,70),this,1);
        CString str("请输入文本");
m_edit_new->SetWindowTextW(str);return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE}

注意:刚开始的时候用CEdit类创建一个对象,如果是CEdit m_edit_new那么,在初始化函数结束的时候就会把与这个对象相关联的资源丢掉,那么自然你看不到产生的控件了==

(2)

改变控件的颜色

要改变控件的颜色,接触到一个消息WM_CTLCOLOR消息,该消息是在对话框控件要被绘制的时候才会发送,可以看到WM_CTLCOLOR消息的响应函数

HBRUSH C控件使用Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor){HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);// TODO:  在此更改 DC 的任何属性// TODO:  如果默认的不是所需画笔,则返回另一个画笔return hbr;}

可以知道该函数返回一个画刷,可以用CBrush构造一个新的画刷,然后返回

可以看到结果如下(对话框和控件的颜色都已经改变了,仅有两个按钮颜色未变,后面将提到)



OnCtrlColor函数有三个参数,第一个参数是当前要绘制空间的CDC句柄,可以通过它设置背景色(SetBkColor),设置文本颜色(SetTextColor),设置背景模式SetBkMode参数可以设置为TRANSPARENT,这样背景色将不会被改变,而不会出现全部是白色

pDC->SetTextColor(RGB(250,0,0));

pDC->SetBkMode(TRANSPARENT);


第三个参数是当前控件的类型

CTLCOLOR_BTN 按钮类型 CTLCOLOR_EDIT 编辑控件 

通过这个参数可以识别一类控件,但是如果你想要摄者某一个控件的话,那么就要用到第二个参数

这里有一个新的函数就是GetDlgCtrlID(),它将会返回一个ID值,通过判断这个ID值就可以知道是否为所需控件

HBRUSH C控件使用Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor){HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);// TODO:  在此更改 DC 的任何属性// TODO:  如果默认的不是所需画笔,则返回另一个画笔if(pWnd->GetDlgCtrlID()==1){pDC->SetTextColor(RGB(250,0,0));pDC->SetBkMode(TRANSPARENT);return m_brush;}else{return hbr;}}
效果如下图:




3,此次学系完全是阅读MSDN而学的

这次学习的是如何响应编辑控件的消息

If you want to handle Windows notification messages sent by an edit control to its parent (usually a class derived from CDialog), add a message-map entry and message-handler member function to the parent class for each message.Each message-map entry takes the following form:ON_Notification( id, memberFxn )where id specifies the child window ID of the edit control sending the notification, and memberFxn is the name of the parent member function you have written to handle the notification.The parent's function prototype is as follows:afx_msg void memberFxn( );Following is a list of potential message-map entries and a description of the cases in which they would be sent to the parent:ON_EN_CHANGE   The user has taken an action that may have altered text in an edit control. Unlike the EN_UPDATE notification message, this notification message is sent after Windows updates the display.ON_EN_ERRSPACE   The edit control cannot allocate enough memory to meet a specific request.ON_EN_HSCROLL   The user clicks an edit control's horizontal scroll bar. The parent window is notified before the screen is updated.ON_EN_KILLFOCUS   The edit control loses the input focus.ON_EN_MAXTEXT   The current insertion has exceeded the specified number of characters for the edit control and has been truncated. Also sent when an edit control does not have the ES_AUTOHSCROLL style and the number of characters to be inserted would exceed the width of the edit control. Also sent when an edit control does not have the ES_AUTOVSCROLL style and the total number of lines resulting from a text insertion would exceed the height of the edit control.ON_EN_SETFOCUS   Sent when an edit control receives the input focus.ON_EN_UPDATE   The edit control is about to display altered text. Sent after the control has formatted the text but before it screens the text so that the window size can be altered, if necessary.ON_EN_VSCROLL   The user clicks an edit control's vertical scroll bar. The parent window is notified before the screen is updated.

这是MSDN对CEDIT控件接受消息的解释

我们接受控件消息,是谁去接收?怎么去接受呢?

事实很简单,第一个加消息映射,你可以选择以上六种消息之一加入消息映射,注意不要加逗号,其次就是添加消息响应函数,仅此两步,是不是很简单啊?

呵呵,进入主题............

第一步加消息映射

BEGIN_MESSAGE_MAP(C控件使用Dlg, CDialog)  
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_STN_CLICKED(IDC_STATIC1, &C控件使用Dlg::OnStnClickedStatic1)
ON_BN_CLICKED(IDC_BUTTON1, &C控件使用Dlg::OnBnClickedButton1)
ON_WM_CTLCOLOR()
ON_EN_CHANGE(1,OnEditNotify)//这里我要响应的控件ID是1,这个是看具体情况而填
END_MESSAGE_MAP()

 //注意这里不要加错位置了,加到BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)的消息映射,那么将会无法识别OnEditNotify函数,

第二步就是写响应函数了,原型如下(在父类中添加)

afx_msg void memberFxn( ); 注意加上前面的afx_msg 不过不佳也能响应

我自己写的响应函数是这样的

afx_msg void OnEditNotify(void)
{
MessageBox(CString("文本已修改"));
}

下面上图:

我响应的消息是ON_EN_CHANGE,文本框要修改的时候,就会发送此消息

在第一启动程序的时候,也会发送此消息==注意



第四点==

这要实现一点小功能,就是编辑框原本显示着一行字母,譬如“请输入文本”,当其获得输入焦点的时候那么将会全部显示为空

这就是响应ON_EN_SETFOCUS 消息

通过GetDlgItem获得控件指针,然后调用SetWindowTextW,将所在内容设为空就可以了

这个看起来很高级哟==

嘿嘿,小点子就是了

不过以后注意ID值应该从哪里用起,这次就是因为IDOK的值就是1,所以应该慎重取值

可以看到自己添加的ID控件的值都是从1000开始的,这个找到一个控件ID,然后转到定义resource.h里面就可以知道资源ID的数值了

睡觉了==================================================