Child Window control-Windows Programming (blog of Netease small starfish)

Source: Internet
Author: User

Take the window handle of the parent window:
Hwndparent = getparent (hwnd );

Send message:
Sendmessage (hwndparent, message, wparam, lparam );

Predefined controls include:
Button, check box, edit box, list box, drop-down list box, static string label, and scroll bar.

When using a predefined control, you do not have to register a window class for it. The window class already exists in Windows and has a pre-defined name.
You only need to specify the window class name in the createwindow () parameter. The window style parameters of createwindow () accurately define subwindows.
Widget shape and functions. Windows has built-in window message processing programs for these controls.

==========================================================
Button Control

Specify the window class as "button" when creating a subwindow ".

The createwindow parameters of the displayed window are as follows:
Class Name (category name) text ("button ")
Window text (window text) a c string sztext
Window style (window style) ws_child | ws_visible | button style (as described below)
X position (X position)
Y position (Y position)
Width)
Height)
Parent window (parent window)
Child Window ID (subwindow ID) to be converted to the hmenu type. For example (hmenu) I
Instance handle
Extra parameters (additional parameter) null

The button style is as follows:
Bs_pushbutton
Bs_defpushbutton
Bs_checkbox (the status with the check box. Check box must be set to manually send the bm_setcheck message)
Bs_autocheckbox (the status with the check box. check box is automatically set)
Bs_radiobutton (with single-choice button. The status must be set to manually send the bm_setcheck message)
Bs_3state
Bs_auto3state
Bs_groupbox (grouping box. It is just a title box and does not process input .)
Bs_autoradiobutton
Bs_ownerdraw

The button will send the wm_command message to the parent window. parameters:
Loword (wparam) is the Child Window ID.
Hiword (wparam) is the notification code.
Lparam is the sub-window handle.

The notification code hiword (wparam) includes:
Bn_clicked
Bn_paint
Bn_hilite or bn_pushed
Bn_unhilite or bn_unpushed
Bn_disable
Bn_doubleclicked or bn_dblclk
Bn_setfocus
Bn_killfocus

The parent window can also send messages to the button.
Bm_getcheck (selected mark of the check box)
Bm_setcheck
Bm_getstate ("normal" or "pressed ")
Bm_setstate
Bm_setstyle (Change button style)
Bm_click
Bm_getimage
Bm_setimage

To obtain the Control ID, use
Getdlgctrlid (hwndchild );
To obtain the control's subwindow handle, use
Getdlgitem (hwndparent, ID );
 

Used to change the text of a button
Setwindowtext (hwnd, pszstring );
Use the current text of the button
Ilength = getwindowtext (hwnd, pszbuffer, imaxlength );

==============================================
Show/hide subwindows

If the style of the window class in the sub-window does not contain ws_visible, the window will not be displayed before showwindow is called.
Used to display subwindows:
Showwindow (hwndchild, sw_shownormal );
Hide a subwindow:
Showwindow (hwndchild, sw_hide );
To check whether the subwindow is visible:
Iswindowvisible (hwndchild );

Disable/enable subwindow

To make the button unavailable (the text turns gray). Use:
Enablewindow (hwndchild, false );
Restore to available:
Enablewindow (hwndchild, true );
Determine whether it is enabled:
Iswindowenabled (hwndchild );

Input focus

When a user uses a button, the button obtains the input focus, and its parent window loses the input focus. In this case, the parent window receives the wm_killfocus message first.
(The wparam parameter is the handle of the window that obtains the input focus). Then, the window that obtains the input focus (the button subwindow) receives a Wm
_ Setfocus message (the wparam parameter is the handle of the window that loses the input focus ).

Controls and colors

System color
Windows retains 29 system colors for various display purposes (for example, menu color, menu text color, window color, and so on ). You can
Use getsyscolor and setsyscolors to obtain and set these colors. The system color is only available in Windows
Valid during the process.

To display the icon or bitmap in the button, you can use the bs_icon or bs_bitmap style and use the bm_setimage message to set the bitmap.
For bs_ownerdraw style buttons, it allows you to draw buttons completely freely.

==========================================
Static controls

When creating a child window, specify the window class as "static". It neither receives mouse or keyboard input nor sends wm_command messages to the parent window.
Setwindowtext is used to set the text of the static control.

======================================
Scroll bar Control

Specify the window class as "scrollbar" when creating a subwindow ".
It does not send the wm_command message to the parent window, but sends the wm_vscroll and wm_hscroll messages as the window scroll bar does.
You can use the lparam parameter to distinguish between the window scroll bar and the scroll bar control. The value of the subwindow scroll bar is 0, and the value of the scroll bar control is scroll.
The handle of the window. Wparam has the same meaning for the window scroll bar and the scroll bar control.

The function used to set the scroll bar control is the same as that used to set the window scroll bar:
Setscrollrange (hwndscroll, sb_ctl, Imin, IMAX, bredraw );
Setscrollpos (hwndscroll, sb_ctl, IPOs, bredraw );
Setscrollinfo (hwndscroll, sb_ctl, & Si, bredraw );
The difference is that when you set the scroll bar control, the first parameter is the control sub-window handle rather than the parent window handle. The second parameter is sb_ctl instead
Sb_vert or sb_horz.

======================================
Specify window functions for controls

The Window Function of the control is in windows. However, you can use gwl_wndproc as a parameter to get it through the getwindowlong function.
You can use setwindowlong to specify a new window function. For example:
Oldscroll = (wndproc) setwindowlong (hwndscroll, gwl_wndproc,
(Long) scrollproc); // the return value is the address of the original window function.

When you need to use the tab key to switch the input focus between controls, because the control obtains the input focus, all keyboard messages are sent to the control window.
Function. The original window function of the control does not process messages that are pressed by the tab key. In this case, you can use the method above to reset a window for the control.
The interface message processing function processes the tab key, and then calls the original window function in the new window function to process other messages:
Return callwindowproc (oldscroll, hwnd, message, wparam, lparam );
In this way, you can process the tab key.

Just like you can use setwindowlong to reset the window function. You can use setclasslong to set a window.
Something of the interface class. For example, you can reset the painting brush of the window class below:
Setclasslong (hwnd, gcl_hbrbackground,
(Long) createsolidbrush (RGB (color [0], color [1], color [2]);

====================================
Edit controls

Use "edit" when creating a subwindow. For example:
Hwndedit = createwindow (text ("edit"), null,
Ws_child | ws_visible | ws_hscroll | ws_vscroll |
Ws_border | es_left | es_multiline |
Es_autohscroll | es_autovscroll,
0, 0, 0, 0, hwnd, (hmenu) id_edit,
(Lpcreatestruct) lparam)-> hinstance, null );

Edit the widget style:
Text alignment: es_left, es_right, and es_center
Multiple rows: es_multiline (single row by default)
In a single row style, to horizontally scroll the text: es_autohscroll
In a multiline style, you need to scroll the text horizontally (not a scroll bar) (This prevents automatic line feed): es_autohscroll
In a multi-row style, you must scroll the text vertically (not a scroll bar): es_autovscroll
To display a scroll bar in a multiline style, use ws_hscroll and ws_vscroll.
The editing control is missing, saving time and no borders. To display borders, use ws_border
When you select to edit the text of the control, the text is displayed in reverse white. However, after the control loses focus, the text will not be highlighted.
The selected text is still highlighted. Use es_nohidesel.

Edit the control to send the wm_command message to the parent window message handler. The message parameter is
Loword (wparam) subwindow ID
Hiword (wparam) Notification code
Lparam subwindow handle

The notification code is:
The en_setfocus Edit Control has obtained the input focus.
The en_killfocus editing control has lost the input focus.
The content of the en_change edit control changes.
The content of the en_update Edit Control has changed.
The execution of the en_errspace editing control exceeds the intermediate limit.
The en_maxtext editing control exceeds the inserted space.
The horizontal scroll bar of the en_hscroll editing control has been pressed.
The vertical scroll bar of the en_vscroll editing control has been pressed.

To process the tab key and switch the input focus or process the Enter key, you can specify a window function for it and intercept it.

To insert text in the editing area, use:
Getwindowtextlength
Getwindowtext
Setwindowtext

Send a message to the Edit Control:
Sendmessage (hwndedit, wm_cut, 0, 0); // clipboard
Sendmessage (hwndedit, wm_copy, 0, 0); // copy
Sendmessage (hwndedit, wm_clear, 0, 0); // Delete selected text
Sendmessage (hwndedit, wm_paste, 0, 0); // Paste

Sendmessage (hwndedit, em_getsel, (wparam) & istart, (lparam) & iend );
// Obtain the currently selected Start position and end position. (The end position is the position of the last selected text plus 1)

Sendmessage (hwndedit, em_setsel, istart, iend); // select text

Sendmessage (hwndedit, em_replacesel, 0, (lparam) szstring );
// Replace the selected text with other text

Icount = sendmessage (hwndedit, em_getlinecount, 0, 0 );
// Obtain the number of rows of the multi-row editing Control

Ioffset = sendmessage (hwndedit, em_lineindex, iline, 0 );
// For any specific row (starting from 0), you can get the offset from the beginning of the text in the editing buffer.
// If iline is-1, the offset of the row where the cursor is located is returned.

Ilength = sendmessage (hwndedit, em_linelength, iline, 0 );
// Obtain the length of the iline row

Ilength = sendmessage (hwndedit, em_getline, iline, (lparam) szbuffer );
// Copy the row iline to the buffer zone szbuffer

======================================
List controls

When creating a list subwindow, use "ListBox" as the window class.

List style:
Lbs_commany allows the parent window to receive wm_command messages from the List (lack of time does not send wm_command messages to the parent window)
Lbs_sort sorts projects in the List
Lbs_multiplesel lists are multiple choices (single choice by default)
Lbs_noredraw prevents automatic list re-painting when adding a project to the list
Style lbs_standard contains the most common styles: (lbs_notify | lbs_sort | ws_vscroll | ws_border)

Put the string to the list:
Sendmessage (hwndlist, lb_insertstring, I, (lparam) szstring );
The first parameter is the window handle of the list control.
The second parameter lb_insertstring indicates that a project is to be inserted.
The third parameter indicates the position to be inserted. (The position value starts from 0. 0 indicates the first position on the top.-1 indicates the last position on the top)
The fourth parameter is the string to be inserted (the string ends with 0 ).
If the function returns 0, the operation is completed normally.

If the list control contains the lbs_sort style, you can set the 2nd parameters to lb_addstring when inserting a string.
The string is automatically inserted to a position. For example:
Sendmessage (hwndlist, lb_addstring, 0, (lparam) szstring );

To delete a string from the list control, specify the first parameter as lb_deletestring. For example:
Sendmessage (hwndlist, lb_deletestring, iindex, 0 );

To delete all contents in the list, set the 2nd parameter to lb_resetcontent. For example:
Sendmessage (hwndlist, lb_resetcontent, 0, 0 );

When you add or delete a string to or from the list, the list control is automatically repainted by its window function. If you have many strings, you need to add them.
You may want to temporarily block automatic re-painting of the list before all strings are added. This requires:
Sendmessage (hwndlist, wm_setredraw, false, 0 );
After adding the control, you can restore the automatic re-painting of the list control:
Sendmessage (hwndlist, wm_setredraw, true, 0 );

To obtain the number of projects in the List control:
Icount = sendmessage (hwndlist, lb_getcount, 0, 0 );

In the radio list control, select a project that will be highlighted:
Sendmessage (hwndlist, lb_setcursel, iindex, 0 );
// The iindex parameter specifies the items to be selected. specifying-1 indicates canceling all selections.

In the radio list control, you can also select a project based on the first letter of a string. For example:
Iindex = sendmessage (hwndlist, lb_selectstring, iindex,
(Lparam) szsearchstring );
Indicates that the search starts from the iindex position. If the start letter of a project is the same as that of szsearchstring, select the project and
Return the location of the project.-1 is returned if no matching project exists.

In the radio list control, you must obtain the index of the selected project. Use:
Iindex = sendmessage (hwndlist, lb_getcursel, 0, 0 );
If no project is selected, the function returns lb_err (value:-1 ).

In the radio list control, copy a project to a character buffer. Use:
Ilength = sendmessage (hwndlist, lb_gettext, iindex, (lparam) szbuffer );
Copy the items indexed as iindex in the list control to szbuffer. The length of the string ilength is returned.
(To make the szbuffer buffer large enough, you can use lb_gettextlen as a parameter to obtain the string length of the project first .)

For multiple-choice list controls:

You can use lb_setsel to set the selection status of a specific project (it does not affect the selection status of other projects ):
Sendmessage (hwndlist, lb_setsel, wparam, iindex );
If the wparam parameter is set to 0, deselect the option. If it is set to-1, select/cancel all projects. If it is set to another value, select the iindex project.

View the selection status of a project:
Iselect = sendmessage (hwndlist, lb_getsel, iindex, 0 );
// If the project is selected, no value is returned. Otherwise, no value is returned.

 

To use the configuration control to obtain the input focus:
Setfocus (hwndlist );

The message sent by the configuration control to the parent window is wm_command. The parameters are as follows:
Loword (wparam) subwindow ID
Hiword (wparam) Notification code
Lparam subwindow handle
 
The value of the notification code is as follows:
Lbn_errspace-2 indicates that the execution space has exceeded.
Lbn_selchange 1 indicates that the current selection has been changed
Lbn_dblclk 2 indicates that a project has been double-clicked.
Lbn_selcancel 3
Lbn_setfocus 4
Lbn_killfocus 5
 
The configuration control sends lbn_selchange and lbn_dblclk to the parent window only when the Configuration window style includes lbs_notify.

File List
Enter the file directory list in the list. Use:
Sendmessage (hwndlist, lb_dir, iattr, (lparam) szfilespec );
// The iattr parameter is the file Property Code, and its low byte is the file Property Code:
Iattr Value Attribute
Ddl_readwrite 0x0000 common file
Ddl_readonly 0x0001 read-only file
Ddl_hidden 0x0002 hide a file
Ddl_system 0x0004 system files
Ddl_directory 0x0010 subdirectory
Ddl_archive 0x0020 archive Archives
// The high byte provides additional control over required items:
Iattr Value Attribute
Ddl_drives 0X4000 includes disk drive handle
Ddl_exclusive 0x8000 mutual exclusion search

Related Article

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.