使用C ++创建按钮 [英] Creating a Button With C++

查看:141
本文介绍了使用C ++创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++设计游戏,目前正在主菜单上工作,该菜单包括用于三个难度级别的三个按钮。问题是,我实际上不知道如何在C ++中创建按钮。我遇到了一些有关如何执行此操作的YouTube教程,但是制作视频的两个人都只是将这段代码插入到现有程序中,因此我很难弄清楚如何使其与我的代码一起使用。 / p>

这是我到目前为止的内容:

  #include stdafx .h 
#include< iostream>
#include< Windows.h>
使用命名空间std;
int main()
{
system( color e0);
cout<< 你能赶上音速吗? <<恩德尔
cout<< 您能找到Sonic藏在哪个块下吗?请为那只快速的刺猬睁大眼睛,并在块停止移动后尝试找到他。<<恩德尔
CreateWindow(TEXT( button),TEXT( Easy),WS_VISIBLE | WS_CHILD,
10、10、80、25,NULL,NULL,NULL,NULL);
返回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;
//如果添加WS_CHILD标志,则CreateWindow将失败,因为没有父窗口。
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);

而(GetMessage(& msg,NULL,0,0))
{
TranslateMessage(& msg);
DispatchMessage(& msg);
}

return(int)msg.wParam;
}


I'm working on designing a game in C++ and am currently working on my main menu which includes three buttons for three difficulty levels. The problem is, I don't actually know how to create a button in C++. I came across a couple of YouTube tutorials on how to do this, but both guys doing the videos were just inserting this piece of code into an existing program and I'm having trouble figuring out how to get it to work with my code.

Here is what I have so far:

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

When I run this, the console pops up with the correct background color and messages, but there is no button. Can anyone tell me what I'm doing wrong? I'm sure it has something to do with all those NULLs, but not sure what to replace them with.

This is what the code from the YouTube video was, but like I said, it was in the middle of a program that had already been created:

CreateWindow(TEXT("button"), TEXT("Hello"), 
   WS_VISIBLE | WS_CHILD,
   10, 10, 80, 25,
   hwnd, (HMENU) 1, NULL, NULL);

Any ideas? I'm really new to this so any help or suggestions would be greatly appreciated.

解决方案

You should create a message loop and show the button before the loop.

#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 ++创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆