A simple program on the scroll bar has some practical significance.

Source: Internet
Author: User

# Include <windows. h> <br/> int idfocus; <br/> wndproc oldscroll [3]; <br/> lresult callback wndproc (hwnd, uint, wparam, lparam ); <br/> lresult callback scrollproc (hwnd, uint, wparam, lparam); </P> <p> int winapi winmain (hinstance, hinstance hprevinstance, lpstr szcmdline, int icmdshow) <br/>{< br/> static tchar szappname [] = text ("control"); <br/> hwnd; <br/> MSG; <br/> wndclass; <br/> Wndclass. style = cs_hredraw | cs_vredraw; <br/> wndclass. lpfnwndproc = wndproc; <br/> wndclass. cbclsextra = 0; <br/> wndclass. cbwndextra = 0; <br/> wndclass. hinstance = hinstance; <br/> wndclass. hicon = loadicon (null, idi_application); <br/> wndclass. hcursor = loadcursor (null, idc_arrow); <br/> wndclass. hbrbackground = createsolidbrush (0); <br/> wndclass. lpszmenuname = NULL; <br/> wndclass. lpszclassname = szappname; </P> <P> If (! Registerclass (& wndclass) <br/>{< br/> MessageBox (null, text ("this programe requires Windows NT"), szappname, mb_iconerror ); <br/> return 0; <br/>}</P> <p> hwnd = createwindow (szappname, <br/> text ("control scroll "), <br/> ws_overlappedwindow, <br/> cw_usedefault, <br/> null, <br/> null, <br/> hinstance, <br/> null <br/>); <br/> showwind Ow (hwnd, icmdshow); <br/> updatewindow (hwnd); </P> <p> while (getmessage (& MSG, null, 0, 0 )) <br/>{< br/> translatemessage (& MSG); <br/> dispatchmessage (& MSG); <br/>}</P> <p> return MSG. wparam; <br/>}</P> <p> lresult callback wndproc (hwnd, uint message, wparam, lparam) <br/>{< br/> HDC; <br/> paintstruct pS; <br/> static rect; <br/> hinstance; <br/> static int I, cxclient, Cyclient, cychar, color [3]; <br/> static tchar * szcolorlabel [] = {text ("red"), text ("green "), text ("blue") }; <br/> static colorref RGB [3] = {RGB (0,255, 0), RGB (, 0), RGB (, 0, 255) }; <br/> static hbrush [3], hbrushstatic; <br/> static hwnd hwndscroll [3], hwndlabel [3], hwndvalue [3], hwndrect; <br/> tchar szbuffer [10]; </P> <p> switch (Message) <br/> {<br/> case wm_create: <br/> hinstance = (hinstance) getwind Owlong (hwnd, gwl_hinstance); <br/> // obtain the hinstance of the program window handle <br/> // getwindowlong (hwnd, INT) used to obtain information about Windows related to hwnd </P> <p> hwndrect = createwindow (text ("static"), null, ws_visible | ws_child | ss_whiterect, 0, 0, 0, 0, hwnd, (hmenu) 9, hinstance, null); <br/> // create a static style window of the ss_whiterect type, as background </P> <p> for (I = 0; I <3; I ++) <br/> {<br/> hwndscroll [I] = createwindow (text ("scrollbar"), null, ws_child | ws_visible | ws_tabstop | sbs_vert, HW Nd, (hmenu) I, hinstance, null); <br/> // create three windows of the scrollbar type </P> <p> setscrollrange (hwndscroll [I], sb_ctl, 0,255, false); <br/> // scrollbar range: 0 to 255 </P> <p> setscrollpos (hwndscroll [I], sb_ctl, 0, false); <br/> // set its initial position to 0 </P> <p> hwndlabel [I] = createwindow (text ("static "), szcolorlabel [I], ws_child | ws_visible | ss_center, hwnd, (hmenu) (I + 3), hinstance, null ); // ss_center indicates text center <br/> // create three subwindows of tag type as the recording color tag </P> <P> hwndvalue [I] = createwindow (text ("static"), text ("0"), ws_child | ws_visible | ss_center, 0, 0, 0, 0, hwnd, (hmenu) (I + 6), hinstance, null); <br/> // create three subwindows of label type, tag used to record the actual RGB values </P> <p> oldscroll [I] = (wndproc) setwindowlong (hwndscroll [I], gwl_wndproc, (long) scrollproc ); <br/> // Add the wndproc function for the three scrollbar windows, obtain the wndproc function in the previous scrollbar window </P> <p> hbrush [I] = createsolidbrush (RGB [I]); <br/> // create three basic color paint brushes </P> <p >}</P> <p> hbrus Hstatic = createsolidbrush (getsyscolor (color_btnhighlight); </P> <p> cychar = hiword (getdialogbaseunits (); </P> <p> return 0; </P> <p> case wm_size: <br/> cxclient = loword (lparam); <br/> cyclient = hiword (lparam ); <br/> setrect (& rect, cxclient/, cxclient, cyclient); </P> <p> movewindow (hwndrect, cxclient/2, cyclient, true ); </P> <p> for (I = 0; I <3; I ++) <br/> {<br/> movewindow (hwndscroll [I], (2 * I + 1) * cxclient/14,2 * Cychar, cxclient/14, cyClient-4 * cychar, true); <br/> movewindow (hwndlabel [I], (4 * I + 1) * cxclient/28, cychar/2, cxclient/7, cychar, true); <br/> movewindow (hwndvalue [I], (4 * I + 1) * cxclient/28, cyClient-3 * cychar/2, cxclient/7, cychar, true); <br/>}< br/> // specifies the scrollbar initialization position </P> <p> setfocus (hwnd ); <br/> // focus on the main window <br/> return 0; </P> <p> case wm_setfocus: <br/> setfocus (hwndscroll [idfocus]); <br/> return 0; </P> <p> case Wm_vscroll: // scrollbar Control <br/> I = getwindowlong (hwnd) lparam, gwl_id); <br/> switch (loword (wparam )) <br/>{< br/> case sb_pagedown: <br/> color [I] + = 15; </P> <p> case sb_linedown: <br/> color [I] = min (255, color [I] + 1); <br/> break; </P> <p> case sb_pageup: <br/> color [I]-= 15; </P> <p> case sb_lineup: <br/> color [I] = max (0, color [I]-1); <br/> break; </P> <p> case sb_top: <br/> color [I] = 0; <br/> break; </P> <p> Ca Se sb_bottom: <br/> color [I] = 255; <br/> break; </P> <p> case sb_thumbposition: <br/> case sb_thumbtrack: <br/> color [I] = hiword (wparam); <br/> break; </P> <p> default: <br/> break; </P> <p >}< br/> setscrollpos (hwndscroll [I], sb_ctl, color [I], true); <br/> wsprintf (szbuffer, text ("% I"), color [I]); <br/> setwindowtext (hwndvalue [I], szbuffer); </P> <p> deleteobject (hbrush) setclasslong (hwnd, gcl_hbrbackground, (long) Createsolidbrush (RGB (color [0], color [1], color [2]); </P> <p> invalidaterect (hwnd, & rect, true); </P> <p> return 0; </P> <p> case wm_ctlcolorscrollbar: <br/> I = getwindowlong (hwnd) (lparam ), gwl_id); <br/> return (lresult) hbrush [I]; </P> <p> case wm_ctlcolorstatic: <br/> I = getwindowlong (hwnd) lparam, gwl_id); </P> <p> if (I> = 3 & I <= 8) <br/>{< br/> settextcolor (HDC (wparam ), RGB [I % 3]); <br/> setbkcolor (HDC (wparam), Getsyscolor (color_btnhighlight); <br/> // subclass of the window, and set "Hook" for the existing Window Process </P> <p> return (lresult) hbrushstatic; </P> <p >}< br/> break; </P> <p> case wm_syscolorchange: <br/> deleteobject (hbrushstatic ); <br/> hbrushstatic = createsolidbrush (getsyscolor (color_btnhighlight); <br/> return 0; </P> <p> case wm_destroy: <br/> deleteobject (hbrush) <br/> setclasslong (hwnd, gcl_hbrbackground, (long) getstockobject (white_brush ); <Br/> for (I = 0; I <3; I ++) <br/> deleteobject (hbrush [I]); </P> <p> deleteobject (hbrushstatic); </P> <p> postquitmessage (0); <br/> return 0; </P> <p >}< br/> return defwindowproc (hwnd, message, wparam, lparam ); <br/>}</P> <p> lresult callback scrollproc (hwnd, uint message, wparam, lparam) <br/>{< br/> int id = getwindowlong (hwnd, gwl_id); <br/> switch (Message) <br/>{< br/> case wm_keydown: <br /> If (wparam = vk_tab) <br/> setfocus (getdlgitem (getparent (hwnd), (ID + (getkeystate (vk_shift) <0? 2:1) % 3); <br/> break; </P> <p> case wm_setfocus: <br/> idfocus = ID; <br/> break; <br/>}</P> <p> return callwindowproc (oldscroll [ID], hwnd, message, wparam, lparam ); <br/> // use the old window callback function for other messages <br/>}< br/>

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.