我正在用 C++ 设计一款游戏,目前正在设计我的主菜单,其中包括三个按钮,用于三个难度级别。问题是,我实际上不知道如何在 C++ 中创建按钮。我在 YouTube 上看到了几个关于如何做到这一点的教程,但是制作视频的两个人都只是将这段代码插入到现有程序中,我无法弄清楚如何让它与我的代码一起工作。
这是我目前所拥有的:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
system("color e0");
cout << "Can You Catch Sonic?" << endl;
cout << "Can you find which block Sonic is hiding under? Keep your eyes peeled for that speedy hedgehog and try to find him after the blocks stop moving" << endl;
CreateWindow(TEXT("button"), TEXT("Easy"), WS_VISIBLE | WS_CHILD,
10, 10, 80, 25, NULL, NULL, NULL, NULL);
return 0;
}
当我运行它时,控制台会弹出正确的背景颜色和消息,但没有按钮。谁能告诉我我做错了什么?我确定它与所有这些 NULL 有关,但不确定用什么替换它们。
这就是 YouTube 视频中的代码,但就像我说的,它是在一个已经创建的程序的中间:
CreateWindow(TEXT("button"), TEXT("Hello"),
WS_VISIBLE | WS_CHILD,
10, 10, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
有什么想法吗?我真的很陌生,所以任何帮助或建议将不胜感激。
最佳答案
您应该创建一个消息循环并在循环之前显示按钮。
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
MSG msg;
//if you add WS_CHILD flag,CreateWindow will fail because there is no parent window.
HWND hWnd = CreateWindow(TEXT("button"), TEXT("Easy"), WS_VISIBLE | WS_POPUP,
10, 10, 80, 25, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
关于c++ - 使用 C++ 创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478979/