C语言实现窗体程序,DevC++ 窗体应用

DevC++ 窗体应用,C语言窗体程序

对于很多学习C语言的同学来说,平时自己写的都是控制台程序,一个黑板版,很难受。
今天教大家用C,写出一个九九乘法表的窗体exe。

效果

效果图
环境
创建一个文件,扩展名.c,然后把源码复制上去。
修改时,把自己要绘制的内容放入第三行draw函数里面就可以啦。
好吧,直接上源码:

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

代码你也看完了,求关注不过分吧。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值