Windows Develop Bookmark and Share   
 index > Windows Forms General > Need suggestions and ideas for creating a "GadgetBase" and simplifying any coding if possible...
 

Need suggestions and ideas for creating a "GadgetBase" and simplifying any coding if possible...

Ok, so this is going to be a really fun project for me.  Basically I am writing a new gadget base which will serve as the structure for all gadgets created for my company going forward.  This is what I would like to accomplish and would like any suggestions on how to improve or make things quicker or simplify the coding. 

I'm going to create a GadgetBase class that inherits a control, I'm thinking a panel would be best but will need to research some options more than likely.  This will be done using old school windows forms not WPF also.  

For the base I'd like to allow a user to drop the base onto an owning control (form or panel or any container control)  The container is going to dictate the bounds.  The user should be able to click on the control and drag it around the container and if they overlap bounds with dragging the control will snap back so that it's fully visible in the bounds.  In addition to this i'd like to add cool transparency functionality to it such as on hover its alpha comes into effect.  The kicker there is I only want this to happen with the base control.  All controls contained I would like to always have an alpha of 255 while the background (gadgetBase) can have a variable alpha (hopefully that makes sense)  

I have some other ideas but I'd rather get input and see others ideas on this as well.
KarmaKid15  Wednesday, October 07, 2009 2:53 PM

Hi KarmaKid15,

 

> The user should be able to click on the control and drag it around the container and if they overlap bounds with dragging the control will snap back so that it's fully visible in the bounds.

 

I can give you some suggestions with Winform snap control. Please download the sample code from my skydrive.

http://cid-380a93ce0876cf8b.skydrive.live.com/self.aspx/Microsoft%20MSDN%20Work/Solution%20Code/%5E52009.10.8%5E6WinformSnap.zip

 

In this sample, I have used SendMessage to move the control at run time and well calculate the pixel for the snap action. The SetSnap method will do the snap work.

private void SetSnap(Control ctrl, Form containerForm)

        {

            int snapPix = 20;

            if (ctrl.Location.X < snapPix)

            {

                ctrl.Location = new Point(0, ctrl.Location.Y);

            }

            if (ctrl.Location.Y < snapPix)

            {

                ctrl.Location = new Point(ctrl.Location.X, 0);

            }

            if (ctrl.Location.X > (containerForm.Width - snapPix - ctrl.Width - 15))

            {

                ctrl.Location = new Point(containerForm.Width - ctrl.Width - 15, ctrl.Location.Y);

            }

            if (ctrl.Location.Y > (containerForm.Height - snapPix - ctrl.Height - 40))

            {

                ctrl.Location = new Point(ctrl.Location.X, containerForm.Height - ctrl.Height - 37);

            }

        }

 

When you use that method, you can easily call it at the end of each moving action.

void button1_MouseDown(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                ReleaseCapture();

                SendMessage(this.button1.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);

            }

            this.SetSnap(this.button1, this);

        }

Hope this sample gives you some light with snap effect.

 

>In addition to this i'd like to add cool transparency functionality to it such as on hover its alpha comes into effect.

 

For this requirement, I have used GetCursorPos API to get mouse cursor location of the screen and set the Opacity property of the form.

Point p = new Point();

            GetCursorPos(out p);

            if (p.X < this.Location.X || p.X > this.Location.X + this.Width

                || p.Y < this.Location.Y || p.Y > this.Location.Y + this.Height)

            {

                this.Opacity = 0.2;

            }

            else

            {

                this.Opacity = 1;

            }

 

If you have any question with my code, please feel free to tell me.

 

Sincerely,

Kira Qian

Send us any feedback you have about the help from MSFT at fbmsdn[At]microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Thursday, October 08, 2009 5:25 AM

You can use google to search for other answers

Custom Search

More Threads

• Allowing the user to create combo boxes at runtime
• Linq Where and
• PrintPreviewControl connected with WebBrowser control
• Form_KeyDown events are sometimes not firing
• Changing number of characters per tab indent.
• Problem with ToolBarButton and the appendant image
• Tutorial on writing to text files in notepad
• ComboBoxRenderer.DrawTextBox doesn't look right when disabled
• can i map the content at form to the printdocument
• listview problem