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 creation group 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
/ / -------------------------------------------------------------------------------------------- -
Q Can I do not choose a drop-down list style, or prohibit user input values, what is the way to implement? 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, pop up the menu when you right-click the editing section 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 a button to the button to dynamically created: 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 styles: UINT style = Button.GetButtonStyle (); style = style | BS_BITMAP; Button.SetBitmap (:: LoadBitmap (NULL, MAKEINTRESOURCE (IBM_CHECK ))))
/ / -------------------------------------------------------------------------------------------- -
Q How to capture BN_Clicked messages in the CButton derived class and the parent dialog? 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 to set the digital limit attribute of editing controls? Along style = getWindowlong (m_editctrl.m_hwnd, gwl_style); style | = es_Number; setWindowlong (m_editctrl.m_hwnd, gwl_style, style);
/ / -------------------------------------------------------------------------------------------- -
Q I hope to display the file in ListCtrl, how can I get the same image as 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 (); // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------
Q How to display an 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);
/ / -------------------------------------------------------------------------------------------- -
Q How to implement automatic down when adding new items to ListBox? A After calling addstring, add the following code: m_listbox.settopindex (m_listbox.getcount () - 1);
/ / -------------------------------------------------------------------------------------------- -
Q ListBox's text exceeds the width of the box, how to make the horizontal scroll bar working properly? 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, other CVIEW, in
No matter how the CMAINFRAME is specified in the size of CRECT, the two views below account for the entire window, and you need to drag! 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 Programmatically 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 to add buttons in the status bar and respond? A Create a CMYBUTTON class derived from CButton, add a member variable of the CMYBUTTON class in the main frame class, and then on the oncreate letter
Create a button in the number 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 message, you can in CMYButton Treat all click events in the class
/ / -------------------------------------------------------------------------------------------- -
Q Hide the title bar of the property CPropertySheet, use modifystyle (window_caption, 0) No effect A Create your own cpropertySheet derived class, override OnInitDialog, turn to the default situation, use ModifyStyle to delete
In addition to WS_CAPTION logo
/ / -------------------------------------------------------------------------------------------- -
Q How to make the property page two line tags? A From the CPROPERTYSHEET, add the precomreateWindow processing, add code before calling the base class: cs.style | = tcs_multiline; a few questions about the property page
Q How to pass data between the two pages of the property 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;}
Q How to make the property page two line tags? A From CPropertySheet to add a PRECREATEWINDOW process, then add the following code before calling the base class: cs.style | = tcs_multiline; Q 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); how to adjust the order of TabStop on the Dialog window?
1) Select resource management (resourceView), choose Dialog ID, double click on Layout-> Tab ORDER in the menu, press and hold the Alt, order, order 2) Select ResourceView, select Dialog ID, double click Press and hold the Ctrl D order.
///
Q // control adaptable to form changes
ACWinsizeDlg :: CWinsizeDlg (CWnd * pParent / * = NULL * /): CDialog (CWinsizeDlg :: IDD, pParent) {// {{AFX_DATA_INIT (CWinsizeDlg) // NOTE: the ClassWizard will add member initialization here //}} AFX_DATA_INIT // Note That Loadicon Does Not Require a Subsequent Destroyicon in Win32m_Hicon = AFXGetApp () -> Loadicon (iDR_MainFrame); m_bresizeable = false; // added}
Bool cwinsizedlg :: oninitdialog () {cdialog :: oninitdialog ();
// set the icon for this dialog. The framework does this automatic or //hen the application's main window is not a dialogseticon (m_hicon, true); // set big iconseticon (m_hicon, false); // set small icon
// Todo: Add Extra Initialization Here
RESETCTRLSIZE (); // addedm_bresizeable = true; // addedreturn true; // return true unless}}
Void cwinsizedlg :: Onsize (uint ntype, int cx, int CY) {cdialog :: Onsize (NTYPE, CX, CY); if (m_bresizeable == true) // addedresetCtrlsize (); // added}
Void cwinsizedlg :: resetctrlsize () {// all is added // User custom function, uses the EDIT control when the form is changed, and the EDIT control is followed
CEDIT * PEDIT = (CEDIT *) getdlgitem (idc_edit_demo);
CRECT RCEDIT; CRECT RCDLG;
GetClientRect (rcdlg); // Get a customer area coordinate PEDIT-> getWindowRect (RCEDIT) of the dialog box; // Get the screen coordinate screen of the edit box; // Transform the screen coordinates of the edit box to a dialog box Relative coordinates of the area
RCEDIT.right = rcdlg.right-5; // Set the new coordinate rccedit.bottom = rcdlg.bottom-5; pedit-> movewindow (rcedit); // Adjust the edit box position
}
///
QccomboBox pull-down height too small problem A manual solution: Combo box styles property is changed to Simple, change its height, then the required styles property
The following is the data ccomboBOx * pport = (ccombox *) getDLGITEM (IDC_COMBO1); pport-> addstring ("COM2"); pport-> addwintowtext ("ttt"); pport-> setwindowtext );
///
The Q dialog box has a static text IDC_STATIC, asks the mouse to click this text, then pop up IE to a website! How can I do?
If further, when the mouse is moved to static text, it becomes a hand shape, and the static text becomes blue and loops, just want to browse the website! ! What to do?
A In the same function in your dialog, Bool C **** DLG :: OnsetCursor (CWND * PWND, UINT NHITTEST, UINT Message) {// Todo: Add Your Message Handler Code Here and / or Call Defaultswitch (PWND-> GetDlgCtrlID ()) {case IDC_STATIC1: // IDSetCursor static control (AfxGetApp () -> LoadCursor (IDC_CURSOR1)); // IDC_CURSOR1 is your mouse hand return TRUE;} return CDialog :: OnSetCursor (pWnd, nHitTest, message) }
After clicking, you can use Shellexecute (NULL, "Open", "http://www.google.com", "", ", sw_show);
///
QcfileDialog read and write
A {// write file tchar szfilters [] = _ t ("setting file (* .set) | * .SET | all files | *. * ||"); cfiledialog dlg (false, _t ("set"), _t ("* .SET"), OFN_FILEMUSTEXIST | OFN_HIDEREMUSTEXIST | OFN_HIDEREADOSLY | OFN_OVERWRITEPROMPT, SZFILTERS; if (DLG.Domodal () == idok) {cstring strate; strTest.Format ("% s", "hello!");
CString strFileName = dlg.GetPathName (); try {CStdioFile file (strFileName, CFile :: modeWrite | CFile :: modeCreate); MessageBox (strTest); file.SeekToEnd (); file.WriteString (strTest);} catch (CFileException * e) {e-> reporterror (); e-> delete ();
}}} {// read files CSTRING STRTEST; TCHAR SZFILTERS [] = _ T ("Settings File (* .SET) | * .Set | All files | *. * | ||); cfiledialog DLG (True, _t (" set "), _T (" *. Set "), OFN_FILEMUSTEXIST | OFN_HIDEREMUSTEXIST | OFN_HIDEREADOSLY, SZFILTERS; if (DLG.Domodal () == iDok) {cstring strfilename = DLG.GetPathName (); // Get file name Try {cstdiofile file ( Strfilename, cfile :: modeRead; // Create file.readstring (strTRTEST); // Read MessageBox (strTest); // Display} catch (cfileException * e) {e-> replacerror (); e-> delete () ;}}} Q // Draw Avoid CMY0x10dlg :: OnButton1 () {in a sub-window or control
CWND * PWND = this-> getdlgitem (IDC_STATIC_X); // Static control CRECTRECT; PWND-> getClientRect (Rect); CDC * DC = PWND-> getDC (); // DC-> Moveto (10, 10); / / DC-> LineTo (100,100); // DC-> Rectangle (1, 1, 10, 10); ARC (DC-> M_HDC, 1, 1, 100, 100, 200, 200, 5, 5);} Q mouse automatically moves to the control center ( Likfeng Original) // 9CBS ID: Likfeng Original A1. Dialog Add a Button to define the associated CButton variable M_BTN1
2.void cdlgtest :: OnMousemove (uint nflags, cpoint point) {// Todo: add your message handler code here and / or call default cutmouse; // mouse cRect RectButton; // button PointMouse = POINT;
m_btn1.getwindowRect (RectButton); ScreenToClient (RectButton);
INT di = 30; // The distance of the mouse approaches the button, see needs to adjust, need Di> 0
IF ((RectMouse.x> (RectButton.x CDIALOG :: ONMOUSEMOVE (NFLAGS, POINT);} 3. Ok, try it.