A simple tutorial on CMFCPropertyGridCtrl

Source: Internet
Author: User
Tags rows count


I wrote a blog post from the beginning to the day, and then to the week, and now it has changed to menstruation ..

In other words, a recent requirement is to combine some text with the drop-down box and correspond to each other. If we pile up several static and comboBox tiles on the dialog box, it will seem monotonous, besides, text alignment is troublesome and requires typographical layout. Find the CMFCPropertyGridCtrl control. I found some tutorials and found that this control is quite simple to use. The key point is that we need to understand the hierarchy to avoid confusion and who is the subitem, who are members and so on.

The previous example shows the old practice.

Use vs2010 to create a dialog box-based MFC project, drag a CMFCPropertyGridCtrl, and adjust the size. Note: The static creation method is used first. Most of the required properties can be edited on the dialog box editing interface .) Then, change the ID of the control to IDC_MFCPROPERTYGRID_TEST, and use ClassWizard to add a variable m_propertyGrid to the control.

Next, modify some properties of the control.

In fact, this is not a CMFCPropertyGridCtrl control. Border's basic things will not be mentioned.

"Description Rows Count" indicates the number of Rows in the Description section below.

"Enable Description Area" indicates whether the following Description function is enabled.

"Enable Header" indicates whether to start the Header.

"Mark Modified Properties" indicates whether the change item is highlighted.

You can set it as needed. Use the default settings. Compile and run it first, which is relatively simple.

Okay. Now we need to add the item.

Add the following code to OnInitDialog. I will explain it in one line.

 

 
  1. CMFCPropertyGridProperty * pProp1 = new CMFCPropertyGridProperty (
  2. _ T ("is tianchao suitable for survival? "),
  3. _ T ("BKJIA does not talk about politics "),
  4. _ T ("this is the description part "));
  5.  
  6. M_propertyGrid.AddProperty (pProp1 );

The first line is to create a new project, which is also the most common project, CMFCPropertyGridProperty. Similar to such projects, there are CMFCPropertyGridColorProperty, CMFCPropertyGridFontProperty, and CMFCPropertyGridFileProperty. You can try them all at the same level. The three parameters passed in when the constructor is called are the entry name, default options, and description text. Run the command to understand the details.

Hungry, swollen. However, you only need to set it. Here we have to mention that Microsoft seems very fond of setting the width of the first column to "only accommodate the width of the next ordinary 5th", not only CMFCPropertyGrid, but also CListCtrl, it requires special actions to be adjusted. Add the following sentence before this Code:

 

 
  1. HDITEM item; 
  2. item.cxy=120; 
  3. item.mask=HDI_WIDTH; 
  4. m_propertyGrid.GetHeaderCtrl().SetItem(0, new HDITEM(item)); 

It will be nice to run it again.

Well, next let's take a look at the value on the right side, which can also be replaced with Shenma.

Like in the "properties" column provided in vs2010, CMFCPropertyGridCtrl should also support the drop-down menu. Well, let's add a drop-down menu. Modify the Code:

 

 
  1. CMFCPropertyGridProperty * pProp2 = new CMFCPropertyGridProperty (
  2. _ T ("Am I a handsome guy? "),
  3. _ T ("view options "),
  4. _ T (""));
  5. PProp2-> AddOption (_ T ("yes "));
  6. PProp2-> AddOption (_ T ("certainly "));
  7. PProp2-> AddOption (_ T ("definitely "));
  8. PProp2-> AllowEdit (FALSE); // The option cannot be edited.
  9.  
  10. M_propertyGrid.AddProperty (pProp2 );

Then, the drop-down box appears as expected.

The following are three other projects at the same level:

 

 
  1. CMFCPropertyGridColorProperty * pProp3 = new CMFCPropertyGridColorProperty (
  2. _ T ("color"), RGB (0,111,200 ));
  3. M_propertyGrid.AddProperty (pProp3 );
  4.  
  5. CMFCPropertyGridFileProperty * pProp4 = new CMFCPropertyGridFileProperty (
  6. _ T ("Open File"), TRUE, _ T ("D: \ test.txt "));
  7. M_propertyGrid.AddProperty (pProp4 );
  8.  
  9. LOGFONT font = {NULL };
  10. Fccmpropertygridfontproperty * pProp5 = new CMFCPropertyGridFontProperty (
  11. _ T ("select font"), font );
  12. M_propertyGrid.AddProperty (pProp5 );

Note: Each type of project has two or three overload functions. You can mine them based on your needs. I will not go into details here.

The running effect is as follows:

Is it a little unscientific to pile these different types of things together? This introduces the next concept: grouping. Return to the first figure. In vs2010, the "attributes" column is divided into three groups: Apperance, Behavior, and Misc. It looks much clearer. We can also.

Okay. Let's rebuild our code:

 

 
  1. CMFCPropertyGridProperty * group1 = new CMFCPropertyGridProperty (_ T ("group 1 "));
  2. CMFCPropertyGridProperty * group2 = new CMFCPropertyGridProperty (_ T ("group 2 "));
  3.  
  4. Group1-> AddSubItem (pProp1 );
  5. Group1-> AddSubItem (pProp2 );
  6. Group2-> AddSubItem (pProp3 );
  7. Group2-> AddSubItem (pProp4 );
  8. Group2-> AddSubItem (pProp5 );
  9.  
  10. M_propertyGrid.AddProperty (group1 );
  11. M_propertyGrid.AddProperty (group2 );

The compilation and running effect is as follows:

Now, the method for creating CMFCPropertyGridCtrl statically ends.

Another method is dynamic creation, which is no different from controls such as CStatic and CEdit. After creation, you can also use the built-in functions to modify the properties of the control, such:

 

 
  1. CMFCPropertyGridCtrl * propertyGrid = new CMFCPropertyGridCtrl;
  2. PropertyGrid-> Create (WS_CHILD | WS_BORDER | WS_VISIBLE, CRect (400,100,600,200), this, WM_USER + 100 );
  3. PropertyGrid-> EnableHeaderCtrl (TRUE); // use the header
  4. PropertyGrid-> SetVSDotNetLook (); // use the style
  5. PropertyGrid-> MarkModifiedProperties (); // highlights the modified part.

More attributes are to be explored.

This article from the "front of the tradeford reverse tairuibao blog, please be sure to keep this source http://serious.blog.51cto.com/242085/956984


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.