Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Shake Logic.

Shake Logic.

Scheduled Pinned Locked Moved C#
csshelp
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Baeltazor
    wrote on last edited by
    #1

    Hey all, in my application, I came up with an idea. I want to be able to shake the window to be able to perform a common function that would normally require atleast a few mouse clicks. And depending on how long the user shook the window, the resulting function will differ, accordingly. I've already started coding (The planning stage made it even harder, so I decided to get my hands a little dirty). But the code that I have now, is so cra& that I'm too embarrassed to put it here for everyone to see lol. Here's what I've trying to accomplish: Determine whether the user shook, or moved the window. If shook: then see how long it was shaken for. if shaken for less than one second: perform function 1. if shaken for 2seconds: perform function 2. if shaken for 3seconds: perform function 3. If the window did not shake, instead it was just moved to another spot, then don't do anything. I'm not asking for code, but if somebody does know of any tutorials, samples, or articles that may be of help, please do share :-) I have searched on Google etc, and I've only been able to find one article and about 50trillion sites that refer to that one article (and I can't use it since it only work on Windows 7) I've tried it and the bugger just won't work on XP. (or my other computer that runs Vista). I'd appreciate any help I can get. Thanks everyone. :)

    Regards, Jason Pezzimenti.


    C C 3 Replies Last reply
    0
    • B Baeltazor

      Hey all, in my application, I came up with an idea. I want to be able to shake the window to be able to perform a common function that would normally require atleast a few mouse clicks. And depending on how long the user shook the window, the resulting function will differ, accordingly. I've already started coding (The planning stage made it even harder, so I decided to get my hands a little dirty). But the code that I have now, is so cra& that I'm too embarrassed to put it here for everyone to see lol. Here's what I've trying to accomplish: Determine whether the user shook, or moved the window. If shook: then see how long it was shaken for. if shaken for less than one second: perform function 1. if shaken for 2seconds: perform function 2. if shaken for 3seconds: perform function 3. If the window did not shake, instead it was just moved to another spot, then don't do anything. I'm not asking for code, but if somebody does know of any tutorials, samples, or articles that may be of help, please do share :-) I have searched on Google etc, and I've only been able to find one article and about 50trillion sites that refer to that one article (and I can't use it since it only work on Windows 7) I've tried it and the bugger just won't work on XP. (or my other computer that runs Vista). I'd appreciate any help I can get. Thanks everyone. :)

      Regards, Jason Pezzimenti.


      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You want to handle the event when the form moves, and then track movement to determine if it's a shake and use a timer to work out when the shaking stops.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      B 1 Reply Last reply
      0
      • C Christian Graus

        You want to handle the event when the form moves, and then track movement to determine if it's a shake and use a timer to work out when the shaking stops.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        B Offline
        B Offline
        Baeltazor
        wrote on last edited by
        #3

        Thank you Christian, I had something similar to that, but I didn't use a timer. I'll give your suggestion a whirl and see how things go. :thumbsup:

        Regards, Jason Pezzimenti.


        1 Reply Last reply
        0
        • B Baeltazor

          Hey all, in my application, I came up with an idea. I want to be able to shake the window to be able to perform a common function that would normally require atleast a few mouse clicks. And depending on how long the user shook the window, the resulting function will differ, accordingly. I've already started coding (The planning stage made it even harder, so I decided to get my hands a little dirty). But the code that I have now, is so cra& that I'm too embarrassed to put it here for everyone to see lol. Here's what I've trying to accomplish: Determine whether the user shook, or moved the window. If shook: then see how long it was shaken for. if shaken for less than one second: perform function 1. if shaken for 2seconds: perform function 2. if shaken for 3seconds: perform function 3. If the window did not shake, instead it was just moved to another spot, then don't do anything. I'm not asking for code, but if somebody does know of any tutorials, samples, or articles that may be of help, please do share :-) I have searched on Google etc, and I've only been able to find one article and about 50trillion sites that refer to that one article (and I can't use it since it only work on Windows 7) I've tried it and the bugger just won't work on XP. (or my other computer that runs Vista). I'd appreciate any help I can get. Thanks everyone. :)

          Regards, Jason Pezzimenti.


          C Offline
          C Offline
          codemunkeh
          wrote on last edited by
          #4

          Any notes on the velocity or vigorousness with which the window would get shaken? As for the checking if it was shaken, you may want to check if the window is within X pixels of it's original location. ResizeStart and ResizeEnd should work good.


          Ninja (the Nerd)
          Confused? You will be...

          B 1 Reply Last reply
          0
          • C codemunkeh

            Any notes on the velocity or vigorousness with which the window would get shaken? As for the checking if it was shaken, you may want to check if the window is within X pixels of it's original location. ResizeStart and ResizeEnd should work good.


            Ninja (the Nerd)
            Confused? You will be...

            B Offline
            B Offline
            Baeltazor
            wrote on last edited by
            #5

            Thank you for the advice Ninja-The-Nerd, I'm a little confused as to why I would need to use ResizeStart and ResizeEnd.. Aren't they only for when the form is resized? Or am I wrong about that?

            Ninja-the-Nerd wrote:

            Any notes on the velocity or vigorousness with which the window would get shaken?

            Well, The shake needs to be quick. Kind of like when you have to shake a Juice bottle before opening it. :)

            Regards, Jason Pezzimenti.


            C 1 Reply Last reply
            0
            • B Baeltazor

              Thank you for the advice Ninja-The-Nerd, I'm a little confused as to why I would need to use ResizeStart and ResizeEnd.. Aren't they only for when the form is resized? Or am I wrong about that?

              Ninja-the-Nerd wrote:

              Any notes on the velocity or vigorousness with which the window would get shaken?

              Well, The shake needs to be quick. Kind of like when you have to shake a Juice bottle before opening it. :)

              Regards, Jason Pezzimenti.


              C Offline
              C Offline
              codemunkeh
              wrote on last edited by
              #6

              ResizeStart will only fire when the user starts to drag the edges (or you use the keyboard "shortcut" of alt+space, s, and arrow keys) and ResizeEnd fires when the user lets go. How to determine whether it was a shake or not, I would judge if the average velocity was, um... If the user was moving it one way then the other then...no, ehh On reflection it might be wiser to simply check if the window is within, say, 20 pixels of it's start location. ResizeStart: note the position, start the timer. ResizeEnd: stop the timer, compare the position, execute the function. Edit. In fact, just ignore this. It's late, I've been coding for several hours. I failed to notice that resize != move. I'll just get my coat and leave...


              Ninja (the Nerd)
              Confused? You will be...

              B 1 Reply Last reply
              0
              • C codemunkeh

                ResizeStart will only fire when the user starts to drag the edges (or you use the keyboard "shortcut" of alt+space, s, and arrow keys) and ResizeEnd fires when the user lets go. How to determine whether it was a shake or not, I would judge if the average velocity was, um... If the user was moving it one way then the other then...no, ehh On reflection it might be wiser to simply check if the window is within, say, 20 pixels of it's start location. ResizeStart: note the position, start the timer. ResizeEnd: stop the timer, compare the position, execute the function. Edit. In fact, just ignore this. It's late, I've been coding for several hours. I failed to notice that resize != move. I'll just get my coat and leave...


                Ninja (the Nerd)
                Confused? You will be...

                B Offline
                B Offline
                Baeltazor
                wrote on last edited by
                #7

                Hmmm... Good work Ninja! :) That was very easy to understand and helpful. I'm workiing on it now and will post the code here once I'm done. Thank you :) Edit: Hahaha, sounds like you need some sleep :P Thanks for your help anyways, it was much appreciated. :D Regards, Jason Pezzimenti.

                1 Reply Last reply
                0
                • B Baeltazor

                  Hey all, in my application, I came up with an idea. I want to be able to shake the window to be able to perform a common function that would normally require atleast a few mouse clicks. And depending on how long the user shook the window, the resulting function will differ, accordingly. I've already started coding (The planning stage made it even harder, so I decided to get my hands a little dirty). But the code that I have now, is so cra& that I'm too embarrassed to put it here for everyone to see lol. Here's what I've trying to accomplish: Determine whether the user shook, or moved the window. If shook: then see how long it was shaken for. if shaken for less than one second: perform function 1. if shaken for 2seconds: perform function 2. if shaken for 3seconds: perform function 3. If the window did not shake, instead it was just moved to another spot, then don't do anything. I'm not asking for code, but if somebody does know of any tutorials, samples, or articles that may be of help, please do share :-) I have searched on Google etc, and I've only been able to find one article and about 50trillion sites that refer to that one article (and I can't use it since it only work on Windows 7) I've tried it and the bugger just won't work on XP. (or my other computer that runs Vista). I'd appreciate any help I can get. Thanks everyone. :)

                  Regards, Jason Pezzimenti.


                  C Offline
                  C Offline
                  codemunkeh
                  wrote on last edited by
                  #8

                  OK sorry for the previous ludicrous suggestion. I once had a borderless window, and to handle the movement, I had added a button which, when dragged, would take the window with it. I know you're trying to reduce clicks but this could work. MouseDown: - set "moving" to true - store mouse-coords in "lastLoc" - store window position to "startPos" - start a timer MouseMove: - if moving, - move window by delta (current location - lastLoc) - update lastLoc MouseUp: - set "moving" to false - stop the timer - compare startPos with current position, etc.


                  Ninja (the Nerd)
                  Confused? You will be...

                  B 1 Reply Last reply
                  0
                  • C codemunkeh

                    OK sorry for the previous ludicrous suggestion. I once had a borderless window, and to handle the movement, I had added a button which, when dragged, would take the window with it. I know you're trying to reduce clicks but this could work. MouseDown: - set "moving" to true - store mouse-coords in "lastLoc" - store window position to "startPos" - start a timer MouseMove: - if moving, - move window by delta (current location - lastLoc) - update lastLoc MouseUp: - set "moving" to false - stop the timer - compare startPos with current position, etc.


                    Ninja (the Nerd)
                    Confused? You will be...

                    B Offline
                    B Offline
                    Baeltazor
                    wrote on last edited by
                    #9

                    That's okay :) Thanks for the quick replys too :) I got a little confused with the mouseup parts since i have a bordered form so i changed it around a little and it actually works. the only problem is that you need to ensure that must stop dragging the form before you release the mouse button :( lol. Here's the code:

                        private int shake = 100;
                    
                        private void Form1\_LocationChanged(object sender, EventArgs e)
                        {
                            progressBar1.Enabled = true;
                            shake = 0;
                            timer1.Enabled = true;
                            timer1.Start();
                        }
                    
                        private void timer1\_Tick(object sender, EventArgs e)
                        {
                            if (shake <= 10 && progressBar1.Value >= 5)
                            {
                                timer1.Stop();
                                progressBar1.Enabled = false;
                    
                                if (Tc.TabPages.Count != 0)
                                {
                                    while (Tc.TabPages.Count != 0)
                                    {
                                        Tc.SelectedTab.Dispose();
                                    }
                                }
                                else
                                {
                                    AddTab();
                                }
                            }
                            else
                            {
                                progressBar1.PerformStep();
                                shake++;
                            }
                        }
                    

                    Edit. The logic in this code is a little conveluded, so message me if it requires explaining :P

                    Regards, Jason Pezzimenti.


                    C 1 Reply Last reply
                    0
                    • B Baeltazor

                      That's okay :) Thanks for the quick replys too :) I got a little confused with the mouseup parts since i have a bordered form so i changed it around a little and it actually works. the only problem is that you need to ensure that must stop dragging the form before you release the mouse button :( lol. Here's the code:

                          private int shake = 100;
                      
                          private void Form1\_LocationChanged(object sender, EventArgs e)
                          {
                              progressBar1.Enabled = true;
                              shake = 0;
                              timer1.Enabled = true;
                              timer1.Start();
                          }
                      
                          private void timer1\_Tick(object sender, EventArgs e)
                          {
                              if (shake <= 10 && progressBar1.Value >= 5)
                              {
                                  timer1.Stop();
                                  progressBar1.Enabled = false;
                      
                                  if (Tc.TabPages.Count != 0)
                                  {
                                      while (Tc.TabPages.Count != 0)
                                      {
                                          Tc.SelectedTab.Dispose();
                                      }
                                  }
                                  else
                                  {
                                      AddTab();
                                  }
                              }
                              else
                              {
                                  progressBar1.PerformStep();
                                  shake++;
                              }
                          }
                      

                      Edit. The logic in this code is a little conveluded, so message me if it requires explaining :P

                      Regards, Jason Pezzimenti.


                      C Offline
                      C Offline
                      codemunkeh
                      wrote on last edited by
                      #10

                      Nope, I get it. After many re-reads... :P What's timer1's Interval set to? You could try to determine if the location has changed since the last Tick event. If it hasn't, the drag is over and it should just tidy up/do nothing. I'd try just now, however that sleep I hoped for is beckoning.


                      Ninja (the Nerd)
                      Confused? You will be...

                      B 1 Reply Last reply
                      0
                      • C codemunkeh

                        Nope, I get it. After many re-reads... :P What's timer1's Interval set to? You could try to determine if the location has changed since the last Tick event. If it hasn't, the drag is over and it should just tidy up/do nothing. I'd try just now, however that sleep I hoped for is beckoning.


                        Ninja (the Nerd)
                        Confused? You will be...

                        B Offline
                        B Offline
                        Baeltazor
                        wrote on last edited by
                        #11

                        Hehe,

                        Ninja-the-Nerd wrote:

                        timer1's Interval

                        Interval: 100; (Default)

                        Ninja-the-Nerd wrote:

                        if the location has changed since the last Tick event. If it hasn't, the drag is over and it should just tidy up/do nothing.

                        Thanks :-D doing it now

                        Ninja-the-Nerd wrote:

                        I'd try just now, however that sleep I hoped for is beckoning.

                        ...Yeah, I like sleeping. Although I have about 10 hours to go til I can sleep again :( :P Thanks for all your help, and if you're goin to sleep now, I'll keep yah updated in this thread thingy-ma-bob :D

                        Regards, Jason Pezzimenti.


                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups