Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
// 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();	
}
Posted
Updated 26-May-19 21:36pm
v2

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.
 
Share this answer
 
Buttons are actually windows and are created same way:

C++
<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 />
 
Share this answer
 
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[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900