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

 2023-09-18 阅读 19 评论 0

摘要:该楼层疑似违规已被系统折叠隐藏此楼查看此楼源码:#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,in

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

源码:

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

}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/76376.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息