有趣简单c语言小程序,一个有趣的小程序

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

源码:

#include

#include

#include

#include

#include

HINSTANCE g_hInstance = 0;

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

int WINAPI WinMain(

HINSTANCE hInstance,

HINSTANCE hPreInstance,

LPSTR lpCmdLine,

int nShowCmd

)

{

g_hInstance = hInstance;

srand((unsigned)time(NULL));

//1.注册窗口类

WNDCLASS wc = { 0 };

wc.lpszClassName = _T("MyClass");

wc.lpfnWndProc = WndProc;

wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);

RegisterClass(&wc);

//2.创建窗口

HWND hWnd = CreateWindow(

wc.lpszClassName,

_T("顽皮的按钮"),

WS_OVERLAPPEDWINDOW,

200, 100,

640, 480,

NULL,

NULL,

g_hInstance,

0

);

//3.更新显示窗口

UpdateWindow(hWnd);

ShowWindow(hWnd, SW_SHOW);

//4.消息循环

MSG Msg = { 0 };

while (GetMessage(&Msg, 0, 0, 0))

{

TranslateMessage(&Msg);

DispatchMessage(&Msg);

}

return (int)Msg.lParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

static int nCnt = 0;

switch (uMsg)

{

case WM_CREATE:

{

HWND hButton = CreateWindow(

_T("button"),

_T("来点我"),

WS_CHILD | WS_VISIBLE,

10, 10,

80, 45,

hWnd,

HMENU(0x1000),

g_hInstance,

0

);

}break;

case WM_COMMAND:

{

DWORD nCode = HIWORD(wParam);

DWORD nId = LOWORD(wParam);

if (nId == 0x1000 && nCode == BN_CLICKED)

{

if (nCnt == 10)

{

MessageBox(hWnd, _T("恭喜你点到了!"), _T("嘿嘿"), MB_OK);

PostMessage(hWnd, WM_CLOSE, 0, 0);

}

HWND hButton1 = GetDlgItem(hWnd, 0x1000);

int x = rand() % 520;

int y = rand() % 390;

MoveWindow(hButton1, x, y, 100, 50, true);

UpdateWindow(hWnd);

nCnt++;

}

}break;

case WM_CLOSE:

PostQuitMessage(0);

break;

}

return DefWindowProc(hWnd, uMsg, wParam, lParam);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我可以回答这个问题。以下是几个有趣的小号 C 语言程序: 1. Hello World 程序 #include <stdio.h> int main() { printf("Hello World!\n"); return ; } 这是一个经典的 C 语言程序,它会输出 "Hello World!"。 2. 猜数字游戏 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int num, guess, tries = ; srand(time()); num = rand() % 100 + 1; printf("猜一个 1 到 100 之间的数字\n"); do { printf("你的猜测是:"); scanf("%d", &guess); tries++; if (guess > num) { printf("太大了!\n"); } else if (guess < num) { printf("太小了!\n"); } else { printf("恭喜你,猜对了!你用了 %d 次机会。\n", tries); } } while (guess != num); return ; } 这是一个简单的猜数字游戏,程序会随机生成一个 1 到 100 之间的数字,玩家需要猜测这个数字是多少。 3. 计算器程序 #include <stdio.h> int main() { char op; double num1, num2; printf("请输入一个运算符:+、-、*、/\n"); scanf("%c", &op); printf("请输入两个数字:\n"); scanf("%lf %lf", &num1, &num2); switch(op) { case '+': printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2); break; case '-': printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2); break; case '*': printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2); break; case '/': printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2); break; default: printf("错误的运算符!\n"); } return ; } 这是一个简单的计算器程序,可以进行加、减、乘、除四种运算。玩家需要输入一个运算符和两个数字,程序会输出计算结果。 以上是我为您提供的几个有趣的小号 C 语言程序。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值