post
poster: G-man
description: Animation with SetTimer/winapi
language: C
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

LRESULT CALLBACK window_process (HWND, UINT, WPARAM, LPARAM);
void error(char * from);
void notify(char * message); // Notify user via a message box

LPSTR app_name = "cdump";

int WINAPI WinMain(HINSTANCE instance, HINSTANCE prev_instance,
    PSTR cmd_line, int cmd_show){
    HWND hwnd;
    MSG window_msg;
    WNDCLASS mainwnd;
    
    memset(&mainwnd, 0, sizeof(WNDCLASS));
    mainwnd.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    mainwnd.lpfnWndProc = &window_process;
    mainwnd.cbClsExtra = 0;
    mainwnd.cbWndExtra = 0;
    mainwnd.hInstance = instance;
    mainwnd.hIcon = LoadIcon(instance, "ICON_OPEN");
    mainwnd.hCursor = LoadCursor (NULL, IDC_ARROW); // default IDC_ARROW
    mainwnd.hbrBackground = CreateSolidBrush(RGB(100, 100, 100));
    mainwnd.lpszClassName = app_name;
    mainwnd.lpszMenuName  = NULL; 
    if(!RegisterClass(&mainwnd)){
        error("RegisterClass");
    }

    hwnd = CreateWindow(app_name, app_name, WS_OVERLAPPEDWINDOW    , 
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, instance, NULL);
    if(!hwnd){
        error("from:CreateWindow");
    }
    SetTimer(hwnd, 1, 10, NULL);
    ShowWindow (hwnd, cmd_show);
    UpdateWindow(hwnd);
    while(GetMessage (&window_msg, NULL, 0, 0)){
        TranslateMessage(&window_msg);
        DispatchMessage(&window_msg);
    }
    return window_msg.wParam;
}

static int counter = 0;
LRESULT CALLBACK window_process (HWND hwnd, UINT message, WPARAM
    wParam, LPARAM lParam){
    HDC hdc;
    PAINTSTRUCT paint_class;

    if(counter == 514){
        counter = 0;
    }
    
    if(message == WM_TIMER){
        hdc = GetDC (hwnd);

        SetPixel (hdc, counter, 100, RGB(1, 255, 1));
        SetPixel (hdc, counter-14, 100, RGB(100, 100, 100));
    
        SetPixel (hdc, 100, counter, RGB(1, 255, 1));
        SetPixel (hdc, 100, counter-14, RGB(100, 100, 100));

        counter++;
        ReleaseDC (hwnd, hdc);
    }
    else if(message == WM_PAINT){
        hdc = BeginPaint (hwnd, &paint_class);

        SetPixel (hdc, counter, 100, RGB(1, 255, 1));
        SetPixel (hdc, counter-14, 100, RGB(100, 100, 100));
    
        SetPixel (hdc, 100, counter, RGB(1, 255, 1));
        SetPixel (hdc, 100, counter-14, RGB(100, 100, 100));

        EndPaint (hwnd, &paint_class) ;
    }
    else if(message == WM_DESTROY){
        PostQuitMessage(0);
        return 0;
    }
    else if(message == WM_CLOSE){
        DestroyWindow(hwnd);
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

void error(char * error){
    char message[530];
    memset(message, 0, 530);
    if(!strncmp(error, "from:", 5)){
        error += 5;
        char formulated[513];
        memset(formulated, 0, 513);
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0,
            formulated, 512, NULL);
        sprintf(message, "%s: %s", error, formulated);
    }
    else{
        sprintf(message, "%s", error);
    }
    MessageBox (NULL, message, app_name, MB_ICONERROR);
    exit(1);
}

void notify(char * message){
    MessageBox (NULL, message, app_name, MB_ICONINFORMATION);
}