post
poster: G-man
description: Drawing with the GDI /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 <windows.h>

LRESULT CALLBACK window_process (HWND, UINT, WPARAM, LPARAM);

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");
    }

    ShowWindow (hwnd, cmd_show);
    UpdateWindow(hwnd);
    while(GetMessage (&window_msg, NULL, 0, 0)){
        TranslateMessage(&window_msg);
        DispatchMessage(&window_msg);
    }
    return window_msg.wParam;
}

LRESULT CALLBACK window_process (HWND hwnd, UINT message, WPARAM
    wParam, LPARAM lParam){
    HDC hdc;
    PAINTSTRUCT paint_class;
    
    char text_out[512] = {};
    
    if(message == WM_CREATE){
        hdc = GetDC(hwnd);
        ReleaseDC(hwnd, hdc);
    }
    else if(message == WM_PAINT){
        SetBkMode(hdc, TRANSPARENT);

        int ypos = 0;
        int xpos = 0;
        int counter = 0;
        int cols = 50;
        int rows = 50;

        hdc = BeginPaint(hwnd, &paint_class);
        while(counter != cols){
            MoveToEx (hdc, counter * (500/cols), 0, NULL) ;
            LineTo (hdc, counter * (500/cols), 500);
            counter++;
        }
        
        counter = 0;
        while(counter != rows){
            MoveToEx (hdc, 0, counter * (500/rows), NULL) ;
            LineTo (hdc, 500, counter * (500/rows));
            counter++;
        }

        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);
}