Advertisement

Move bmp

Started by March 11, 2002 11:50 AM
25 comments, last by Mullvad 22 years, 8 months ago
I have created a bmp picture on my window using this code
    

       case WM_PAINT:
    {
        BITMAP bm;
        PAINTSTRUCT ps;

        HDC hdc = BeginPaint(hwnd, &ps);

        HDC hdcMem = CreateCompatibleDC(hdc);
        hbmOld = SelectObject(hdcMem, g_hbmBall);

        GetObject(g_hbmBall, sizeof(bm), &bm);

        BitBlt(hdc, test, test2, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

        SelectObject(hdcMem, hbmOld);
        DeleteDC(hdcMem);

        EndPaint(hwnd, &ps);
    }
    test = 100;
    test2 = 100;
   break;


//*******************Slut paint********************//

  //***Create***//
		 case WM_CREATE:
      	  g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
       	  if(g_hbmBall == NULL)
          	  MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);
   	  break;
         //***Slut create**//


    
But now I wont the bmp to change position on the window when I press the right arrow key. I think you understand. I am trying to create a Downhill game. Mullvad Edited by - Mullvad on March 11, 2002 12:51:25 PM Edited by - Mullvad on March 11, 2002 12:52:16 PM
Mullvad

          case WM_PAINT:          {                 BITMAP bm;         PAINTSTRUCT ps;         HDC hdc = BeginPaint(hwnd, &ps);         HDC hdcMem = CreateCompatibleDC(hdc);         hbmOld = SelectObject(hdcMem, g_hbmBall);           GetObject(g_hbmBall, sizeof(bm), &bm);          BitBlt(hdc, test, test2, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);          SelectObject(hdcMem, hbmOld);         DeleteDC(hdcMem);         EndPaint(hwnd, &ps);    }          test = 100;    

Well, here goes:
In this line

       BitBlt(hdc, test, test2, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);    

the 0,0 are your x and y screen cordinates. So all you have to do is:

A) Include code that handles the windows keyboard messages.
-You do this with a case statement just like you handled WM_PAINT (WM_KEYDOWN in this case); I'd check the MSDEV documentation to find out exactly how to handle keyboard messages if you are unfamilar with them.
B) Inside that code change the x and y coordiates of the image.
C) Redraw the image (clearing the screen if necessary).

Edited by - bob_the_third on March 11, 2002 4:06:10 PM
Advertisement
Thanks I will try that but if wont work I will drag this message up to the top as I did now!! And how do I ReDraw it?

Mullvad

Edited by - Mullvad on March 12, 2002 4:17:34 PM
Mullvad
either you make a timer or you redraw it whenever you press the right arrow key
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Yeah But what CODE do I have to write to REDRAW it??

Mullvad
Mullvad
just send a WM_PAINT message.

LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to send
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);


eg:
SendMessage(hWnd, WM_PAINT, 0, 0);


Tadaa!
[email=webwill666@hotmail.com]Will O'Connor[/email], Flamma Productions.
Advertisement
Something isnt working couse if I press the left arrow key then the bitmap position gets now value and I send a WM_PAINT message but nothing happends if I doesnt minimize the window first and then reizes it why??

Mullvad
Mullvad
Have you sent the correct window handle ?

,Jay
Here is the whole code!


    #include <windows.h>#include "Slalomres.h"const char g_szClassName[] = "myWindowClass";HBITMAP g_hbmBall = NULL;int test, test2;LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){    switch(msg)    {       case WM_PAINT:    {        BITMAP bm;        PAINTSTRUCT ps;        void* hbmOld;        HDC hdc = BeginPaint(hwnd, &ps);        HDC hdcMem = CreateCompatibleDC(hdc);        hbmOld = SelectObject(hdcMem, g_hbmBall);        GetObject(g_hbmBall, sizeof(bm), &bm);        BitBlt(hdc, test, test2, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);        SelectObject(hdcMem, hbmOld);        DeleteDC(hdcMem);        EndPaint(hwnd, &ps);    }   break;     case WM_CREATE:      	  g_hbmBall = LoadBitmap(GetModuleHandle(NULL),   MAKEINTRESOURCE(IDB_BALL));       	  if(g_hbmBall == NULL)          	  MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);   	  break;       case WM_KEYDOWN:       		switch(wParam)            	{						case VK_LEFT:                     test += 10;                     test2 += 10;                     SendMessage(hwnd, WM_PAINT, 0, 0);                     break;               }            break;         case WM_RBUTTONDOWN:         		DeleteObject(g_hbmBall);               break;        case WM_CLOSE:            DestroyWindow(hwnd);        break;        case WM_DESTROY:            PostQuitMessage(0);        break;        default:            return DefWindowProc(hwnd, msg, wParam, lParam);    }    return 0;}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,    LPSTR lpCmdLine, int nCmdShow){    WNDCLASSEX wc;    HWND hwnd;    MSG Msg;        wc.cbSize        = sizeof(WNDCLASSEX);    wc.style         = 0;    wc.lpfnWndProc   = WndProc;    wc.cbClsExtra    = 0;    wc.cbWndExtra    = 0;    wc.hInstance     = hInstance;    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);    wc.lpszMenuName  = NULL;    wc.lpszClassName = g_szClassName;    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);    if(!RegisterClassEx(&wc))    {        MessageBox(NULL, "Window Registration Failed!", "Error!",            MB_ICONEXCLAMATION | MB_OK);        return 0;    }    hwnd = CreateWindowEx(        WS_EX_CLIENTEDGE,        g_szClassName,        "The title of my window",        WS_OVERLAPPEDWINDOW,        CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,        NULL, NULL, hInstance, NULL);    if(hwnd == NULL)    {        MessageBox(NULL, "Window Creation Failed!", "Error!",            MB_ICONEXCLAMATION | MB_OK);        return 0;    }    ShowWindow(hwnd, nCmdShow);    UpdateWindow(hwnd);        while(GetMessage(&Msg, NULL, 0, 0))    {        TranslateMessage(&Msg);        DispatchMessage(&Msg);    }    return Msg.wParam;}    


Mullvad

[edited by - Mullvad on March 16, 2002 12:58:02 PM]
Mullvad
I don''t know if this will help, but try calling:
InvalidateRect(hWnd, NULL, TRUE);
Instead of:
SendMessage(hwnd, WM_PAINT, 0, 0);

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

This topic is closed to new replies.

Advertisement