VC entry 4 (common control)

zhaozj2021-02-16  50

Common control

He Zhidan

main content:

1, button

(1), bitmap buttons and dynamic buttons

(2), self-drawn button

2, list box

3, edit control and crichedit

4, CSLIDERCTRL

The button is used is very simple, dragging one to the dialog, double-click it to edit the code. Using the Program Wizard into a dialog-based program control, dragging a button on the dialog box, ID is IDC_OWNER, double-click the control to add a response function. Set the associated variable m_ok for IDOK, add the following code to the response function. m_ok.enableWindow (false); its role is to disable OK.

Change the code just added to the following, Ctrl F5, click our plus button to see the determination button to turn the check box, let's click it, it executes the previous code.

Uint style = m_ok.getButtonStyle ();

STYLE | = BS_3STATE;

m_ok.setButtonStyle (Style);

Style: BS_AUTOCHECKBOX BS_AUTORADIOBUTTON BS_AUTO3STATE BS_CHECKBOX BS_DEFPUSHBUTTON BS_GROUPBOX BS_LEFTTEXT BS_OWNERDRAW BS_PUSHBUTTON BS_RADIOBUTTON BS_3STATE

Change the code just added to the following:

CWND * PWND = Getdlgitem (IDOK);

PWND-> EnableWindow (false);

Its effect is also to make the determination button disabled, the first line is based on the ID of the CWND pointer, pay attention to some functions are unique, then I must convert the PWND to the CButton class pointer.

The CButton commonly used functions are:

.Getfocus (); get a window with focus, setfocus (); get focus. If a button has focus, space can make it execute.

Getfont (); setFont (); get and set fonts.

Bitmap button.

On the self-drawing style hook of the determined button, the title of the determination button is changed to OK, plus four bitmaps, "Oku", "Okd", "OKF", "Okx", and bounce with the button, pressed , Get focus, disabled, pay attention to the name of the bit.

Define a member variable CBitmapButton BB, in OnInitDialog ()

Bb.autoload (IDOK, this);

There is a question to note that when you click on the draw, the default button will be canceled, so you must reset a default button.

Change the above sentence to:

Bb.LoadBitmaps ("OKU"); // You can load up to 4, at least one.

bb.subclassdlgitem (IDOK, this); // is associated with the control.

bb.sizetocontent (); // change the size of the control to accommodate bitmap

Self-drawn button:

On the self-portrait hook of the cancel button, Ctrl W open the class wizard, double-click the response function of the CControlDLG message WM_DRAWITEM.

Void CControlsdlg :: OnDrawItem (int Nidctl, LPDrawItemstruct LPDrawItemstructure)

{

IF (IDCANCEL == Nidctl)

{

CDC * PDC = New CDC ();

PDC-> Attach (LPDrawItemstruct-> HDC);

PDC-> Textout (10, 10, "haha");

CDIALOG :: OnDrawItem (Nidctl, LPDrawItemstruct);

}

Alternate function

CRECT R;

GetClientRect (& r);

R Stores the boundary value of the cancel button.

List box

Create a dialog-based program control, add a list box, ID: IDC_LIST, set a variable m_ctrl of the control type, add three buttons, IDs are: append, insert, delete;

Void ccontrolsdlg :: onappend ()

{

Updatedata ();

IF (m_input.isempty ()) Return;

INT i = m_ctrl.getcount ();

m_ctrl.setcurseel (i-1); // Select the last one;

m_ctrl.addstring (m_INPUT);

}

Void ccontrolsdlg :: onjinsert ()

{

INT i = m_ctrl.getcurseel ();

IF (-1 == i)

MessageBox ("Please select an item in the list box, the new item will be behind this", "Note");

Else

{

Updatedata ();

m_ctrl.insertstring (i 1, m_input);

}

}

Void ccontrolsdlg :: onremove ()

{

INT i = m_ctrl.getcurseel ();

IF (-1! = i)

m_ctrl.deleteString (i);

}

Common functions:

RESETCONTENT is empty.

INT FINDSTRING (INT NSTARTAFTER, LPCTSTAR LPSZITEM) Const;

Start searching for the string referred to by LPSZITEM from the NSTARTAFTER, if the return -1 is not found, otherwise returns the serial number.

INT SelectString (int NStartAfter, LPCTSTARTAFTER, LPCTSTAR LPSZITEM);

Similar to FINDSTRING, find the found string after finding.

Edit control:

Generate a dialog-based program controls, add an editing control, set an associated variable m_ctrl of a control type. Add a button, double-click Code:

m_ctrl.setsel (0, 3); // Select the first 4 characters.

m_ctrl.cut (); // Cut

m_ctrl.paste (); // Paste

m_ctrl.paste (); // Paste

Similar functions include undo, Copy, ReplaceSel.

Void MoveWindow (int X, int y, int nwidth, int nheight, bool bowt = true);

Void MoveWindow (LPCRect LPRECT, BOOL BREPAINT = true);

Mobile window (such as controls).

The last parameter is heavy, the front parameters indicate the position of the four corners, pay attention to the size and proportion possible may change.

Build a dialog-based application Control, add a CRICHEDIT CTRL F5, and there should be no reactions. Because we didn't add:

Bool ccontrolsapp :: initInstance ()

{

Afxinitrichedit ();

That'll be fine.

CSLIDERCTRL

Generate a dialog-based application Controls, add a slider control, let it associate with a shaped variable m_value, let it associate the NM_RELEASEDCAPTURE of this control with a control type, add: Void CControlsdlg :: OnReleaseDcaptureSlider1 (NmHDR * PNMHDR, LRESULT * PRESULT)

{

Updatedata (TRUE);

CString Str;

Str.Format ("% d", m_value);

MessageBox (STR);

* PRESULT = 0;

}

Plus:

m_ctrl.setRange (20, 120); // Set the smallest, maximum

m_ctrl.setpagesize (30); // How much is set once a change

m_ctrl.setpos (30); // Set location

转载请注明原文地址:https://www.9cbs.com/read-25726.html

New Post(0)