Visual C++ and MFC Fundamentals programming phần 8 pot

90 500 0
Visual C++ and MFC Fundamentals programming phần 8 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 16: Text -Based Controls Visual C++ and MFC Fundamentals 496 © FunctionX, Inc. 3. Return to MSVC 16.1.3 Static Labels Another type of label you can add to a form or dialog box consists of clicking the Static Text button from the Controls toolbar and clicking on the host. The Static Text control is based on the CStatic class. A user cannot directly change any aspect of a label. For this reason, you will usually not be concerned with a label's IDentifier. In fact, as we will see in this lesson, all controls that are static type have an identifier called IDC_STATIC. If, for any reason, you want to refer to a label in your code, you must first change its identifier from IDC_STATIC to something else. As stated already, the most important aspect of a label is the text it displays. This is the Caption property. If the label is used to identify another control, you can help the user access that control using an indicator called an access key. An access key is a underlined letter on the label so that, when clicked, it gives access to the accompanying control. To create an access key, choose a letter on the label and precede it with the ampersand character “&”. Form example L&etter would produce Letter. When there are many access keys on a form or dialog box, you should make sure that no two access keys are the same. That is, you should not use the same letter on various labels, although this is allowed. Because you can forget to follow this rule, after creating the access keys, you can ask Visual Studio to check for access key duplicates. To do this, right-click the form or dialog box and click Check Mnemonics: Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls © FunctionX, Inc. 497 If there are duplicate access keys, you would receive a message accordingly: If you possible, you should correct by changing one of the access keys without using one that exists already. To use the access keys, the user presses Alt and the letter that corresponds to the access key. The text of the caption follows a line alignment that can assume one of three values: Left, Center, or Right. By default, text of a label is aligned to the left. At design time, to change it, select the desired value using the Align Text combo box from the Styles property page of the Properties window. To apply a fancy appearance to a label, you can use the Extended Styles of the Properties window. Chapter 16: Text -Based Controls Visual C++ and MFC Fundamentals 498 © FunctionX, Inc. Practical Learning: Using Static Labels 1. From the Dialog folder of the Resource View, double-click the IDD_CLARKSVILLEICESCREAM1_FORM form to display it 2. On the Controls toolbox, click Static Text and click on the form. 3. On the Properties window, click Caption and type &Date Hired: 4. Using the Static Text control, complete the form as follows: 5. Test the application and return to MSVC 16.2 Edit Controls 16.2.1 Introduction An edit box is a Windows control used to display text or get it from the user. To provide its functionality, the control displays a box, whose background is white by default, surrounded by a black line. If the box is empty, the user may be expected to enter some letters, numbers, or other characters into it. If the box contains some text, the user should be able to edit it. Another edit box may be used to present text to the user without his or her being able to change it. The text that displays in an edit box is referred to as its value. Like most other controls, the role of an edit box is not obvious at first glance. That is why it should (always) be accompanied by a label that defines its purpose. From the user’s standpoint, an edit box is named after the label closest to it. Such a label is usually positioned to the left or the top side of the edit box. From the programmer’s point of view, an edit box is a place holder used for various things. For example, you can show or hide it as you see fit. To create an edit box, click the Edit Box button from the Controls toolbox and click the desired area on a form or a dialog box. Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls © FunctionX, Inc. 499 Practical Learning: Creating Edit Boxes 1. Display the form 2. On the Controls toolbox, click the Edit Control button and click on the right side of the Date Hired label 3. Using the Edit Control and additional labels, complete the form as follows: 4. On the main menu, click Layout or Format and click Tab Order 5. Click the Date Hired edit box to place the number 1 in it and arrange the rest of the tab sequence as follows: 6. Test the application Chapter 16: Text -Based Controls Visual C++ and MFC Fundamentals 500 © FunctionX, Inc. 7. Return to MSVC 16.2.2 Edit Control Characteristics An edit box is a control based on the CEdit class. Therefore, to programmatically create an edit box, declare a variable of CEdit type using its (default) constructor. To define the characteristics of this control, you can call the CEdit::Create() method. Its syntax is: BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID); The content of an edit box, that is, its value, is a string. It could be considered as null- terminated constant (LPCTSTR) or a CString value. Like every other control added during design, you should give some attention to the identifier of an edit box. The first edit box placed on a form or dialog box receives an identifier of IDC_EDIT1. The second would be identified as IDC_EDIT2, etc. It is highly recommended that you give a meaningful and friendly identifier to each control. For example, if an edit box is used to receive or display an address, you can set its identifier to IDC_ADDRESS. If an edit box is used to enter an email address, you can change its ID to IDC_EMAILADDRESS or IDC_EMAIL_ADDRESS. An identifier should have a maximum of 30 characters. Notice that the ID contains C, which stands for Control. If you plan to access an edit box in your code, you should create a variable for it. The variable can be a CEdit or a CString object. The difference is that, if you want to access the control as an MFC class, you should create the variable as a CEdit object. If you are more interested with the value (as a string) of the control, you should declare the variable as CString. Fortunately, you can add two variables for the same control. Probably the most important characteristic of an edit control for both the programmer and the user is the text it is displaying or that it can display. When you add an Edit control, Visual C++ 6 displays Edit and Visual C++ 7 displays Sample edit box. This text is not part of the control and does not show when the control comes up. It is only an indicator. Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls © FunctionX, Inc. 501 If the user wants to change the text of an edit box and if the control allows changes, he or she must first click in the control, which places a caret in the edit box. The caret is a blinking I beam that serves as a reminder that lets the user know what edit control would receive any change made. This means that if the user starts typing, the edit control in which the caret is positioned would display a change in its value. The edit box that has the caret is said to have focus. As mentioned already, the user gives focus to an edit box by clicking it. Remember that if a label that accompanies an edit box has an access key, the user can also give focus to the edit control by pressing the access key. Also remember that you can programmatically give focus to an edit control by calling the SetFocus() method. At any time, you can find out what text is in an edit control and there are various techniques you can use. We saw already how to get a handle to a control by calling the CWnd::GetDlgItem() method. After calling this method, you can use the CWnd::GetWindowText() method to find out what text an edit box holds. Here is an example: void CEditBoxDlg::CreateName() { CEdit *edtFirstName, *edtLastName, *edtFullName; CString FirstName, LastName; char FullName[40]; edtFirstName = reinterpret_cast<CEdit *>(GetDlgItem(IDC_FIRST_NAME)); edtLastName = reinterpret_cast<CEdit *>(GetDlgItem(IDC_LAST_NAME)); edtFullName = reinterpret_cast<CEdit *>(GetDlgItem(IDC_FULL_NAME)); edtFirstName->GetWindowText(FirstName); edtLastName ->GetWindowText(LastName); sprintf(FullName, "%s %s", FirstName, LastName); edtFullName->SetWindowText(FullName); } void CEditBoxDlg::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CreateName(); CDialog::OnLButtonDblClk(nFlags, point); } Another technique you can use to get the text of an edit control consists of calling the CWnd::GetDlgItemText() method. It is provided in two syntaxes as follows: int GetDlgItemText( int nID, LPTSTR lpStr, int nMaxCount ) const; int GetDlgItemText( int nID, CString& rString ) const; Chapter 16: Text -Based Controls Visual C++ and MFC Fundamentals 502 © FunctionX, Inc. The nID argument is the identifier of the control whose text you want to retrieve. The lpStr or the rString is the returned value of the text in the edit box. It must be provided as a string variable. If you are using a null-terminated variable, pass a third argument as nMaxCount that specifies the maximum length of the string. Here is an example: void CFormView1View::OnButton1() { // TODO: Add your control notification handler code here CString Edit1, Edit2, Result; GetDlgItemText(IDC_EDIT1, Edit1); GetDlgItemText(IDC_EDIT2, Edit2); Result = Edit1 + " " + Edit2; SetDlgItemText(IDC_EDIT3, Result); } As mentioned already, an edit box can be used to request information from the user. If you want to prevent the user from changing the value of an edit box, you can make it read-only. This is taken care of by setting the Read-Only property to True or checking its check box. To do this programmatically, call the CEdit::SetReadOnly() method. If an edit box is not “read-only”, that is, if it allows the user to change its value, the user must first give it focus. When an edit box has focus, it displays a blinking caret. By default, the carret is an I beam. If you want to use a different caret, you have various options. You can change the caret from an I beam to a wider or taller gray caret by calling the CWnd::CreateGrayCaret() method. Its syntax is: void CreateGrayCaret( int nWidth, int nHeight ); This method allows you to specify a width and a height for a gray blinking caret. After creating the caret, to display it, call the CWnd::ShowCaret() method. Its syntax is: void ShowCaret( ); Here is an example: BOOL CSolidCaretDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_Username.CreateGrayCaret(5, 15); m_Username.ShowCaret(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls © FunctionX, Inc. 503 The above caret appears gray. If you want the caret to be completely black, you can call the CWnd::CreateSolidCaret() method. Its syntax is: void CreateSolidCaret( int nWidth, int nHeight ); This method creates a rectangular caret of nWidth x nHeight dimensions. Here is an example: BOOL CSolidCaretDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_Username.CreateSolidCaret(5, 15); m_Username.ShowCaret(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } To provide a better designed caret, you can call the CWnd::CreateCaret() method. Its syntax is: void CreateCaret(CBitmap* pBitmap); Before calling this method, you can first design a bitmap, load it, and then pass it as the pBitmap argument. After initializing and loading the bitmap, to display it in the edit box, call the CWnd::ShowCaret() method. Here is an example: BOOL CDialogCaret::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CEdit *edtBookTitle; CBitmap *pBitmap = new CBitmap; edtBookTitle = reinterpret_cast<CEdit *>(GetDlgItem(IDC_BOOK_TITLE)); pBitmap->LoadBitmap(IDB_BMP_CARET); edtBookTitle->CreateCaret(pBitmap); edtBookTitle->ShowCaret(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } Chapter 16: Text -Based Controls Visual C++ and MFC Fundamentals 504 © FunctionX, Inc. If an edit box is editable and the user starts typing in it, the new characters would display. As the user is typing, the caret moves from left to right (for US English). The user can also move the caret back and forth using the Backspace, Delete, Home, End, or the arrow keys. At any time you can find out the current position of the caret by calling the CWnd::GetCaretPos() method. Its syntax is: static CPoint PASCAL GetCaretPos( ); This method retrieves the left (x) and bottom (y) coordinates of the caret in the control that called it. These coordinates represent the member variables of a CPoint that this method returns. The measures are relative to the control and not the control’s parent. Here is an example: void CDialogCaret::CaretPosition(void) { CEdit *edtBookTitle; CStatic *stcCaretPos; CPoint CrtPos; char Msg[40]; edtBookTitle = reinterpret_cast<CEdit *>(GetDlgItem(IDC_BOOK_TITLE)); stcCaretPos = reinterpret_cast<CStatic *>(GetDlgItem(IDC_CARET_POS)); CrtPos = edtBookTitle->GetCaretPos(); sprintf(Msg, "Caret Position: (%d, %d)", CrtPos.x, CrtPos.y); stcCaretPos->SetWindowText(Msg); } void CDialogCaret::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CaretPosition(); CDialog::OnLButtonDown(nFlags, point); } If want to hide the characters that display in an edit box, you can set the Password property to True. To do this programmatically, add the ES_PASSWORD style to the edit control. This style makes the edit control displays each character, or changes each one of its characters into an asterisk. If you prefer another symbol for the password style, call the CEdit::SetPassword() method. Its syntax is: void SetPasswordChar(TCHAR ch); This method takes as argument the character that you want to use. Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls © FunctionX, Inc. 505 If an edit box is configured to display an asterisk character and you want to find out what that character is, call the CEdit::GetPasswordChar(). Its syntax is: TCHAR GetPasswordChar() const; This method takes no argument but returns the symbol used to mask the characters of a password-configured edit box. By default, an edit box is configure to display or receive text of any alphabetic and non- alphabetic character. The alphabetical letters are received by their case and kept like that, uppercase and lowercase. If you want to convert an edit box' characters to uppercase, set the Uppercase property to True or programmatically add the ES_UPPERCASE style. In the same way, to convert the characters to lowercase, either at design time set the Lowercase property to True or programmatically add the ES_LOWERCASE style. The non-alphabetical characters are not treated by their case. If you want, you can configure the edit box to allow only numeric characters. This is done by setting the Number property to True. By default, the characters entered in an edit box start their positioning to the left, which is the default for regular text. You can align its value to the center or the right by selecting the desired value from the Align Text combo box. Any of these three alignment modes can also be set programmatically by adding either the ES_LEFT, the ES_CENTER, or the ES_RIGHT style. As mentioned above, the value of an edit box is of high interest to you and your users. If you want to retrieve the value of an edit box, call the CWnd::GetWindowText() method. If you want to display or change the text of an edit box, call the CWnd::SetWindowText() method. As we have seen in the past, SetWindowText() takes a constant pointer to null-terminated string (LPCTSTR) and displays its value in the edit. This method is very convenient if you had add a CEdit variable to your edit control. If you have added the variable as a CString, you can use the CString::Format() method to change or format the value to display in an edit box. Practical Learning: Configuring Edit Boxes 1. The Clarksville Ice Scream1 application should still be opened Using the Add Resource dialog box, add a new dialog: [...]... Controls Return to MSVC 521 Chapter 17: Track-Based Controls 522 Visual C++ and MFC Fundamentals © FunctionX, Inc Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls Chapter 17: Track-Based Controls ? Spin Buttons ? Updown Controls ? © FunctionX, Inc Slider Controls 523 Chapter 17: Track-Based Controls Visual C++ and MFC Fundamentals 17.1 Spin Button 17.1.1 Overview A spin button is a Windows... Suggested Username edit box and click Add Event Handler 3 Add an event handler for the EN_MAXTEXT message and click either Edit Code or Finish 4 Implement the event as follows: void CAccountDlg::OnEnMaxtextUsername() { // TODO: Add your control notification handler code here MessageBox("A username must have a maximum of 5 characters"); } 512 © FunctionX, Inc Visual C++ and MFC Fundamentals 5 Chapter 17:... 506 Visual C++ and MFC Fundamentals ID IDD_DLG_ACCOUNT Caption Employee Account Setup &Full Name IDC_FULL_NAME Other Properties Read-Only = True &Suggested Username: IDC_USERNAME Lowercase = True &Password IDC_PASSWORD Password = True &Confirm Password IDC_CONF_PASSWORD IDC_BTN_VALIDATE Password = True &Validate Create a class for the dialog box and name it CAccountDlg © FunctionX, Inc Visual C++ and. .. event handler for each button and implement the events as follows: void CRicher1Dlg::OnBnClickedBtnBold() { // TODO: Add your control notification handler code here CHARFORMAT Cfm; m_Richer.GetSelectionCharFormat(Cfm); Cfm.cbSize = sizeof(CHARFORMAT); Cfm.dwMask = CFM_BOLD; Cfm.dwEffects ^= CFE_BOLD; m_Richer.SetSelectionCharFormat(Cfm); m_Richer.SetFocus(); 5 18 © FunctionX, Inc Visual C++ and MFC Fundamentals. .. Press and hold Ctrl Then click each one of the other boxes to select them Then release Ctrl 21 On the Properties window, set the Align Text property to Right 22 In the same way, set the Align Text property of the edit boxes of the IDD_ G3D and the IDD_QUADRILATERAL dialog boxes to Right 5 08 © FunctionX, Inc Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls 23 Test the application and. .. to IDC_EDIT_RED, IDC_SPIN_RED, IDC_EDIT_GREEN, IDC_SPIN_GREEN, IDC_EDIT_BLUE, and IDC_SPIN_BLUE © FunctionX, Inc and click under the picture and click to the right side of the 527 Chapter 17: Track-Based Controls Visual C++ and MFC Fundamentals 17.1.3 The Spin Button Properties By default, a spin button appears with an up and a down pointing arrows If you want the arrows to be horizontally directed,... CFE_UNDERLINE, and CFE_PROTECTED These effects can be combined as needed using the bitwise OR operator For example, you can combine CFE_BOLD and CFE_ITALIC as CFE_BOLD | CFE_ITALIC to have text that is both in bold and italic The yHeight variable is used to set the new height of the text The yOffset variable is used to create a superscript or subscript effect 516 © FunctionX, Inc Visual C++ and MFC Fundamentals. .. notification handler code here PARAFORMAT Pfm; m_Richer.GetParaFormat(Pfm); Pfm.cbSize = sizeof(PARAFORMAT); Pfm.dwMask = PFM_NUMBERING; Pfm.wNumbering ^= PFN_BULLET; } 3 520 m_Richer.SetParaFormat(Pfm); m_Richer.SetFocus(); Test the application © FunctionX, Inc Visual C++ and MFC Fundamentals 4 © FunctionX, Inc Chapter 17: Track-Based Controls Return to MSVC 521 Chapter 17: Track-Based Controls 522 Visual C++. .. 80 , 35), this, 0x1 28) ; When the buddy window displays the values of the spin button and when the value gets over 999, if you want the number to be separated by a comma, set the No Thousands property to True To apply this property with code, add the UDS_NOTHOUSANDS style If you do not want the thousand sections to be separated by comma, set the No Thousands property to False or omit the UDS_NOTHOUSANDS... you create a spin button and specify its range of values from 3 to 22 with an incremental of 5 When the control is incremented to 20 and the user clicks the up arrow button or presses the up arrow key, you can decide that the incrementing should stop at 20 or wrap to 22, although 22 is not a valid incremental value for this scenario The 5 28 © FunctionX, Inc Visual C++ and MFC Fundamentals Chapter 17: . BN_CLICKED Event Handler to the button associated with the view class and implement it as follows: Chapter 16: Text -Based Controls Visual C++ and MFC Fundamentals 5 08 © FunctionX, Inc. . access keys, you can ask Visual Studio to check for access key duplicates. To do this, right-click the form or dialog box and click Check Mnemonics: Visual C++ and MFC Fundamentals Chapter 17:. the IDD_G3D and the IDD_QUADRILATERAL dialog boxes to Right Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls © FunctionX, Inc. 509 23. Test the application and return

Ngày đăng: 06/08/2014, 17:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan