Use the C # to implement control to adjust the appearance

xiaoxiao2021-03-06  40

This article mainly discusses how to make dragging when designing the design similar to VS.NET. The specific idea is: making a container control, inheriting from Panel, name is MyPanel; making a control to be dragged, here is inherited from Label, named Mylabel. When running an application, the form loads MYPANEL, MyPanel loads MYLABEL, and MyLabel can be arbitrarily dragged in the container MyPanel.

Inheriting a class from Label, named myLabel, add some initial information code in the initialization function, such as: public mylabel () {this.text = "this is my customer"; this.width = 200; this.backcolor = system .Drawing.color.white;} Declare a Point object, the object (as a variable) decides to move the form in any case. Point mouseoffset; declaration to get an absolute coordinate API.

[DLLIMPORT ("User32.dll")] Static Extern Bool ClientToscreen (INTPTR HWND, Ref Point LP); Rewriting the OnMouseDown method, add the following code: // Number of opposite coordinates of the mouse MouseOffset = New Point (-ex, -ey); rewriting the onmousemove method, add the following code: if (e.button == mousebuttons.Left) {// Get the absolute coordinate value of the parent container location Point LPoint = this.parent.Location; Bool TMP = ClientToscreen (THIS .Parent.handle, ref lpoint; // Get the current mouse relative to the coordinate of the parent container Point MousePoint = new point (control.mouseposition.x-lpoint.x, control.mouseposition.y-lpoint.y); // Mobile Control mousepoint.offset (mouseoffset.x, mouseoffset.y); // Setting position this.location = mousepoint;} Inheriting a class from Panel, this class is a container, named MyPanel Add some initial information code in the initialization function, such as : Public mypanel () {this.width = 400; this.height = 300; this.borderstyle = borderStyle.Fixed3D; this.backcolor = color.gray; this.location = new point (0); this.setStyle ControlStyles.Selectable, true); this.setStyle (ControlStyles.doublebuffer, true); this.setStyle (ControlStyles.Userpaint, true); this.setStyle (ControlStyles.allPaintingInwmpaint, true); THIS.DOCK = DockStyle.Fill; Mylabel label = new mylabel (); label.location = new point (100, 100); this.controls.add (label); mylabel label2 = new mylabel (); label2.location = new point 100,200); this.controls.add (label2);} In the final plus of the initialization function of the Windows Form Code, the following code is added, manually add myPanel object: mypanel panel = new mypanel (); panel.location = new point (0, 0); this.Controls.add (panel); OK, run the object to take the object.

转载请注明原文地址:https://www.9cbs.com/read-70875.html

New Post(0)