Witam mam oto taki kod jak poniżej. Program działa przez kilka sekund i nagle zmienia się kolor tła obrazu na biały mimo tego, że cały czas tło zamolowywane jest przez funkcje FillRect() na zółto. Animacja tekstu też się zatrzymuje w lewym dolnym rogu. To wygląda jakby program tracił uchwyt do kontekstu i nie wykonywał już na nim operacji rysowania, albo jakiś bufer obrazu się przepełnia i wszystko ustaje. Jeśli to ta druga opcja to jak ten bufer wyczyścić. W punkcie (400, 10) g_buffera jest pixel sprawdzający kolor tła może to coś wam pomoże. Czekam na pomoc ;)
#include <windows.h>
#include <stdio.h>
#define BACKGROUND (HBRUSH)CreateSolidBrush(RGB(255.0, 255.0, 0))
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void Animation(void);
void DeInit(HWND hwnd);
char szClassName[ ] = "WindowsApp";
HDC g_hdc = 0;
HDC g_buffer = 0;
HBITMAP bitmap = 0;
BITMAP bitmap_info = {0};
HDC bitmap_hdc = 0;
HBITMAP buffer_bitmap = 0;
int mouse_x = 0;
int mouse_y = 0;
RECT rect = {0, 0, 800, 600};
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd = 0;
MSG messages = {0};
WNDCLASSEX wincl = {0};
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx( 0,
szClassName,
"Prototype_application",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,600,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
UpdateWindow(hwnd);
for(;;)
{
if(PeekMessage(&messages, 0, 0, 0, PM_REMOVE) != 0 )
{
if(messages.message == WM_QUIT) break;
TranslateMessage(&messages);
DispatchMessage(&messages);
}
Animation();
BitBlt(g_hdc, 0, 0, 800, 600, g_buffer, 0, 0, SRCCOPY);// show ready device context
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
g_hdc = GetDC(hwnd);
g_buffer = CreateCompatibleDC(g_hdc);
SetBkMode(g_buffer, TRANSPARENT);
buffer_bitmap = CreateCompatibleBitmap(g_hdc, 800, 600);
SelectObject(g_buffer, buffer_bitmap);
bitmap_hdc = CreateCompatibleDC(0);
bitmap = (HBITMAP)LoadImage(0, "obraz.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(bitmap, sizeof(BITMAP), &bitmap_info);
SelectObject(bitmap_hdc, bitmap);
break;
case WM_KEYDOWN:
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
mouse_x = LOWORD(lParam);
mouse_y = HIWORD(lParam);
break;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
void Animation(void)
{
FillRect(g_buffer, &rect, BACKGROUND);
char buffor[30];
COLORREF colorRGB = GetPixel(g_hdc, 400, 10);
unsigned int R = GetRValue(colorRGB);
unsigned int G = GetGValue(colorRGB);
unsigned int B = GetBValue(colorRGB);
sprintf(buffor, "R: %i G: %i B: %i", R, G, B);
TextOut(g_buffer, 10, 530, buffor, strlen(buffor));
BitBlt(g_buffer, mouse_x-(bitmap_info.bmWidth/2), mouse_y -(bitmap_info.bmHeight/2),
bitmap_info.bmWidth, bitmap_info.bmHeight, bitmap_hdc, 0, 0, SRCCOPY);
}
void DeInit(HWND hwnd)
{
ReleaseDC(hwnd, g_hdc);
ReleaseDC(hwnd, g_buffer);
DeleteObject(bitmap);
DeleteObject(buffer_bitmap);
DeleteDC(g_hdc);
DeleteDC(g_buffer);
}