Click here to Skip to main content
15,885,934 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: register window message Pin
xrg_soft@163.com19-Jun-11 17:44
xrg_soft@163.com19-Jun-11 17:44 
GeneralRe: register window message Pin
softwaremonkey19-Jun-11 19:41
softwaremonkey19-Jun-11 19:41 
QuestionMoving Objects in small increments Pin
Cyclone_S19-Jun-11 14:07
Cyclone_S19-Jun-11 14:07 
QuestionRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 15:03
enhzflep19-Jun-11 15:03 
AnswerRe: Moving Objects in small increments Pin
Cyclone_S19-Jun-11 16:03
Cyclone_S19-Jun-11 16:03 
GeneralRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 16:23
enhzflep19-Jun-11 16:23 
GeneralRe: Moving Objects in small increments Pin
Cyclone_S19-Jun-11 16:49
Cyclone_S19-Jun-11 16:49 
GeneralRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 18:24
enhzflep19-Jun-11 18:24 
Sorry about that. Um, I think I understand your intention.

It sounds to me much like a game of pong or similar. Lord knows what I've done with my old code. I can't be bothered even think about the physics right now, but I've thrown together some code that will throw a red dot into a parabolic arc.

In such a path, we'll have to both be able to have small increments of movement, and be able to vary them. You just have to assign the ball's velocity and position in an appropriate way for your application.

Enjoy. Smile | :)

#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Click to release a ball",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
float ballPosX, ballPosY, ballVelocityX, ballVelocityY;
const int myTimerId = 1;
const int intervalMs = 50;

VOID CALLBACK myTimerProc(HWND hwnd,        // handle to window for timer messages
    UINT message,     // WM_TIMER message
    UINT idTimer,     // timer identifier
    DWORD dwTime)     // current system time
{
	ballPosX += ballVelocityX;
	ballPosY += ballVelocityY;
	ballVelocityY -= 0.1;
	InvalidateRect(hwnd, NULL, true);
}

/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;

    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            KillTimer(hwnd, myTimerId);
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);

            SetPixel(hdc, ballPosX, ballPosY, RGB(250,0,0));
            SetPixel(hdc, ballPosX, ballPosY+1, RGB(250,0,0));
            SetPixel(hdc, ballPosX+1, ballPosY, RGB(250,0,0));
            SetPixel(hdc, ballPosX+1, ballPosY+1, RGB(250,0,0));

            EndPaint(hwnd, &ps);
            break;

        case WM_LBUTTONUP:
            ballPosX=0;
            ballPosY=0;
            ballVelocityX=3.2;
            ballVelocityY=8.1;

            KillTimer(hwnd, myTimerId);
            SetTimer(hwnd, myTimerId, intervalMs, myTimerProc);
            break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

GeneralRe: Moving Objects in small increments Pin
enhzflep19-Jun-11 21:35
enhzflep19-Jun-11 21:35 
GeneralRe: Moving Objects in small increments Pin
Cyclone_S20-Jun-11 14:33
Cyclone_S20-Jun-11 14:33 
GeneralRe: Moving Objects in small increments Pin
enhzflep20-Jun-11 18:45
enhzflep20-Jun-11 18:45 
QuestionWSA Events Pin
csrss19-Jun-11 3:32
csrss19-Jun-11 3:32 
AnswerRe: WSA Events Pin
enhzflep19-Jun-11 4:13
enhzflep19-Jun-11 4:13 
GeneralRe: WSA Events Pin
csrss19-Jun-11 5:43
csrss19-Jun-11 5:43 
QuestionRe: WSA Events Pin
Mark Salsbery19-Jun-11 9:38
Mark Salsbery19-Jun-11 9:38 
GeneralRe: WSA Events Pin
csrss19-Jun-11 13:13
csrss19-Jun-11 13:13 
GeneralRe: WSA Events Pin
Mark Salsbery19-Jun-11 14:10
Mark Salsbery19-Jun-11 14:10 
GeneralRe: WSA Events Pin
csrss20-Jun-11 5:46
csrss20-Jun-11 5:46 
GeneralRe: WSA Events Pin
Mark Salsbery20-Jun-11 9:29
Mark Salsbery20-Jun-11 9:29 
GeneralRe: WSA Events Pin
csrss20-Jun-11 10:17
csrss20-Jun-11 10:17 
QuestionHow can I read all files from 'Recent File List' ? Pin
_Flaviu18-Jun-11 8:56
_Flaviu18-Jun-11 8:56 
AnswerRe: How can I read all files from 'Recent File List' ? Pin
Richard MacCutchan18-Jun-11 21:42
mveRichard MacCutchan18-Jun-11 21:42 
GeneralRe: How can I read all files from 'Recent File List' ? [modified] Pin
_Flaviu18-Jun-11 21:45
_Flaviu18-Jun-11 21:45 
GeneralRe: How can I read all files from 'Recent File List' ? Pin
Richard MacCutchan19-Jun-11 21:45
mveRichard MacCutchan19-Jun-11 21:45 
GeneralRe: How can I read all files from 'Recent File List' ? Pin
_Flaviu20-Jun-11 21:28
_Flaviu20-Jun-11 21:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.