Jump to content


Check out our Community Blogs

LogicKills

Member Since 25 May 2008
Offline Last Active May 23 2010 05:44 PM
-----

Topics I've Started

Moving a window dynamically and have it react to collisions

01 June 2009 - 08:14 PM

I am sure you are all aware of the code floating around that moves your mouse around your screen and has it bounce off the sides making beeps as it does. It is a very easy process and requires very little knowledge. What I am trying to accomplish is designing a different implementation. Instead of moving the cursor. I want it moving a window.

I have yet to add in the collision detection, I plan on using
int screenX = GetSystemMetrics(SM_CXSCREEN);
    int screenY = GetSystemMetrics(SM_CYSCREEN);

Anywhoo, this is my try at designing the moving window program. Any help/tip/code snippets would be quite helpful. Thanks

#include <windows.h>
#include <math.h>

int newX(int oldx);
int newY(int oldy);
double SmoothMoveELX(double x);
int main()
{

    int lengthInMs = 10*1000;
    HWND notepad = FindWindow("Notepad",NULL);
    RECT window;
    SetTimer(
            notepad,
            NULL,
            30,
            (TIMERPROC)NULL
            );
    int startTime = GetTickCount();
    int pos = elap / lengthInMs;


    while(1)
    {
            RECT window;
            GetWindowRect(notepad,&window);
            int elap = (GetTickCount() - startTime);
            if(elap  >= lengthInMs)
            {
                  int NEWX = NewX(window.x);
                  int NEWY = NewY(window.y);
                  MoveWindow(
                             notepad,
                             NEWX,
                             NEWY,
                             100,
                             100,
                             TRUE
                             );  
                             }
                             }
} 

int NewX(int oldx)
{
    int newx = oldx*(1-SmoothMoveELX(pos))
             + 10 *SmoothMoveELX(pos));

    return newx;
}

int newY(int oldy)
{
    int newy = oldy*(1-SmoothMoveELX(pos))
             + 10 *SmoothMoveELX(pos));
    return newy;
}

double SmoothMoveELX(double x)
{
       double PI = Atan(1) * 4;
       return ((cos(1 - x) * PI + 1) /2 )
}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download