Criar e apagar arquivo em modo Janela com a biblioteca "windows.h" e "stdio.h"

Criar Arquivo e Apagar Usando a Função unlink

#include <windows.h>
#include <stdio.h>

#define ID_BTN_1 1
#define ID_BTN_2 2

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

void Botoes(HWND);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
    static   TCHAR szAppName[] = TEXT("LineDemo");
    HWND     hwnd;
    MSG      msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = GetSysColorBrush(COLOR_INFOBK);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(
        szAppName, TEXT("Line Demonstration"), WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 280, 180,
        NULL, NULL, hInstance, NULL
    );

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch(message) {
        case WM_SIZE:
            Botoes(hwnd);
            return 0;

        case WM_COMMAND:
            if(LOWORD(wParam)==ID_BTN_1) {
                FILE *fp;
                fp = fopen("teste.html", "w");
                fwrite("Valor", sizeof(fp), 1, fp);
                fclose(fp);
            }
            if(LOWORD(wParam)==ID_BTN_2) {
                unlink("teste.html");
            }
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

void Botoes(HWND hwnd) {
    CreateWindow(
        TEXT("button"), TEXT("Criar"), WS_VISIBLE | WS_CHILD,
        10, 40, 100, 20,
        hwnd, (HMENU) ID_BTN_1, NULL, NULL
    );
    CreateWindow(
        TEXT("button"), TEXT("Apagar"), WS_VISIBLE | WS_CHILD,
        140, 40, 100, 20,
        hwnd, (HMENU) ID_BTN_2, NULL, NULL
    );
}

Comentários