CLR custom. Net Control creation (4) -- Response to mouse messages

Source: Internet
Author: User

When the custom view in step 3 is implemented, the original view is overwritten directly.ControlView, resulting in the originalControlThe mouse message cannot be captured, so we need to add some methods to implement the message response.

You can use the message sending method to respond to the mouse message,

Myview. cppFile implementation:

 1   # Include  "  Stdafx. h  "  
2 # Ifdef _ debug
3 # Define New debug_new
4 # Endif
5 # Include " Myview. h "
6
7 Implement_dyncreate (cmyview, cview)
8
9 Begin_message_map (cmyview, cview)
10 On_wm_lbuttondown ()
11 On_wm_lbuttonup ()
12 On_wm_rbuttondown ()
13 On_wm_rbuttonup ()
14 On_wm_mousemove ()
15 On_wm_contextmenu ()
16 On_wm_mbuttondown ()
17
18 End_message_map ()
19
20 Cmyview: cmyview ()
21 {
22 // Todo: Add a structure hereCode
23 M_parenthwnd = NULL;
24 M_pcallback = NULL;
25 }
26
27 Cmyview :: ~ Cmyview ()
28 {
29 }
30
31 Void Cmyview: onlbuttonup (uint nflags, cpoint point)
32 {
33 Cmyview: onlbuttonup (nflags, point );
34 // Todo: Add message processing hereProgramCode and/or call Default Value
35 Long x = Point. X;
36 Long y = Point. Y;
37 Lparam = Makelparam (x, y );
38 : Sendmessage (m_parenthwnd, wm_lbuttonup, 0 , Lparam );
39 }
40 // Other mouse response functions are omitted.
41 // ......

In Myviewcontrol. h Response in the designerMousedownEvent, in System :: void myviwecontrol : myviwecontrol _ mousedown ( System : Object ^ sender , System :< span style =" color: #010001; "> Windows :< span style =" color: #010001; "> forms :< span style =" color: #010001; "> mouseeventargs ^ E ) The corresponding code is added, and the response will be returned, because onlyMousedown,Therefore, to determine whether the left mouse button or right-click, use the ParameterE.

 

For the view created by me, messages from the scroll wheel and keyboard cannot be sent directlyControl, Then I rewrite the message processing function.DefwndprocTo intercept the scroll wheel message.Myviewcontrol. hThe Code is as follows:

 1   Void Myviwecontrol: defwndproc (System: Windows: forms: Message  %  M)
2 {
3 Switch (M. msg)
4 {
5 // Trigger keyboard drop message
6 Case Wm_keydown:
7 {
8 This -> Focus ();
9 Switch (M. wparam. toint32 ())
10 {
11 Case Vk_f5:
12 MessageBox: Show ( " F5 " );
13 Break ;
14
15 Default :
16 Break ;
17 }
18 }
19 Case Wm_keyup:
20 {
21 }
22 Break ;
23 // Trigger the scroll wheel message
24 Case Wm_mousewheel:
25 {
26 Short Zdelta = Get_wheel_delta_wparam (M. wparam. toint32 ()); // Determine scroll wheel direction
27 Long Xpos = Get_x_lparam (M. lparam. toint32 ()); // Returns the X value of the current coordinate.
28 Long Ypos = Get_y_lparam (M. lparam. toint32 ()); // Returns the X value of the current coordinate.
29
30 }
31 Break ;
32
33 Default :
34 Break ;
35 }
36 Usercontrol: defwndproc (m );
37 }

In this way, a rough custom view is complete, and further improvements and optimizations will be made later.

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.