Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I am trying to make a musical notation and I have a problem with mouse position .I have some lines as staff .the problem is how can I set mouse movement that the cursor move just on or between lines ,NOT EVERYWHERE. thank you.
C#
public partial class Form2 : Form
   {
       Graphics staff;
       Graphics note;
       Font f1 = new System.Drawing.Font("Polihymnia", 35, FontStyle.Regular);//music font size 35
       public Form2()
       {
           InitializeComponent();
       }

<pre lang="C#">private void Form2_Load(object sender, EventArgs e)
        {
            staff = panel1.CreateGraphics(); 
             note = panel1.CreateGraphics();
                              //------------------------------------------
            PictureBox pb = new PictureBox() { Image = Image.FromFile(@"G:\hh.png") };//cursor _whole note   
            this.Cursor = new Cursor(((Bitmap)pb.Image).GetHicon());
            
                              //------------------------------------------
         
            
        }

        private void staff_show(int x)//
        {
           
            
          

            Pen _pen = new Pen(Color.Black); 
            //---------------------------------------------

            for (int i = x; i <= x + 110; i = i + 110) 
            {
                for (int j = i; j <= i + 40; j = j + 10)
                {
                    staff.DrawLine(_pen, 100, j, 900, j); // staff
                }
            note.DrawString("c", f1, Brushes.Black, 125, i-22); // 4/4 ---------------------------------
            }

            for (int ii = 100; ii <= 900; ii = ii + 200)
            {
                staff.DrawLine(_pen, ii, x, ii, x + 150); // barline
                
            }
            note.DrawString("G", f1, Brushes.Black, 90, x-11); // G clef --------------------------------------------
            note.DrawString("?", f1, Brushes.Black, 90, x+80); // F clef --------------------------------------------
        
           
           
        }

        private void Form2_Shown(object sender, EventArgs e)
        {

            staff_show(100);
            staff_show(340);
            staff_show(580);

        }
      

    }
}


What I have tried:

I design a form with a panel in which I draw some lines as staff and used a music font for draw notes
Posted
Updated 3-May-16 5:50am
v3
Comments
Ralf Meier 3-May-16 12:20pm    
I suppose you want to "draw" the melody with the mouse. If you Click you want to place a dot (a note) a the position.
If I'm right I would suggest the following :
you take the MousePosition a Click-Time and calculate the correct position for the music-note (depending on the MousePosition) and place it in your Music-Chart (depending on the calculated Position).
hojatIzadi 7-May-16 4:00am    
Hi ralf and thank you .can you send an example code please
hojatIzadi 7-May-16 4:09am    
Actually,the problem is How can I input mouse position in my panel that the cursor moves on or between lines .and when user click there the note(character note) write on or between lines(by: {note.DrawString("w", f1, Brushes.Black,e.X ,e.Y );}// "w" is whole note character.
Ralf Meier 9-May-16 12:40pm    
Hi.
I don't have an example for you - only the suggestion. But I could give you more Information :
Perhaps you have a line at y-pos. 100 and another one at y-pos. 150.
Now you want to draw your text. Therefore you have to calculate the real text-height and after this take the half from it.

Now you get a mouse-click at y-position 110. In this case I would set the note on the first line. Your y-position for the text would be 100-textheight/2.
Next time you get a mouse-click at y-position 130. In this case I would set the note between both lines. Your y-position for the text would be 125-textheight/2.
Now you get a mouse-click at y-position 140. In this case I would set the note on the second line. Your y-position for the text would be 150-textheight/2.

The hysteresis for calculation to where the note should be placed on a line is line-location(y) +/-distance between the lines/4.
The hysteresis for calculation to where the note should be placed between the lines is line-location(y) + distance between the lines/2 +/-distance between the lines/4.

Could you follow me ...?


1 solution

I would not recommend to programmatically move a mouse cursor, ever. It can be considered as too intrusive and irritate most users.

However, if you really need to do that, you can do it via the Windows API SendInput:
SendInput function (Windows)[^].

I can be done via P/Invoke. Everything is already done for you: http://www.pinvoke.net/default.aspx/user32.sendinput[^].

Actually, P/Invoke greatly compromise your platform compatibility, but you have no choice; controlling of the cursor is not a part of .NET FCL; anyway, see my first paragraph above. :-)

Your code sample, but my feeling is that you a making too big mistakes: using PictureBox and using CreateGraphics. This is not how graphic rendering is done. But we can discuss it separately; it needs more information of your project.

—SA
 
Share this answer
 
Comments
hojatIzadi 3-May-16 12:18pm    
thank you sergey .I think more about picture box and I will send more information for you
Sergey Alexandrovich Kryukov 3-May-16 13:10pm    
Let's first finish up with mouse control. Will you accept the solution formally?
—SA
hojatIzadi 4-May-16 1:18am    
Ok,I try it.thanke you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900