我素wfz 2021-08-21 20:48 采纳率: 100%
浏览 58
已结题

C语言到底如何制作窗体程序啊?

win10系统,使用vs2019,必须C语言,究竟该如何做出最简单的HelloWorld啊?
dev也可以

  • 写回答

4条回答 默认 最新

  • 六年级初中生 2021-08-22 09:26
    关注
    #include <string.h>
    #include <windows.h>
    void draw(HWND hwnd) {
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        //字符串1
        //SetTextColor(hdc,RGB(255,0,0));
        DrawText(hdc,
                 TEXT ("卧槽,我在哪儿?"), -1,
                 &rect,
                 DT_TOP|DT_CENTER
        );
        //字符串2
        rect.left=0;
        rect.top=30;
        //SetTextColor(hdc,RGB(69,116,224));
        DrawText(hdc,
                 TEXT ("乘法口诀表:"), -1,
                 &rect,
                 DT_TOP
        );
        //字符串3
        char str[2000]="";
        int i;
        int j;
        for (i= 1; i <= 9; ++i) {
            for (j = 1; j <= i; ++j) {
                char a1[10],a2[10],a3[10];
                itoa(i,a1,10);
                itoa(j,a2,10);
                itoa(i*j,a3,10);
    
                strcat(str, a1);
                strcat(str, "x");
                strcat(str, a2);
                strcat(str, "=");
                strcat(str, a3);
                strcat(str, "  ");
            }
            strcat(str,  "\n");
        }
    
        rect.left=0;
        rect.top=60;
        //SetTextColor(hdc,RGB(87,30,210));
        DrawText(hdc,
                 TEXT (str), -1,
                 &rect,
                 DT_TOP
        );
        EndPaint(hwnd, &ps);
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
        switch (Message) {
            case WM_PAINT:
                draw(hwnd);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            default:
                return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG msg;
        memset(&wc, 0, sizeof(wc));
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.lpfnWndProc = WndProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
        wc.lpszClassName = "WindowClass";
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        if (!RegisterClassEx(&wc)) {
            MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
        hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                              "WindowClass",
                              "C语言实现窗体",
                              WS_VISIBLE | WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              600,
                              300,
                              NULL, NULL, hInstance, NULL);
    
        if (hwnd == NULL) {
            MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
        while (GetMessage(&msg, NULL, 0, 0) > 0) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月23日
  • 已采纳回答 8月22日
  • 创建了问题 8月21日

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行