简单有趣的c语言小程序,一个有趣的小程序
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
源碼:
#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.創(chuàng)建窗口
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.消息循環(huán)
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("來點(diǎn)我"),
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("恭喜你點(diǎn)到了!"), _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);
}
總結(jié)
以上是生活随笔為你收集整理的简单有趣的c语言小程序,一个有趣的小程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 点烟器通电发动机亮黄灯什么原因?
- 下一篇: 怀旧服推荐配置_【怀旧服】狂暴战P4毕业