GDI简单的按钮响应

// practice7.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "windows.h"
#include "stdio.h"
typedef unsigned char U1;
typedef unsigned short U2;
typedef unsigned long U4;
typedef signed char S1;
typedef signed short S2;
typedef struct {
    U1 u1_flag;
    U1 u1_new;
}ST_PRACTICE6_WARNING;
ST_PRACTICE6_WARNING st_s_warning1, st_s_warning2, st_s_warning3;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC    hdc;             //句柄
    PAINTSTRUCT  ps;
    RECT    rect;            //矩形
    HINSTANCE       hInstance;       //窗口实例
    static HWND     hwndButton[2];   //button句柄
    switch (message)
    {
    case WM_CREATE: //创建button
    {
        hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
        //button1
        hwndButton[0] = CreateWindow("BUTTON", "WARNING1",
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            520, 10, 90, 60, hwnd, NULL,
            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
       // button2
        hwndButton[1] = CreateWindow("BUTTON", "WARNING2",
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            520, 120, 90, 60, hwnd, NULL,
            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
        return 0;
    }
    case WM_PAINT: //绘制文字
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        DrawText(hdc, TEXT("By:Eastmount CSDN制作"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        EndPaint(hwnd, &ps);
        return 0;
    case WM_COMMAND: //响应button消息
        if ((HWND)lParam == hwndButton[0])
        {
            //MessageBox(NULL, TEXT("是否训练图片?"), TEXT("提示"), MB_YESNO | MB_ICONQUESTION);
            st_s_warning1.u1_flag=st_s_warning1.u1_flag > 0 ? 0 : 1;
        }
        if ((HWND)lParam == hwndButton[1])
        {
            MessageBox(NULL, TEXT("是否获取图片?"),TEXT("提示"),MB_YESNO|MB_ICONQUESTION);
        }
        return 0;
    case WM_CLOSE: //关闭
        if (IDYES == MessageBox(hwnd, "是否关闭程序?", "提示", MB_YESNO | MB_ICONQUESTION))
        {
            DestroyWindow(hwnd);
        }
        return 0;
    case WM_DESTROY:
        printf("\ndestroying window\n");//退出程序
        PostQuitMessage(0);
        return 0L;
    case WM_LBUTTONDOWN:
        printf("\nmouse left button down at (%d, %d)\n", LOWORD(lParam), HIWORD(lParam));
        return 0;
        // fall thru
    default:
        //printf(".");
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
}
typedef struct MyRect {
    int x, y, width, height;
} MyRect;
int main()
{
    MSG msg;
    PAINTSTRUCT ps;
    HDC hdc;
    U2 u2_t_paint_x;
    u2_t_paint_x = 10;
    printf("hello world!\n");
    const char* const myclass = "myclass";
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    /* Win 3.x */
    wc.style = CS_DBLCLKS;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = GetModuleHandle(0);
    wc.hIcon = LoadIcon(0, IDI_APPLICATION);
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = 0;
    wc.lpszClassName = myclass;
    /* Win 4.0 */
    wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, TEXT("注冊类失败!"), myclass, MB_ICONERROR);
        return 0;
    }
    //MessageBox(NULL, TEXT("注冊类成功!"), myclass, MB_ICONERROR);
        MyRect rect;
        rect.x = 300;
        rect.y = 300;
        rect.width = 640;
        rect.height = 480;
        HWND hwnd = CreateWindowEx(0, myclass, "title",
            WS_OVERLAPPEDWINDOW, rect.x, rect.y,
            rect.width, rect.height, 0, 0, GetModuleHandle(0), 0);
        hdc = GetDC(hwnd);
        ShowWindow(hwnd, SW_SHOWDEFAULT);
        if (hwnd)
        {
            while (1)
            {
                if (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE))
                {
                    DispatchMessage(&msg);
                }
                if (st_s_warning1.u1_flag)
                {
                    printf(".");
                    MoveToEx(hdc, u2_t_paint_x, 10, NULL);
                    LineTo(hdc, u2_t_paint_x, 70);
                }
                u2_t_paint_x++;
                if (u2_t_paint_x > 400)
                {
                    u2_t_paint_x = 0;
                }
                Sleep(10);
            }
        }
        system("pause");
        ReleaseDC(hwnd, hdc);
        return  0;
}
posted @ 2020-09-16 00:32  路人甲MT  阅读(224)  评论(0)    收藏  举报