Q How to handle the Enter key in ComboBox? Avoid exiting the program? A The method used in the general EDIT is to process preTranlateMessage (), execute code CWND * PWnd = getfocus (); if (pwnd! = Null) {if (PWND == getdlgitem (IDC_EDIT1) {... // idc_edit1 has Focus}} But in ComboBox, it is different. It is the focus of ComboBox's editing control, so judgment code: 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 ID {Return True;}}} Return CDialog :: PretranslateMessage (PMSG);
/ / -------------------------------------------------------------------------------------------- -Q Dynamic Create Composite Box How to set the height of the drop-down list box? A m_combo.create (WS_CHILD | WS_VISIBLE | WS_VSCROLL CBC_SORT | CBC_DropDownload | WS_TABSTOP, CRECT (320, 10, 580, 280), THIS, 114); // CRECT's last parameter (here 280) means pull-down size
/ / -------------------------------------------------------------------------------------------- (Whether the user input value can be prohibited without selecting a drop-down list style, what is the method can be implemented? A Set the editing control of the drop-down list to 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 (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, want to pop up the menu when you right-click the editing part of the combo box? A method is to perform a subclass of the edit box in CCustomCombo, an 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 = CCOMBOX: ONCTLCOLOR (PDC, PWND, NCTLCOLOR); Return Hbr;} // where m_edit is CEDIT class Implementation, it is displayed on WM_RButtonUp // -------------------------------------- ----------- q How to add Figure A for the button to play the button: CButton Button; Button.create (_T ("My Button), WS_CHILD | WS_VISIBLE | BS_BITMAP, CRECT (10, 10, 60, 50), PParentWnd, 1); Button.SetBitmap (: Loadbitmap)); or modify style: uint style = button.getButtonStyle (); style = style | BS_bitmap; button. SetBitmap (:: loadbitmap (null, makeintresource (ibm_check));
/ / -------------------------------------------------------------------------------------------- - How do I capture BN_Clicked messages in the CButton derived class and parent dialog box? A On the contrary to the WM_NOTIFY 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_NOTIFY_REFLECT
/ / -------------------------------------------------------------------------------------------- -Q how to determine if an object has the current focus? A return (cwnd :: getfocus () == PWND);
/ / -------------------------------------------------------------------------------------------- -Q How do I set the digital limit attribute of the editing control? Along style = getWindowlong (m_editctrl.m_hwnd, gwl_style); style | = es_Number; setWindowlong (m_editctrl.m_hwnd, gwl_style, style);
/ / -------------------------------------------------------------------------------------------- -Q wants to display files in ListCtrl, how can I get the same image of Explorer? A You can add the iMageList's imagelist to the listCtr, and then get the appropriate icon index with the SHGETFILEINFOFO with the SHGFI_ICON flag: // Image list Set HimageList HimageList; shfileinfo fi; cimagelist m_smalllist; // Get the handle of the system small icon list HimageList = (HimageList) SHGETFILEINFO ((LPCTSTR) _T ("C: //"), 0, & FI, SIZEOF (SHFILEINFO), SHGFI_SYSICIONDEX | SHGFI_SMALLICON); / / Add to small image list m_smalllist.attach (himagelist); // Setting Image list m_listctrl.setimagelist (& m_smalllist, lvsil_small); // Separate image list m_smalllist.detach (); // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------- How to display icon in any column of the list, not the first column? A lv_item item; ... item.mask = lvif_text | lvif_image | lvif_state | lvif_param; item.iitem = ... // set line number item.lparam = ... // How to set up LPARAM parameter item.isubitem = ... // Set the list number, Item.Statemask = LVIS_STATEMAGEMASK = LVIS_STATEMAGEMASK = LVIS_STATEMAGEMASK; Item.State = indexTostateImageMask (...); // parameter is the icon number item.iimage = ... // Set the icon number item. PSZText = ... // Display text // Insert a new item m_listctrl.insertitem (& item); // Now set the icon M_ListCtrl.SetItemText (0,4, szfield);
/ / -------------------------------------------------------------------------------------------- How does the -q add new items to ListBox to automatically roll? A After calling addstring, add the following code: m_listbox.settopindex (m_listbox.getcount () - 1);
/ / -------------------------------------------------------------------------------------------- -Q ListBox text exceeds the width of the box, how to make the horizontal scroll strip normal? A Use the following code to set the width of the scroll bar to the longest string width void sethorizontalExtent (CListBox & ListBox) {int index = listbox.getcount (); if (index == lb_error) return; int next = 0; if ( Index) {CDC * PDC = listbox.getdc (); cfont * PoldFont = PDC-> selectbox.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); next = text.cx 2; PDC-> SelectObject (PoldFont); ListBox.releaseDC (PDC);} listbox.sethorizontaleXtent (Nextent);} // --------------------- --------------------------- Q When split the view, create four views (2 lines 2), the lower right CFormView, others are CView, on CMAINFRAME's oncreatecilent, regardless how CRECT is specified, the two views below account for the entire window, need to be dragged! A generally only need to add: m_wndspitter.setrowinfo (0,200,0); // Add this line code
/ / -------------------------------------------------------------------------------------------- -Q how to specify the minimum width of the split window? A Use cspitterWnd :: setColumnInfo () Void setColumnInfo (int COL, / / Specify column INT DEAL, // ideal width (pixel) int Cxmin); // The minimum width (pixel) should also call recallayout after using SetColumnInfo; Re-adjust the layout. / / -------------------------------------------------------------------------------------------- --Q how to determine the toolbar is or vertical? A IF ((m_toolbar.get barStyle () & cbrs_align_left) == CBRS_ALIGN_LEFT ||
(m_toolbar.getbarstyle () & cbrs_align_right) == CBRS_ALIGN_RIGHT) AFXMESSAGEBOX ("Vertical"); Else AfxMessageBox ("Horizontal");
/ / -------------------------------------------------------------------------------------------- --Q programming way to modify the visibility of the toolbar button? A 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 buttons to the status bar and respond? A Create a CMYBUTTON class derived from CButton, add the member variable of the CMYBUTTON class in the main frame class, and then create a button in the oncreate function and associate it and 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 handle all click events in the CMYBUTTON class.
/ / -------------------------------------------------------------------------------------------- - Q How to hide the title bar of the property cpropertySheet, use modifystyle (window_caption, 0) No effect A creation your own cpropertySheet derived class, overnInitDialog, turn to the default situation, use modifyStyle to delete the WS_CAPTION flag
/ / -------------------------------------------------------------------------------------------- - How to make the property page two lines of labels? A From the CPROPERTYSHEET, add the processing of PrecreateWindow, add code before calling the base class: cs.style | = tcs_multiline;
/ / -------------------------------------------------------------------------------------------- ---- Q How to pass data between the two pages of the attribute table? A CPropertyPage has a member function QuerySiblings (WPARAM, LPARAM). Applications can use this function. QuerySiblings generates a PSM_QuerySiblings message, which passes to all the brothers, that is, other property pages on the property table. Generally you can create all page visible enumerations, such as enum {query_my_string, query_something_lse, .......} then use code: cstring mystring; if ( lL == QuerySiblings (QUERY_MY_STRING, (LPARAM) & myString)) {.... // Get string string provided} sheet processing PSM_QUERYSIBLINGS message: LRESULT CPageThatHasString :: OnQuerySiblings (WPARAM wParam, LPARAM lParam) {if (QUERY_MY_STRING = = WPARAM) {* ((cstring *) lparam) = _t ("test string"); return 1L;} else return 0L;} // ------------------ --------------------------------------- How to make the property page two lines of labels? A derived a class from CPROPERTYSHEET to add a precomreateWindow process, and then add the following code before calling the base class: cs.style | = tcs_multiline;
/ / -------------------------------------------------------------------------------------------- ----------- How to hide the title bar of the property page? A derive a class from CPropertySheet and override OnInitDialog. After transferring to the default, use MODIFYSTYLE to delete the title bar flag WS_CAPTION. ModifyStyle (WS_CAPTION, 0); // ---------------------------------------- -------------------------- Q how to enumerate the desktop project? A1 gets the pointer 2 to the ISHELLFOLDER interface to obtain a pointer to the IMALLOC interface to get a pointer 4 of the IenumIDList interface to extract the list of the subscript of the next item of the next item of the PIDL 5 Processing the Type 6 Processing the item 7 Release PIDL Allocation Memory 8 Repeat 4 to 7 steps, know all items to enumerate 9 Release Ishellfolder IMALLOC IEENUMIDLIST interface pointer
LPSHELLFOLDER lpshellfolder; LPMALLOC 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 lpshellfolder, LPMALLOC lpmalloc, LPENUMIDLIST lpidlist) {sTRRET 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, & number);}} // -------------------------------------------------- -------------- Q How to create a desktop shortcut? A: 1 initialize com2 create LShellLink Object3 Use IShellLink interface to get the pointer about IPersistFile4 Use IShellLink interface to initialize link5 Use LPersistFile interface to save the link6 Release all the com pointer7 Com return to previous status
1 HRESULT hr = CoInitialize (NULL); if (hr == S_OK) {... // Continue} 2IShellLink * pshelllink; pshelllink = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) & pshelllink);
3IPERSISTFILE * PERSISTFILE; persistfile = pshellllick-> queryinterface (IID_IPERSISTFILE, (VOID **) & persistFile;
4PSHELLLINK-> setPath ("c: //config.sys"); pshelllink-> setdescription ("Shortcut to config.sys");
5CHAR PATH [MAX_PATH]; GetWindowsDirectory; int Len = strlen (path); strcpy (& path [len], "// desktop // config.lik"); // Change the char from Ansi To Unicodeolechar WidePath [Max_path]; multibytetowidechar (CP_ACP, 0, Path, -1, Widepath, Max_Path); persistfile-> save (widepath, true);
6PSHELLINK-> Release (); psersistfile-> release ();
7couninitialize ();