post
poster: G-man
description: struct array pointer madness
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "resources/resources.h"

LRESULT CALLBACK window_process (HWND, UINT, WPARAM, LPARAM); // Window process
void error(char * from); // Give API error then quit
void notify(char * message); // Notify user via a message box
void notifyd(int d); // Message box a number
int get_file_count(char *); // get number of files/dirs within a dir
WIN32_FIND_DATA * getWFDs(char * dir); // fill WFD

LPSTR app_name = "FishLIST";
TEXTMETRIC tm; // Character meterics
int client_width; // current width of client area
int client_height; // current height of client area

struct { // user setting
    int pos_x; // x cordinates of window
    int pos_y; // y cordinates of window
    int width; // width of window
    int height; // height of window
    int font_size; // size of font
    char font[FILENAME_MAX]; // font name
} user_config;

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;
    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(0, 0, 0));
    mainwnd.lpszClassName = app_name;
    mainwnd.lpszMenuName  = NULL; 
    
    if(!RegisterClass(&mainwnd)){
        error("RegisterClass");
    }

    FILE * fp_config;
    if(!(fp_config = fopen("config", "rb"))) {
        notify("No configuration file found, so a default one will be created"
        " and used.");
        if(fp_config = fopen("config", "wb")){
            user_config.pos_x = CW_USEDEFAULT;
            user_config.pos_y = CW_USEDEFAULT;
            user_config.width = 500;
            user_config.height = 300;
            memset(user_config.font, FILENAME_MAX, 0);
            sprintf(user_config.font, "Fixed");
            user_config.font_size = 0;
            fwrite(&user_config, sizeof(user_config), 1, fp_config);
        }
        else{
            error("Cannot create configuration file.");
        }
    }
    else{
        fread(&user_config, sizeof(user_config), 1, fp_config);
    }

    HWND hdesk = GetDesktopWindow();
    hwnd = CreateWindow(app_name, app_name, WS_POPUP, 
        user_config.pos_x, user_config.pos_y, user_config.width,
        user_config.height, hdesk, NULL, instance, NULL);
    
    if(!hwnd){
        error("from:CreateWindow");
    }
    HWND hprogman = FindWindow("Progman","Program Manager");
    if (hprogman == NULL){
        hprogman = FindWindow("#32769","");
    }
    SetParent(hwnd, hprogman);
    SetWindowLong(hwnd, GWL_STYLE, (GetWindowLong(hwnd, GWL_STYLE) | WS_CHILD));
    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;
    static int selected = 0;
    char * dir = "H:\\Documents and Settings\\Gargantua\\Desktop\\*";
    int files = get_file_count(dir);
    if(message == WM_CREATE){
        hdc = GetDC(hwnd);
        GetTextMetrics(hdc, &tm);
        ReleaseDC(hwnd, hdc);
    }
    else if(message == WM_PAINT){
        hdc = BeginPaint(hwnd, &paint_class);
        SetTextColor(hdc, RGB(0, 255, 0));
        SetBkMode(hdc, TRANSPARENT);
        WIN32_FIND_DATA * pfiles_WFD = getWFDs(dir);

        int y = tm.tmHeight + tm.tmExternalLeading ;
        int counter = 0;
        while(counter != files){
            if(counter == selected){
                SetTextColor(hdc, RGB(255, 255, 255));
                TextOut(hdc, 1, y, pfiles_WFD[counter]->cFileName,
                strlen(pfiles_WFD[counter]->cFileName));
                SetTextColor(hdc, RGB(0, 255, 0));
            }
            else{
                TextOut(hdc, 1, y, pfiles_WFD[counter]->cFileName,
                strlen(pfiles_WFD[counter]->cFileName));
            }
            y += tm.tmHeight + tm.tmExternalLeading;
            counter++;
        }
        SetBkMode(hdc, TRANSPARENT);
        EndPaint (hwnd, &paint_class);
    }
    else if(message == WM_KEYDOWN){
        WIN32_FIND_DATA * pfiles_WFD = getWFDs(dir);
        hdc = GetDC(hwnd);
        if(wParam == VK_UP && selected != 0){
            selected--;
        }
        else if(wParam == VK_DOWN && selected != files){
            selected++;
        }
        
        SetTextColor(hdc, RGB(0, 255, 0));
        SetBkMode(hdc, TRANSPARENT);
        int counter = 0;
        int y = tm.tmHeight + tm.tmExternalLeading ;
        while(counter != files){
            if(counter == selected){
                SetTextColor(hdc, RGB(255, 255, 255));
                TextOut(hdc, 1, y, pfiles_WFD[counter]->cFileName,
                strlen(pfiles_WFD[counter]->cFileName));
                SetTextColor(hdc, RGB(0, 255, 0));
            }
            else{
                TextOut(hdc, 1, y, pfiles_WFD[counter]->cFileName,
                strlen(pfiles_WFD[counter]->cFileName));
            }
            y += tm.tmHeight + tm.tmExternalLeading;
            counter++;
        }
        ReleaseDC(hwnd, hdc);
    }
    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);
}

int get_file_count(char * dir){
    WIN32_FIND_DATA file;
    int files = 0; 
    HANDLE fh = FindFirstFile(dir,&file);
    if(fh == INVALID_HANDLE_VALUE){
        error("from:FindFirstFile");
    }
    while(FindNextFile(fh, &file)){
            files++;
    }
}
WIN32_FILE_DATA * getWFDs(char * dir){
    int files = get_file_count(dir);
    WIN32_FILE_DATA * files_WFD = calloc(files, sizeof(WIN32_FILE_DATA));
    HANDLE fh =    FindFirstFile(dir, files_WFD[0]);

    int counter = 0;
    while(FindNextFile(fh, &files_WFD[counter])){
        counter++;
    }
    return * pfiles_WFD;
}

void notifyd(int d){
    char message[20];
    sprintf(message, "%d", d);
    MessageBox (NULL, message, app_name, MB_ICONINFORMATION);
}