如何在C ++中创建按钮 [英] How do I create a button in C++

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

问题描述

我在Dev C ++中创建了一个项目,但我需要在一个按钮上创建一个窗口并控制鼠标事件。

你能帮我吗?



我尝试了什么:



我用过下一个鳕鱼,但它不能正常工作



  //   Grafica de la circunferencia  
// #include< graphics.h>
#include < math.h >
#include < span class =code-preprocessor> < conio.h >
#include < iostream >
#include < winbgim.h >
#include < stdlib.h >
// #include< cstdlib.h>

使用 命名空间性病;
const int ANCHO = 720 ,ALTO = 720 ;
void titulo( int x, int y, char * n = ){
outtextxy(x,y,n);
}

int prueba( int x, int y)
{
rectangle(x,y,x + 70,y + 20);

if (mousex()> x&& mousex()< x + 70&& mousey()> ; y&& mousey()< y + 20&& ismouseclick(WM_LBUTTONDOWN))
{
clearmouseclick(WM_LBUTTONDOWN);
return 1 ;
}
else
{
clearmouseclick(WM_LBUTTONDOWN);
return 0 ;
}
}

void estado( int x, int y, int c1, int c2)
{
if (mousex()> x&& mousex()< x + 70&& mousey()> y&& mousey()< y + 20)
{
setcolor(c1);
}
else
{
setcolor(c2);
}
}

/ * ****** ************************************************** ************************************************** ****************** /
int main( int argc, char * argv []){
initwindow( 400 400 Botónparasalir 300 200 );
setbkcolor(RGB( 63 199 168 ));
cleardevice();

while true
{

titulo( 100 40 SALIR);
estado( 100 42 2 15 );
if (prueba( 90 40 ))
{返回 0 ;}

}

getch();
}

解决方案

我不知道Dev C ++,但你需要一个GUI工具包来创建按钮。



据我所见,您需要创建一个Windows应用程序类型的新项目并开始编写Win32 UI元素。



例如:如何在Dev C ++中创建按钮C ++教程 - YouTube [ ^ ]


按钮实际上是窗口,并且创建方式相同:



< br /> 
#define btn_ID 5000 //在创建过程中用作按钮的ID< br />
< pre> HWND hwndButton = CreateWindowEx( 0 ,L < span class =code-string> BUTTON, // 预定义类; Unicode假设< br />
L OK // 按钮文字< br />
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // 样式< br />
10 // x position< br />
10 // y位置< br />
100 // 按钮宽度< br />
100 // 按钮高度< br />
m_hwnd, // 父窗口< br />
(HMENU)btn_ID, / / 按钮ID< br />
appInstance, // 应用程序实例< br />
NULL); // 不需要指针。< / pre> //检查msdn是否创建了Windows< br />
// 然后在Windows程序中控制按钮的功能:< br />
< br />
< pre> LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){ int commandType = HIWORD(wParam); int controlID = LOWORD(wParam); switch (msg){ case WM_CREATE:{} break < /跨度>; case WM_COMMAND:{ switch (commandType){< br />
< br />
case BN_CLICKED:{< br />
< br />
switch (controlID){< br />
< br />
case btn_ID:{< br />
< br />
// 此处按钮操作< br />
}< br />
中断;< br />
}< / pre>< br />


看起来您尝试将BGI库的端口用于Windows。在这里您可以找到文档和简单的代码: Borland图形界面(BGI)文档 [ ^ ]

I have created a project in Dev C++, but I need to create a window and control mouse events over a button.
Could you help me please?

What I have tried:

I used next cod, but it do not works properly

// Grafica de la  circunferencia
//#include <graphics.h>
#include <math.h>
#include <conio.h>
#include <iostream>
#include <winbgim.h>
#include <stdlib.h>
//#include <cstdlib.h>

using namespace std;
const int ANCHO = 720, ALTO = 720;
void titulo(int x, int y, char *n="    "){
  outtextxy(x,y,n);
  } 

int prueba(int x, int y)
{
  rectangle(x,y,x+70,y+20);
  
  if(mousex()>x && mousex()<x+70 && mousey()>y && mousey()<y+20 && ismouseclick(WM_LBUTTONDOWN))
    {   
	    clearmouseclick(WM_LBUTTONDOWN);
    	return 1;
	}
  else
    {
    	clearmouseclick(WM_LBUTTONDOWN);
  	    return 0;
    }     
}

void estado (int x, int y, int c1, int c2)
{
    if(mousex()>x && mousex()<x+70 && mousey()>y && mousey()<y+20 )
	{
    	setcolor(c1);
	}
	else
	{
		setcolor(c2);
	}    
}

/*****************************************************************************************************************************/  
int main(int argc, char *argv[]) {
	initwindow( 400, 400, "Botón para salir", 300,200 );
	setbkcolor(RGB(63,199,168));
	cleardevice();
	
	while(true)
    {
     
	 titulo(100,40,"SALIR");
	 estado(100,42,2,15);
	 if (prueba(90,40))
	   {return 0;}
	 
	}		
	
	getch();	
}

解决方案

I don't know Dev C++ , but you need a GUI toolkit to create buttons.

As far as I can see, you need to create a new project of type "Windows Application" and start coding Win32 UI elements.

For example : How to Make Button in Dev C++ | C++ Tutorial - YouTube[^]

Good luck.


Buttons are actually windows and are created same way:

<br />
#define  btn_ID 5000 //used as the button's id during creation<br />
 <pre>HWND hwndButton = CreateWindowEx( 0, L"BUTTON",  // Predefined class; Unicode assumed <br />
    L"OK",      // Button text <br />
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles <br />
    10,         // x position <br />
    10,         // y position <br />
    100,        // Button width<br />
    100,        // Button height<br />
    m_hwnd,     // Parent window<br />
    (HMENU)btn_ID,    // Button ID<br />
    appInstance, //Application instance<br />
NULL);      // Pointer not needed.</pre>// Check msdn for windows creation<br />
//Then control the button's fuction in windows procedure:<br />
<br />
<pre>LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ){ int commandType = HIWORD (wParam); int controlID   = LOWORD (wParam); switch( msg ){  case WM_CREATE:{ } break; case WM_COMMAND:{ switch( commandType ){<br />
			<br />
				case BN_CLICKED:{<br />
					<br />
					switch( controlID ){<br />
						<br />
						case btn_ID:{<br />
							<br />
						//Do button action here<br />
						}<br />
						break;<br />
					}</pre><br />


It looks your trying to use a port of the BGI library to Windows. Here you may find documentation and smple code: Borland Graphics Interface (BGI) Documentation[^].


这篇关于如何在C ++中创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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