3 Replies - 4712 Views - Last Post: 21 May 2012 - 11:11 AM Rate Topic: -----

#1 hcmar2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-May 12

Hover on C# Windows Form ( Flash Menu type)

Posted 21 May 2012 - 09:38 AM

Hello, i have coded for a menu on Windows form but i encountered some problems. here is the code.
I am trying to achieve something like this My link. something similar to flash menu which zoom the object when mouse over.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Image temprory = null;
        PictureBox[] picBox;
        public Form1()
        {
            InitializeComponent();
            temprory = pictureBox1.Image;

            Point upperLeftCorner = pictureBox1.Location;

            Point current = this.Cursor.HotSpot;
            int initWidth = 100;
            int initHeight = 170;
            temprory = Image.FromFile("d:\\one.jpeg");
            picBox = new PictureBox[3];
            //for creating 3 picture boxes array
            for (int i = 0; i < 3; i++)
            {
                picBox[i] = new PictureBox();
                picBox[i].Image = (Image)temprory.Clone();
                initWidth += 130;
                picBox[i].Location = new Point(initWidth, initHeight);
                picBox[i].Visible = true;
                picBox[i].Height = 128;
                picBox[i].Width = 128;
                this.Controls.Add(picBox[i]);
                picBox[i].MouseMove += new MouseEventHandler(picbox_mousemove);
                picBox[i].MouseHover += new EventHandler(picbox_MouseHover);
                picBox[i].Name = "P" + i;
            }

        }

        // for tooltip on mouse hover on each of the picboxes
        void picbox_MouseHover(object sender, EventArgs e)
        {
            PictureBox p = sender as PictureBox;
            ToolTip tt = new ToolTip();
            tt.SetToolTip(p, p.Name);
        }





        // To calculate current cursor position when mouse is over any of the picture boxes and send it to the calculate function which calculates new size of the icon

        public void picbox_mousemove(object sender, MouseEventArgs e)//
        {
            PictureBox p = sender as PictureBox;
            int xpoint = p.Location.X + e.X;

            calculate(xpoint);

        }




        //On mouse move calculate new size of the icons w.r.t current position of the cursor 
        //private void Form1_MouseMove(object sender, MouseEventArgs e)
        private void calculate(int x)
        {
            //PictureBox p = sender as PictureBox;//
            for (int j = 0; j < 3; j++)
            {
                Point upperLeftCorner = picBox[j].Location;//pixel position of top left corner of picture box
                float xOrigin = upperLeftCorner.X + (picBox[j].Width / 2);//to find the middle point along x axis for picture box
                //float dx = e.X - xOrigin;//to calculate distance b/w current position of cursor and mid point of picture box
                float dx = x - xOrigin;
                //if (p.Name != null) { MessageBox.Show(p.Name); }
                int distance;
                if (dx < 0)
                {
                    distance = (int)(100 + dx);//distance of current cursor position from xorigin if cursor is to the left of the picture box
                }
                else
                {
                    distance = (int)(100 - dx);//distance of current cursor position from xorigin if cursor is to the right of the picture box
                }
                if (distance > 0 && distance < 75)// to make sure that height and width does'nt increase if mouse is too far from picture box
                {

                    int nHeight = (int)(distance * pictureBox1.Image.Height) / 100;//% zoom calculation
                    int nWidth = (int)(distance * pictureBox1.Image.Width) / 100;

                    if (48 + nHeight < 100)//defining max zoomed in size of image
                    {

                        Image temp = new Bitmap(temprory, 48 + nHeight, 48 + nWidth);//using base image to perform the zoomin zoomout so as to avoid decrease in quality
                        picBox[j].Image = temp;//giving picture box  resized image
                    }

                }
            }
        }

  
    }
}

This post has been edited by tlhIn`toq: 21 May 2012 - 09:45 AM
Reason for edit:: highlight the lines THEN click the code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Hover on C# Windows Form ( Flash Menu type)

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,652
  • Joined: 02-June 10

Re: Hover on C# Windows Form ( Flash Menu type)

Posted 21 May 2012 - 09:47 AM

Is there a question in there somewhere?


What does "I need help" and "I'm having trouble" mean?
  • You are getting an error? What does this error mean?
  • Your code isn't doing what you expect it to?
  • you want someone to design your algorithm for you because you can't think of a way it should work?


In other words... You have to actually tell us what is going wrong if you expect us to be able to help you. We aren't standing over your shoulder looking at your project.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,652
  • Joined: 02-June 10

Re: Hover on C# Windows Form ( Flash Menu type)

Posted 21 May 2012 - 09:57 AM

015 PictureBox[] picBox;
I would recommend you get away from the primitive collections like arrays and take advantage of more advanced types like List<>. They allow you to create a collection without pre-defining the size... ability to .RemoveAt() and so on.

026 temprory = Image.FromFile("d:\\one.jpeg");
You should read the MSDN description for Image.FromFile().
This command keeps a constant link to the file on the harddrive until object linked to it is disposed of. Generally that isn't desireable. You should either .Clone the image into a new object or read the file as a byte[] then make a new image. Either will get you an image object that isn't linked to the HDD file.

Why are you micromanaging the mouse move? You can subscribe to the .MouseOver event and react accordingly.

For that matter it looks a lot like your application is micromanaging all the behavior for all the menu items. That's a lot of extra work and doesn't really follow the OOP concept. I would make a smarter menuItem class and give it all the behavior and events you need. Then you just make a collection of them. Have each menuItem take care of itself.

Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Was This Post Helpful? 0
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: Hover on C# Windows Form ( Flash Menu type)

Posted 21 May 2012 - 11:11 AM

I posted this in your last attempt at this question, don't know if you ever investigated it.

You should look at PictureBoxSizeMode zoom.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1