VC/mfc Q &

Source: Internet
Author: User

Q How to Deal with the Enter key in ComboBox? Avoid exiting the program?
In general, the method used by a in edit is to process pretranlatemessage () and execute the code.
Cwnd * pwnd = getfocus ();
If (pwnd! = NULL)
{
If (pwnd = getdlgitem (idc_edit1)
{
... // Idc_edit1 with focus
}
}
 
But in ComboBox, it seems that the Edit Control of ComboBox gets the focus, so the code is determined:
Bool cdlg: pretranslatemessage (MSG * PMSG)
{
If (PMSG-> message = wm_keydown & PMSG-> wparam = vk_return)
{
Cwnd * pwnd = getfocus ();
If (pwnd! = NULL)
{
If (pwnd-> getparent () = getdlgitem (idc_combo1) // change the ID
{
Return true;
}
}
}
Return cdialog: pretranslatemessage (PMSG );
}

//-------------------------------------------------
Q How do I set the height of the drop-down list box in a dynamically created combo box?
A m_combo.create (ws_child | ws_visible | ws_vscroll cbc_sort | cbc_dropdownlist | ws_tabstop, crect (580,280, 10, 114), this );
// The last parameter of crect (280 here) indicates the drop-down size.

//-------------------------------------------------
Q is there any way to disable user input without selecting the drop-down list style?
A sets the Edit Control of the drop-down list as read-only. The method is as follows:
Ccombobox * pcombo;
Cwnd * pwnd = pcombo-> getwindow (gw_child );
While (pwnd)
{
Char classname [256];
: Getclassname (pwnd-> m_hwnd, classname, 256)
If (strcmp (classname, "edit") = 0)
{
Cedit * pedit;
Pedit = new cedit ();
Pedit-> subclasswindow (pwnd-> m_hwnd );
Pedit-> setreadonly ();
Pwnd = pwnd-> getnextwindow ();
Delete pedit;
}
If (pwnd)
Pwnd = pwnd-> getnextwindow ();
}

//-------------------------------------------------
Q ComboBox's custom pop-up menu. Do you want to pop up the menu when you right-click the edit part of the ComboBox?
A method is implemented in the onctlcolor function of ccustomcombo to generate a subclass of the edit box in ComboBox. For example:
Hbrush ccustomcombo: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
If (nctlcolor = ctlcolor_edit)
{
If (m_edit.getsafehwnd () = NULL)
M_eidt.subclasswindow (pwnd-> getsafehwnd ());
}
Hbrush HBr = ccombobox: onctlcolor (PDC, pwnd, nctlcolor );
Return HBr;
}
// M_edit is the implementation of the cedit class. It displays the right-click menu on wm_rbuttonup

//-------------------------------------------------
Q: How to add bitmap to buttons?
A
The button for dynamic creation:
Cbutton button;
Button. Create (_ T ("My button"), ws_child | ws_visible | bs_bitmap, crect (10, 10, 60, 50), pparentwnd, 1 );
Button. setbitmap (: loadbitmap (null, makeintresource (ibm_check )));
Or modify the style:
Uint style = button. getbuttonstyle ();
Style = style | bs_bitmap;
Button. setbitmap (: loadbitmap (null, makeintresource (ibm_check )));

//-------------------------------------------------
Q how to capture the bn_clicked message in the cbutton derived class and the parent dialog box?
A is opposite to a wm_policy message. The notification message bn_clicked is sent as a wm_command message. Therefore, the application should use on_control_reflect_ec instead of on_policy_reflect.

//-------------------------------------------------
Q how to determine whether an object has the current focus?
A return (cwnd: getfocus () = pwnd );

//-------------------------------------------------
Q How do I set the numeric limit attribute of the Edit Control?
A
Long style = getwindowlong (m_editctrl.m_hwnd, gwl_style );
Style | = es_number;
Setwindowlong (m_editctrl.m_hwnd, gwl_style, style );

//-------------------------------------------------
Q How can I obtain the same image used by explorer if I want to display the file in listctrl?
A can add the system imagelist to listctrl, and then use shgetfileinfo with the shgfi_icon to obtain the appropriate icon index:
// Set the image list
Himagelist;
Shfileinfo fi;
Cimagelist m_smalllist;
// Obtain the handle of the system small icon list
Himagelist = (himagelist) shgetfileinfo (lpctstr) _ T ("C: //"), 0, & Fi, sizeof (shfileinfo), shgfi_sysiconindex | shgfi_smallicon );
// Add to the small image list
M_smalllist.attach (himagelist );
// Set the listctrl image list
M_listctrl.setimagelist (& m_smalllist, lvsil_small );
// List of separated Images
M_smalllist.detach ();

//-------------------------------------------------
Q How do I display icons in any column of the list, instead of the first column?
A
Lv_item item;
...
Item. Mask = lvif_text | lvif_image | lvif_state | lvif_param;
Item. iItem =... // set the row number
Item. lparam =... // how to set the lparam Parameter
Item. isubitem =... // set the column number, starting from 0
Item. statemask = lvis_stateimagemask;
Item. State = indextostateimagemask (...); // The parameter is the icon number.
Item. iimage =... // set the icon number
Item. psztext =... // display text
// Insert a new item
M_listctrl.insertitem (& item );
// Set the icon now
M_listctrl.setitemtext (0, 4, szfield );

//-------------------------------------------------
Q How can I implement automatic rollback when I add a new item to ListBox?
After calling addstring, A adds the following code:
M_listbox.settopindex (m_listbox.getcount ()-1 );

//-------------------------------------------------
Q How can I make the horizontal scroll bar work normally when the text in ListBox exceeds the width of the box?
A uses the following code to set the scroll bar width to the longest string width.
Void sethorizontalextent (clistbox & ListBox)
{
Int Index = ListBox. getcount ();
If (Index = lb_error)
Return;
Int nextent = 0;
If (INDEX)
{
CDC * PDC = ListBox. getdc ();
Cfont * poldfont = PDC-> SelectObject (ListBox. getfont ());
Cstring S;
Size text;
Long maxtxt = 0;
Whilw (index --)
{
ListBox. gettext (index, S );
TEXT = PDC-> getoutputtextextent (s );
If (text. CX> maxtxt)
Maxtxt = text. CX;
}
Text. Cx = maxtxt;
PDC-> lptodp (& text );
Nextent = text. cx + 2;
PDC-> SelectObject (poldfont );
ListBox. releasedc (PDC );
}
ListBox. sethorizontalextent (nextent );
}

//-------------------------------------------------
Q when splitting a view, four views (two rows and two columns) are created, and the bottom right is cformview. The other views are cview. The oncreatecilent of cmainframe specifies the size of the crect no matter how it is specified, the two views below occupy the entire window and need to be dragged!
Generally, a only needs to be added at the end of oncreateclient:
M_wndspitter.setrowinfo (0,200, 0); // Add this line of code

//-------------------------------------------------
Q How to specify the minimum width of the split window?
A uses cspitterwnd: setcolumninfo ()
Void setcolumninfo (INT Col, // specifies the column
Int deal, // ideal width (pixel)
Int cxmin); // minimum width (pixel)
After using setcolumninfo, you should also call recallayout (); to re-adjust the layout.

//--------------------------------------------------
Q How do I determine whether the toolbar is horizontal or vertical?
A If (m_toolbar.getbarstyle () & cbrs_align_left) = cbrs_align_left |

(M_toolbar.getbarstyle () & cbrs_align_right) = cbrs_align_right)
Afxmessagebox ("vertical ");
Else
Afxmessagebox ("horizontal ");

//--------------------------------------------------
Q: How can I modify the visibility of toolbar buttons by programming?
Sample Code:
DWORD style = m_toolbar.getbuttonstyle (nindex );
If (m_bhide)
M_toolbar.setbuttonstyle (nindex, Style &~ Ws_visible );
Else
M_toolbar.setbuttonstyle (nindex, style | ws_visible );
M_bhide =! M_bhide;

//--------------------------------------------------
Q How do I add a button in the status bar and respond?
A creates a cmybutton class derived from cbutton, adds a member variable of the cmybutton class to the main framework class, creates a button in the oncreate function, and associates it with the status bar:
M_mybtn.create ("mybutton", ws_child | visible, crect (0, 0, 60, 20), & m_wndstatusbar, 0 );
By processing bn_clicked messages, you can process all click events in the cmybutton class.

//--------------------------------------------------
Q how to hide the title bar of the attribute cpropertysheet. Using modifystyle (window_caption, 0) has no effect
A creates its own cpropertysheet derived class and overwrites oninitdialog. after going to the default situation, use modifystyle to delete the ws_caption flag.

//--------------------------------------------------
Q How can I have two rows of tags on the property page?
A adds precreatewindow processing from the cpropertysheet derived class, and adds code before calling the base class:
CS. Style | = tcs_multiline;

//------------------------------------------------------
Q How do I transmit data between two pages of an attribute table?
A
Cpropertypage has a member function querysiblings (wparam, lparam ). This function can be used by applications.
Querysiblings generates a psm_querysiblings message, which is passed to all brothers, that is, other attribute pages in the Attribute Table. Generally, you can create an enumeration that is visible to all pages, for example:
Enum {query_my_string, query_something_else ,.......}
Then, when a property page requires information from other property pages, use the code:
Cstring mystring;
If (LL = querysiblings (query_my_string, (lparam) & mystring ))
{
... // Obtain the string
}
Provide a string page to process the psm_querysiblings message:
Lresult cpagethathasstring: onquerysiblings (wparam, lparam)
{
If (query_my_string = wparam)
{
* (Cstring *) lparam) = _ T ("test string");
Return 1l;
}
Else
Return 0l;
}

//----------------------------------------------------------
Q How to Make the property page have two rows of labels?
A
Derive a class from cpropertysheet, add a precreatewindow handler, and add the following code before calling the base class handler: CS. Style | = tcs_multiline;

 

//-----------------------------------------------------------
Q how to hide the title bar of an attribute page?
A
Derive a class of your own from cpropertysheet and overwrite the oninitdialog. Use modifystyle to delete the title bar flag ws_caption after you go to the default setting.
Modifystyle (ws_caption, 0 );

//-------------------------------------------------------------------
Q How do I enumerate desktop projects?
A
1. Get the pointer to the ishellfolder interface.
2. Get the pointer to the imalloc interface.
3. Get the pointer to the ienumidlist interface.
4. Extract pidl from the next project in enumeration.
5. determine the type of the identifier represented by pidl
6. process the project
7. Release the memory allocated by pidl
8. Repeat steps 4 to 7 to know that all projects have been enumerated.
9 Release the pointer of the ishellfolder imalloc ienumidlist Interface

Lpshellfolder;
Lpmalloc;
Lpenumidlist lpidlist;
M_namecount = 0;
Hresult hR = shgetdestopfolder (& lpshellfolder );
If (hR = noerror)
{
HR =: shgetmalloc (& lpmalloc );
If (hR = noerror)
{
HR = lpshellfolder-> enumobject (null, shcontf_folders | shcontf_nonfolders, & lpidlist );
If (hR = noerror)
Processfolder (lpshellfolder, lpmalloc, lpidlist); // custom deal Function
Lpmalloc-> release ();
Lpidlist-> release ();
Invalidate ();
}
Lpshellfolder-> release ();
}
 
Void ***: processfolder (lpshellfolder, lpmalloc, lpenumidlist lpidlist)
{
Strret;
Ulong numfetch;
Lpitemidlist lpitemlist;
Hresult hR = lpidlist-> next (1, & lpitemlist, & numfetch );
While (hR = noerror)
{
Ulong attributes = sfgao_folder;
Lpshellfolder-> getattributes (1, (const struct _ itemidlist **) & lpitemlist, & attributes );
If (attributes & sfgao_folder)
{
HR = lpshellfolder-> getdiaplaynameof (lpitemlist, shgdn_normal, & strret );
If (m_namecount <20)
M_names [m_namecount ++] = strret. STR;
}
Lpmalloc-> free (lpitemlist );
HR = lpidlist-> next (1, & lpitemlist, & numfetch );
}
}
//-------------------------------------------------------------------
Q How to Create a desktop shortcut?
A:
1 initialize com
2 create lshelllink object
3 Use ishelllink interface to get the pointer about ipersistfile
4 Use ishelllink interface to initialize Link
5 Use lpersistfile interface to save the link
6 release all the com pointer
7 COM return to previous status

1
Hresult hR = coinitialize (null );
If (hR = s_ OK)
{
... // Continue
}

2
Ishelllink * pshelllink;
Pshelllink = cocreateinstance (clsid_shelllink, null, clsctx_inproc_server, iid_ishelllink, (void **) & pshelllink );

3
Ipersistfile * persistfile;
Persistfile = pshelllink-> QueryInterface (iid_ipersistfile, (void **) & persistfile );

4
Pshelllink-> setpath ("C: // config. sys ");
Pshelllink-> setdescription ("shortcut to config. sys ");

5
Char path [max_path];
Getwindowsdirectory (path, max_path );
Int Len = strlen (PATH );
Strcpy (& path [Len], "// desktop // config. Lik ");
// Change the char from ANSI to Unicode
Olechar widepath [max_path];
Multibytetowidechar (cp_acp, 0, path,-1, widepath, max_path );
Persistfile-> Save (widepath, true );

6
Pshelllink-> release ();
Psersistfile-> release ();

7
Couninitialize ();

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.