Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[c++, winAPI]działanie WM_PAINT

Ostatnio zmodyfikowano 2014-11-16 21:36
Autor Wiadomość
SMTI
Temat założony przez niniejszego użytkownika
[c++, winAPI]działanie WM_PAINT
» 2014-11-16 20:49:38
Od razu przejdę do rzeczy: Zaczynam bawienie się z bitmapami. Gdy chcę użyć w programie obsługi komunikatu WM_PAINT przestają mi działać MessageBox'y i timery. Problem pojawia się nawet gdy napiszę w programie
case WM_PAINT: {
break;
}
W programie muszę użyć i timera i WM_PAINT(można ten komunikat czymś innym zastąpić?). Proszę o pomoc :).
P-120834
Monika90
» 2014-11-16 21:06:05
Podaj więcej kodu.
P-120837
SMTI
Temat założony przez niniejszego użytkownika
» 2014-11-16 21:10:34
C/C++
#include <windows.h>
#include <time.h>
#include <cstdlib>

HWND g_hwnd;
HDC hdcOkno;
int x, y;

HBITMAP hbmObraz;
BITMAP bmInfo;
RECT rcKulka;
HDC hdcNowy;
HDC hdcNew;

/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc( HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam ) {
    switch( Message ) {
       
        /* Upon destruction, tell the main thread to stop */
    case WM_DESTROY: {
            DeleteObject( hbmObraz );
            DeleteDC( hdcNowy );
            PostQuitMessage( 0 );
            break;
        }
    case WM_LBUTTONDOWN: {
            if( 10 == 10 )
            {
                if( LOWORD( lParam ) >= x && LOWORD( lParam ) <= x + 85 && HIWORD( lParam ) >= y && HIWORD( lParam ) <= y + 150 )
                {
                    hdcOkno = GetDC( g_hwnd );
                    FillRect( hdcOkno, & rcKulka,( HBRUSH )( COLOR_WINDOW + 1 ) );
                    x = rand() % 1115;
                    y = rand() % 750;
                    SetRect( & rcKulka, x, y, x + 85, y + 150 );
                    ReleaseDC( g_hwnd, hdcOkno );
                    hbmObraz =( HBITMAP ) LoadImage( NULL, "plik.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
                    hdcOkno = GetDC( hwnd );
                    hdcNowy = CreateCompatibleDC( hdcOkno );
                    SelectObject( hdcNowy, hbmObraz );
                    BitBlt( hdcOkno, x, y, 85, 150, hdcNowy, 0, 0, SRCCOPY );
                    ReleaseDC( g_hwnd, hdcOkno );
                    ReleaseDC( g_hwnd, hdcNowy );
                }
            }
            break;
        }
       
        /* All other messages (a lot of them) are processed using default procedures */
        default:
        return DefWindowProc( hwnd, Message, wParam, lParam );
    }
    return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
   
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
    MSG Msg; /* A temporary location for all messages */
   
    /* zero out the struct and set the stuff we want to modify */
    memset( & wc, 0, sizeof( wc ) );
    wc.cbSize = sizeof( WNDCLASSEX );
    wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
   
    /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
    wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 );
    wc.lpszClassName = "WindowClass";
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); /* Load a standard icon */
    wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION ); /* use the name "A" to use the project icon */
   
    if( !RegisterClassEx( & wc ) ) {
        MessageBox( NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
        return 0;
    }
   
    hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, "WindowClass", "Caption", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, /* x */
    CW_USEDEFAULT, /* y */
    1200, /* width */
    900, /* height */
    NULL, NULL, hInstance, NULL );
   
    if( hwnd == NULL ) {
        MessageBox( NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
        return 0;
    }
   
    srand( time( NULL ) );
    x = rand() % 1115;
    y = rand() % 750;
   
    g_hwnd = hwnd;
    hbmObraz =( HBITMAP ) LoadImage( NULL, "plik.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
    hdcOkno = GetDC( hwnd );
    hdcNowy = CreateCompatibleDC( hdcOkno );
    SelectObject( hdcNowy, hbmObraz );
    BitBlt( hdcOkno, x, y, 85, 150, hdcNowy, 0, 0, SRCCOPY );
    ReleaseDC( hwnd, hdcOkno );
    ReleaseDC( hwnd, hdcNowy );
   
    SetRect( & rcKulka, x, y, x + 85, y + 150 );
   
    MessageBox( NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
   
    while( GetMessage( & Msg, NULL, 0, 0 ) > 0 ) { /* If no error is received... */
        TranslateMessage( & Msg ); /* Translate key codes to chars if present */
        DispatchMessage( & Msg ); /* Send it to WndProc */
    }
    return Msg.wParam;
}
 nwm czy to nie za dużo, przepraszam za ewentualny spam czy coś takiego, jestem nowy na forum.
(kod bez timera i WM_PAINT ale cała reszta jest)
P-120838
Monika90
» 2014-11-16 21:22:33
Prawidłowa obsługa WM_PAINT wymaga wywołania funkcji BeginPaint i EndPaint, tak jak to jest opisane w dokumentacji.
P-120840
SMTI
Temat założony przez niniejszego użytkownika
» 2014-11-16 21:36:05
Przepraszam za zawracanie głowy, musiałem wcześniej coś źle napisać bo teraz napisałem jeszcze raz i magia - działa. :)
P-120846
« 1 »
  Strona 1 z 1