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;
}
代码你也看完了,求关注不过分吧。