Test the CListBox CEdit self-painted scroll bar

Source: Internet
Author: User

If you need code directly, please go directly to the http://www.codeproject.com/KB/dialog/skinscrollbar.aspx (Note: I am not the author of this Code)

After checking with spy ++, we can see that the clistbox scroll bar is not an independent scroll bar window, but a logical window. Therefore, the subclass method on the scrollbar is not feasible, therefore, you can only block the scrollbar.

Solution 1: Create a cscrollbar on clistbox and change the Z order to make it at the top to block the original scrollbar. Result: failed! The original scroll bar will still appear.

Solution 2: Hide the original scrollbar of ListBox and create a scrollbar outside ListBox.

Step 1: Set clistbox as owner-draw, write some code of the drawitem function, and find that the scroll bar exists.

Step 2: automatically respond to the wm_ncpaint message to block the painting of the scroll bar. You can temporarily block the scroll bar. However, the scroll bar appears again when you scroll the list with the mouse.

Step 3: Respond to the wm_nccalcsize message. After the clistbox: onnccalcsize function is processed, modify the parameters according to the current style, modify the lpncsp-> rgrc [0] rectangle in nccalcsize_params * lpncsp to remove the scroll bar position (I .e., lpncsp-> rgrc [0]. right + = getsystemmetrics (sm_cxvscroll). If it is the left scroll bar, it will be processed according to Law). However, even if the result is left with only two pixel borders, the scroll bar will be drawn to the width of the two pixels, the border is not drawn first. Therefore, the border can only be removed when it is created, that is, ws_border and ws_ex_clientedge are removed.

Step 4: write your own scroll bar CImgScrollBar and rewrite Various Mouse response messages. In addition, you also need to set and obtain the scroll bar data in response to SBM_SETSCROLLINFO and SBM_GETSCROLLINFO messages. Note the relationship between nPage and nMin and nMax in the scrollinfo structure to the slider size and drag value, that is: movable distance = nMax-nMin-nPage + 1. Because the parent window is not a ListBox, you can add a SetTarget function for the scroll bar to specify the sending objects of all WM_VSCROLL and WS_HSCROLL.

Step 5: Because the self-created scroll is not embedded in the window, you need to synchronize the data of the scroll bar from time to maintain the correct size and effect of the scroll bar at any time. Therefore, responding to the WM_ERAZEBKGND message and synchronizing it in the message response is a good method. At the same time, it is also necessary to respond to messages of LB_ADDSTRING and LB_DELETESTRING.

Step 5: Because the ListBox is removed from the border, an additional window is required to draw the border of The ListBox, that is, to create a CWnd class CBorderWnd and set its size to the size of the ListBox, set the size of The ListBox to the size after the border is removed, and move a border distance to get the border effect.

Step 6: rewrite the OnPaint function of CBorderWnd to draw the positions of the square boxes in the lower left and lower right corner, because when the window has both WS_VSCROLL and WS_HSCROLL, the lower left or lower right corner is displayed, CBorderWnd must be used to draw the two corners. However, you must draw the lower-left or lower-right corner based on the current ListBox status, you cannot brush both corners in one brain.

After the above explanations, three feasible solutions are available for consideration: 1. Write your own CImgListBox to control the size, and adjust the position of the scroll bar and CBorderWnd outside the border in OnSize, however, calling the MoveWindow function in OnSize is a big problem, and the condition judgment in OnSize is complicated (WS_HSCROLL, WS_VSCROLL, and the position of the ScrollBar is not yet released? Is the ScrollBar position released? Come on ~~~). 2. Use CBorderWnd as the parent window and place the size and layout adjustment to CBorderWnd for processing. Good IDEA, aha. 3. This is the method used in the Link given at the beginning of the article: instead of removing the position of the scroll bar, it creates a Wnd as its parent window, the Size of your own window is larger than the Size of the parent window. However, if the parent window is used to crop the child window, the scroll bar is blocked, therefore, the general structure is CBorderWnd-> Wnd-> ListBox. The two scrollbars belong to CBorderWnd. The CBorderWnd implements the forwarding of WM_VSCROLL and WS_HSCROLL messages to ListBox. The trouble is that when you use GetDlgItem, you get a CBorderWnd instead of a CListBox object.

The Control Method of Edit is roughly the same as that of ListBox.

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.