Button Control
Create button: bool cbutton: Create (maid, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint NID );
Processing button message: to process the button message, you need to map the message in the parent window. The ing macro is on_bn_clicked (ID, memberfxn) ID, which is the ID value of the button, which is the NID value specified during creation. The prototype of the processing function is afx_msg void memberfxn ();
Static box
Static text controls have simple functions and can be used as display strings, icons, and bitmaps. To create a window, you can use the member functions:
Bool cstatic: Create (lpctstr lpsztext, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint nid = 0 xFFFF );
Dwstyle indicates the style of the window. In addition to the common style ws_child and ws_visible of the sub-window, you can specify a special style for static controls.
Ss_center, ss_left, and ss_right indicate the alignment of characters.
Ss_grayrect displays a gray rectangle.
Ss_noprefix if this style is specified, the characters & will be displayed directly; otherwise & will be used as escape characters & will not be displayed and the characters after it will be underlined, if you want to display it directly, you must use.
Ss_bitmap display bitmap
Ss_icon
Ss_centerimage center display
Control the displayed text. Use the setwindowtext/getwindowtext member function to set/obtain the currently displayed text.
The member functions seticon and getIcon are used to set and obtain the currently displayed icons.
Controls the displayed bitmap. The setbitmap/getbitmap function is used to set/obtain the currently displayed bitmap. The following code demonstrates how to create a static window for displaying bitmaps and set bitmaps.
Cstatic * pstadis = new cstatic;
Pstadis-> Create ("", ws_child | ws_visible | ss_bitmap | sscenterimage,
Crect (0, 0, 40), pwnd, 1 );
Cbitmap BMP load;
BMP load. loadbitmap (idb_test );
Pstadis-> setbitmap (BMP load. Detach ());
Picture Control
The following describes how to use picture control to display BMP images:
The setbitmap is defined as follows:
// Ubmpresource: Image ID; uctrlresource: Picture Control ID
Void setbitmap (uint ubmpresource, uint uctrlresource)
{
Hbitmap;
Cstatic * pstatic = (cstatic *) getdlgitem (uctrlresource );
Hbitmap = (hbitmap) LoadImage (
AfxGetInstanceHandle (),
Makeintresource (ubmpresource ),
Image_bitmap,
0,
0,
Lr_loadmap3dcolors );
Pstatic-> modifystyle (0xf, ss_bitmap );
Pstatic-> setbitmap (hbitmap );
}
According to the definition, write the following code at the beginning of the dialog box (the result is displayed successfully ):
Bool clogindog: oninitdialog ()
{
Cdialog: oninitdialog ();
// Add images in the login window
Hbitmap;
// Associate the image ID
Hbitmap = (hbitmap) LoadImage (AfxGetInstanceHandle (),
Makeintresource (idb_bitmap_login_pic ),
Image_bitmap, 0, 0,
Lr_loadmap3dcolors );
// Obtain the pointer to the static control
Cstatic * pstatic = (cstatic *) getdlgitem (idc_static_pic); // Control ID
// Set the style of the static control so that it can use bitmap, and try to align the display.
Pstatic-> modifystyle (0xf, ss_bitmap | ss_centerimage );
// Set the static control display bitmap
Pstatic-> setbitmap (hbitmap );
Return true;
} Source: Examination major-Computer Level 2 Examination
//////////////////////////////////////// ////////////////////////////////////
Static bitmap loading:
1. Add control: add the text control as the display bitmap control and add the "display bitmap" button control;
2. Insert a bitmap resource and set the ID to idb_bitmap1;
3. Use classwizard to add a response function for the button control and add the following code:
Void cstaticimagedlg: onshowbitmap ()
{
// Todo: add your control notification handler code here
// Obtain the pointer to the static control
Cstatic * pstatic = (cstatic *) getdlgitem (idc_staticimage2 );
// Obtain the bitmap handle
Hbitmap =: loadbitmap (afxgetapp ()-> m_hinstance, makeintresource (idb_bitmap1 ));
// Set the style of the static control so that it can use bitmap, and try to align the display.
Pstatic-> modifystyle (0xf, ss_bitmap | ss_centerimage );
// Set the static control display bitmap
Pstatic-> setbitmap (hbitmap );
}
Then you can compile and run the program.
Dynamic bitmap Loading
Dynamic bitmap loading means you can click the button to select the bitmap you want to open in the file, instead of importing it in advance.
1. Add the display bitmap control first, but this time the picture control is added, and then add the "display bitmap" button;
2. Add a response function for the "display bitmap" button:
Void cloadmapdlg: onloadmap ()
{
// Todo: add your control notification handler code here
Onpaint ();
Cstring m_strfile;
Crect R;
Getclientrect (& R); // obtain the client window size of the control.
Cfiledialog DLG (true, null, null, ofn_hidereadonly | ofn_overwriteprompt,
_ T ("layer files (*. DEM ;*. tiff ;*. BMP ;*. JPG) | *. DEM ;*. tiff ;*. BMP ;*. JPG;) | "), null );
If (DLG. domodal ())
{
M_strfile = DLG. getpathname ();
}
Hbitmap = (hbitmap) LoadImage (AfxGetInstanceHandle (), m_strfile, image_bitmap, R. Width ()/2,
R. Height ()/2, lr_loadfromfile );
Cstatic * PPIC = (cstatic *) getdlgitem (idc_picture );
Assert (PPIC );
PPIC-> setbitmap (hbitmap );
Invalidate ();
}
3. The most important thing is that the type of the inserted picture control should be "bitmap", rather than frame. Otherwise, compilation will pass and the bitmap cannot be displayed!
//////////////////////////////////////// //////////////////////////////////////// ///
View articles
When VC dynamically loads BMP bitmap resources SDK and MFC inserts resources, both require BMP format bitmap resources. However, bitmap resources do not display dynamic images. Okay, no nonsense. The following describes two types of VC dynamic loading of BMP bitmap resources:
1. Load the bitmap resource of the BMP file path
An API function LoadImage () is used to query the msdn
Add a static text control idc_static, add the member variable m_image, and add the following code:
This-> m_image.modifystyle (0, ss_bitmap | ss_centerimage); // This static control is used to display bitmaps,
Hbitmap hbmp = (hbitmap): LoadImage (0, "2.bmp", image_bitmap, 0, 0, lr_loadfromfile); // The BMP file path directly reads the handle of the BMP image, convert to hbitmap type
This-> m_image.setbitmap (hbmp); // displays the BMP file in a static text box.
2. dynamically load the bitmap BMP in the Resource
Use the API function loadbitmap ()
Add the bitmap resource ID idb_bitmap1 and a static text control idc_static, and add the member variable m_image to it,
Add the following code:
This-> m_image.modifystyle (0, ss_bitmap | ss_centerimage); // set this static control to display bitmap
Hbitmap =: loadbitmap (afxgetresourcehandle (), makeintresource (idb_bitmap1 ));
// Obtain the bitmap handle of idb_bitmap1 from the project resource and assign it to the hbitmap variable
This-> m_image.setbitmap (hbitmap); // displays the BMP file in a static text box.
Compiled by VC ++ 6.0
Edit box
The edit window is the most common control used to receive user input. You can use the member functions to create an input window:
Bool cedit: Create (lpctstr lpsztext, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint nid = 0 xFFFF );
Dwstyle indicates the style of the window. In addition to the commonly used style ws_child and ws_visible of the subwindow, you can specify a special style for the input control.
Es_autohscroll and es_autovscroll indicate that the input text is automatically rolled when the display range is exceeded.
Es_center, es_left, es_right specify alignment
Does es_multiline allow multiple input rows?
Whether es_password is a password input box. If this style is specified, the entered text is displayed *
Whether es_readonly is read-only
Es_uppercase and es_lowercase show uppercase/lowercase characters
Control the displayed text. Use the setwindowtext/getwindowtext member function to set/obtain the currently displayed text.
You can use getlimittext/setlimittext to obtain/set the number of characters entered in the input box.
Because the user may select a piece of text during input, the user can use voidcedit: getsel (Int & nstartchar, Int & nendchar) to obtain the range of characters selected by the user, and call void cedit :: setsel (INT nstartchar, int nendchar, bool bnoscroll = false) can be used to set the selected text range. If you specify nstartchar = 0
Nendchar =-1 indicates all the selected text. Void replacesel (lpctstr lpsznewtext, bool bcanundo = false) can replace the selected text with the specified text.
In addition, the input box also has some functions related to the clipboard, voidclear (); Delete the selected text, void copy (); you can send the selected text to the clipboard, void paste (); insert the content in the clipboard to the cursor position in the current input box. Void cut () is equivalent to combining copy and clear.
Finally, we will introduce several common message ing Macros in the input box:
The on_en_change field is generated after the text is updated.
On_en_errspace is generated when the input box cannot allocate memory
On_en_killfocus/on_en_setfocus is generated when the input box loses/gets the input focus
The preceding message ing methods are used to define the function of the prototype afx_msgvoid memberfxn (); and the message ing in the form of on_notification (ID, memberfxn. If the input box is used in the dialog box, class wizard automatically lists related messages and generates message ing code.
Scroll bar
Generally, scroll bar is not used independently, because spinctrl can replace a part of the scroll bar, but if you need to generate a derived window by yourself, the scroll bar will be useful. To create a scroll bar, you can use the member function ::
Bool cedit: Create (lpctstr lpsztext, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint nid = 0 xFFFF );
Dwstyle indicates the style of the window. In addition to ws_child and ws_visible, you can specify a special style for the scroll bar.
After creating a scroll bar, call voidsetscrollrange (INT nminpos, int nmaxpos, bool bredraw = true) to set the scroll range,
Int getscrollpos ()/INT setscrollpos () is used to obtain and set the position of the current scroll bar.
Void showscrollbar (bool bshow = true); used to display/hide the scroll bar.
Bool enablescrollbar (uint narrowflags = esb_enable_both) is used to set whether the arrow on the scroll bar is allowed. The following values can be used for narrowflags:
Both the esb_enable_both arrows are allowed.
The/left arrow on esb_disable_ltup is disabled.
The lower/right arrow of esb_disable_rtdn is disabled.
Both the esb_disable_both arrows are disabled.
If you need to be notified when the scroll bar position is changed, you need to define a ing for the message wm_vscroll/wm_hscroll in the parent window. The method is to overload in the parent window class.
Afx_msg void onvscroll (uint nsbcode, uint NPOs, cscrollbar * pscrollbar)/afx_msg void onhscroll (uint nsbcode, uint NPOs, cscrollbar * pscrollbar)
The message ing macro used is on_wm_vscroll () and on_wm_hscroll (). You do not need to specify the ID of the scroll bar in the ing macro because all the scroll messages of the scroll bar are processed by the same function. The third parameter in onhscroll/onvscroll specifies the pointer to the current scroll bar. The first parameter indicates the action on the scroll bar. the following values are recommended:
Sb_top/sb_bottom has been rolled to the top/Bottom
Sb_lineup/sb_linedown scroll up/down a row
Sb_pagedown/sb_pageup scroll up/down one page
Sb_thumbposition/sb_thumbtrack scroll bar is dragged to a certain position. The NPOs parameter specifies the current position (The NPOs parameter is invalid in other cases)
Sb_endscroll scroll bar dragged (the user releases the mouse)
List box/check list box
The ListBox window is used to list a series of texts, each of which occupies one line. To create a list window, you can use the member function:
Bool clistbox: Create (lpctstr lpsztext, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint nid = 0 xFFFF );
Dwstyle indicates the style of the window. In addition to the commonly used style ws_child and ws_visible of the sub-window, you can specify a special style for the list control.
Lbs_multiplesel indicates that multiple rows can be selected simultaneously in the list box.
Lbs_extendedsel you can press shift/CTRL to select multiple rows
All lbs_sort rows are sorted alphabetically.
After the list box is generated, you must add or delete rows to or from the list box. You can use:
Int addstring (lpctstr lpszitem) to Add rows,
Int deletestring (uint nindex) deletes a specified row,
Int insertstring (INT nindex, lpctstr lpszitem) inserts the row into the specified position.
Void resetcontent () can delete all rows in the list box.
Call int getcount () to obtain the number of rows in the current list box.
To obtain/set the currently selected row, call int getcursel ()/INT setcursel (INT iindex ). If you specify the multi-row style, you need to call int getselcount () to obtain the number of selected rows, and then intgetselitems (INT nmaxitems, lpint rgindex) obtain all the selected rows. The rgindex parameter is an array that stores the selected rows. By calling int
Getlbtext (INT nindex, lptstr lpsztext) returns the string of the specified row in the list box.
In addition, you can call int findstring (INT nstartafter, lpctstr lpszitem) to find the specified character transfer position in all current rows. nstartafter indicates to start searching from that row.
Int selectstring (INT nstartafter, lpctstr lpszitem) can be used to select rows containing the specified string.
Added the cchecklistbox class in MFC 4.2, which is derived from clistbox and has all functions of clistbox. The difference is that a check box can be added before each row. Note that the lbs_ownerdrawfixed or lbs_ownerdrawvariable style must be specified during creation.
You can use void setcheckstyle (uint nstyle)/uintgetcheckstyle () to set/obtain the style of the check box.
You can use void setcheck (INT nindex, intncheck)/INT getcheck (INT nindex) to set and obtain the check status of a row.
At last, we will introduce several common message ing Macros in the list box:
On_lbn_dblclk double-click
On_en_errspace is generated when the input box cannot allocate memory
On_en_killfocus/on_en_setfocus is generated when the input box loses/gets the input focus
The row selected by on_lbn_selchange changes.
The preceding message ing methods are used to define the function of the prototype afx_msgvoid memberfxn (); and the message ing in the form of on_notification (ID, memberfxn. If the list box is used in the dialog box, class wizard automatically lists related messages and generates message ing code.