Write controls by yourself-I learned to write custom controls for the first time

Source: Internet
Author: User

It has always been a system control and never knows how to write a control ~~~

There are too few online tutorials and I don't know how to start them ~~

You have been using registerclassex + createmediawex to create a masterProgramWindow, but I never know that the control is also written in this way.
It seems that we are always writing controls by ourselves ~~~

The following describes the creation process of the simple function control I wrote for the 1st write control:
1. Write a window class for the control, specify the default window function, and register the window class
2. Call createmediawex to create the child control, which is exactly the same as creating the main window.

Important:
The specified window process must process corresponding messages for its child controls, such as mouse messages and wm_paint ~~~

Khan, 1st times of writing this kind of things, there is no experience, there is nothing to say, just give me a demonstration of not writing controls like me, pasteCode, Not to mention ~~~

The function is as follows: When you click a control, the background color of the control changes ~~~

It doesn't make any special sense. I just want to tell everyone who doesn't know how to write a control ~~~

 //  Testcontrol. c # Include <windows. h> Lresult callback controlwindowproc (hwnd, uint umsg, wparam, lparam ); Void  Registercontrolclass (hinstance) {wndclassexw wce = { 0  }; Wce. cbsize = Sizeof  (Wce); wce. hbrbackground = (Hbrush) getstockobject (white_brush); wce. hinstance = Hinstance; wce. lpfnwndproc = Controlwindowproc; wce. lpszclassname = L "  Controlclass  " ; Wce. Style = Cs_hredraw | Cs_vredraw; registerclassexw ( & Wce);} lresult callback controlwindowproc (hwnd, uint umsg, wparam, lparam ){  Static  Hbrush hbrblack, hbrred;  Switch  (Umsg ){  Case  Wm_create: {hbrblack = Createsolidbrush (RGB ( 0 , 0 , 0 ); Hbrred = Createsolidbrush (RGB ( 255 , 0 , 0  ));  Return   0  ;}  Case  Wm_paint: {paintstruct pS; HDC = Beginpaint (hwnd ,& PS); SelectObject (HDC, hbrblack); rectangle (HDC,  0 ,0 , 100 , 50  ); Endpaint (hwnd, & PS );  Return   0  ;}  Case  Wm_lbuttondown: {HDC; HDC = Getdc (hwnd); SelectObject (HDC, hbrred); rectangle (HDC,  0 , 0 ,100 , 50  ); Settextcolor (HDC, RGB (  255 , 0 , 0  ); Textoutw (HDC,  10 , 10 , L "  Test controls  " , 4  ); Releasedc (hwnd, HDC); setcapture (hwnd ); Return   0  ;}  Case  Wm_lbuttonup: {HDC; HDC = Getdc (hwnd); SelectObject (HDC, hbrblack); rectangle (HDC,  0 , 0 , 100 , 50  ); Settextcolor (HDC, RGB (  0 , 0 ,0  ); Textoutw (HDC,  10 , 10 , L "  Test controls  " , 4  ); Releasedc (hwnd, HDC); releasecapture ();  Return   0  ;}  Case  Wm_destroy: deleteobject (hbrblack); deleteobject (hbrred ); Return   0  ;}  Return  Defwindowprocw (hwnd, umsg, wparam, lparam);} lresult callback mainwindowproc (hwnd, uint umsg, wparam, lparam ){  Switch  (Umsg ){  Case  Wm_create: {hinstance = Getmodulehandlew (null); createwindowexw (  0 , L "  Button " , L "  Buttons  " , Ws_child | ws_visible, 0 , 0 , 100 , 50 , Hwnd, (hmenu) 101  , Hinstance, null); createdomainwexw (  0 , L "  Controlclass  " , L"  Haha  " , Ws_child | ws_visible, 200 , 200 , 100 , 50 , Hwnd, (hmenu) 100  , Hinstance, null); createdomainwexw (  0 , L "  Controlclass  " , L "  Haha " , Ws_child | ws_visible, 400 , 400 , 100 , 50 , Hwnd, (hmenu) 102  , Hinstance, null );  Return   0  ;}  Case  Wm_destroy: postquitmessage (  0  );  Return  0  ;  Default  :  Break  ;}  Return  Defwindowprocw (hwnd, umsg, wparam, lparam );}  Int Winapi wwinmain (hinstance, hinstance hprevinstance, lpwstr lpcmdline, Int  Nshowcmd) {hwnd; wndclassexw wce = { 0  }; MSG; registercontrolclass (hinstance); wce. cbsize =Sizeof  (Wce); wce. hbrbackground = (Hbrush) getstockobject (white_brush); wce. hcursor = Loadcursor (null, idc_arrow); wce. hicon = Loadicon (null, idi_application); wce. hinstance = Hinstance; wce. lpfnwndproc = Mainwindowproc; wce. lpszclassname = L "  Mywindowclass  "  ; Wce. Style = Cs_hredraw |Cs_vredraw; registerclassexw ( & Wce); hwnd = Createdomainwexw ( 0 , L "  Mywindowclass  " , L "  Test  "  , Ws_overlappedwindow, cw_usedefault, null, null, hinstance, null); updatewindow (hwnd); showwindow (hwnd, nshowcmd );  While (Getmessagew (& MSG, null, 0 ,0  ) {Translatemessage ( & MSG); dispatchmessagew ( & MSG );}  Return   0  ;} 

 

If there is a picture with the truth, the font is a little ugly, but the default font of the system is as follows:

 

Girl don't cry @ 02:47:56 @ http://www.cnblogs.com/nbsofer

 

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.