kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Saturday, November 29, 2014

Chào các bạn!
Mình xin giới thiệu cho các bạn biết về cách để 1 chương trình C++ của bạn có thể nhận click chuột.
Có nhiều dạng code để làm được việc đó. Mình chia sẻ một cách đơn giản sau:
Chương trình của chúng ta sẽ gồm 2 nút bấm. Các bạn sẽ đưa chuột và click vào đấy, chương trình sẽ thực hiện chức năng tương ứng.
Các bước thực hiện
i.              Hàm hiển thị nút bấm:
void Mouse::Show()
{
int x=20,y=10;
gotoxy(25,5); textcolor(10);
cprintf("YOU HAS TWO BUTTON");
VarButton(BUTTON1,"[ BUTTON1 ]",x,y);
VarButton(BUTTON2,"[ BUTTON2 ]",x+20,y);
ShowButton(BUTTON1);
ShowButton(BUTTON2);
}
ii. Hàm xác nhận click chuột:
void Mouse::EnterClick()
{
do
{
MOUSE_THERE=1;
if(kbhit()) getch();
get_mouse_button(&lbutton,&rbutton,&xmouse,&ymouse);
}while(lbutton==0);
}
iii. Hàm phân loại click cho từng chức năng thiết kế:
Mouse::Click()
{
MOUSE_THERE=0;
if(ClickButton(BUTTON1,xmouse,ymouse))
{
EffectClick(BUTTON1) return 1;
}
if(ClickButton(BUTTON2,xmouse,ymouse))
{
EffectClick(BUTTON2) return 2;
}
return 3;
}
iv. Chức năng người dùng thiết kế:
void Mouse::Program()
{
switch(Click())
{
case 1:
clrscr();
gotoxy(20,10); textcolor(10);
cprintf("YOU HAVE CLICK ON BUTTON 1. ENTER TO CONTINUE");
getch();
gotoxy(20,10);clreol();
break;
case 2:
clrscr();
gotoxy(20,10); textcolor(10);
cprintf("YOU HAVE CLICK ON BUTTON 2. ENTER TO EXIT");
getch();
gotoxy(20,10);clreol();
exit(1);
break;
}
}
v. Menu:
[CODE=C++]void Mouse::Menu()
{
while(1)
{
Show();
EnterClick();
Program();
}
}
OK, vậy là xong các hàm rồi nha, các bạn cứ đọc từ từ sẽ hiểu thôi, cũng không khó lắm nhỉ?
Code:
include
#include
#include
#include"button.ta"
#include
#include"mouse.inc"
/*----------------------Dinh_Nghia_Doi_Tuong-------------------------------*/
class Mouse
{
private:
Button BUTTON1,BUTTON2;
public:
void Show();
void EnterClick();
int Click();
void Program();
void Menu();
};
/*-------------------Hien_Thi_Cac_Nut_Bam----------------------------------*/
void Mouse::Show()
{
int x=20,y=10;
gotoxy(25,5); textcolor(10);
cprintf("YOU HAS TWO BUTTON");
VarButton(BUTTON1,"[ BUTTON1 ]",x,y);
VarButton(BUTTON2,"[ BUTTON2 ]",x+20,y);
ShowButton(BUTTON1);
ShowButton(BUTTON2);
}
/*---------------------Nhan_Click_Chuot------------------------------------*/
void Mouse::EnterClick()
{
do
{
MOUSE_THERE=1;
if(kbhit()) getch();
get_mouse_button(&lbutton,&rbutton,&xmouse,&ymouse);
}while(lbutton==0);
}
/*-----------------------Phan_Loai_Click-----------------------------------*/
int Mouse::Click()
{
MOUSE_THERE=0;
if(ClickButton(BUTTON1,xmouse,ymouse))
{
EffectClick(BUTTON1);/*sau khi click chuot thi doi mau khac*/
return 1;
}
if(ClickButton(BUTTON2,xmouse,ymouse))
{
EffectClick(BUTTON2);/*sau khi click chuot thi doi mau khac*/
return 2;
}
return 3;
}
/*------------------------Cac_Chuc_Nang_Cua_Chuong_Trinh-------------------*/
void Mouse::Program()
{
switch(Click())
{
case 1:
clrscr();
gotoxy(20,10); textcolor(10);
cprintf("YOU HAVE CLICK ON BUTTON 1. ENTER TO CONTINUE");
getch();
gotoxy(20,10);clreol();
break;
case 2:
clrscr();
gotoxy(20,10); textcolor(10);
cprintf("YOU HAVE CLICK ON BUTTON 2. ENTER TO EXIT");
getch();
gotoxy(20,10);clreol();
exit(1);
break;
}
}
/*--------------------------Menu_Chinh-------------------------------------*/
void Mouse::Menu()
{
while(1)
{
Show();
EnterClick();
Program();
}
}
/*---------------------------Chuong_Trinh_Chinh----------------------------*/
int main()
{
Mouse ob;
ob.Menu();
return 0;
}
/*------------------------------The_End------------------------------------*/
[/CODE]
Trong chương trình trên, bạn sử dụng 2 thư viện tự tạo, đó là:
[CODE=C++]#include"button.ta"
#include"mouse.inc"

Đoạn code của các thư viện như sau:
button.ta:
include
#include
typedef struct
{
int xbutton;
int ybutton;
char *name;
int statusbutton;
}Button;
void VarButton(Button &B,char *s,int x,int y)
{
B.xbutton=x;
B.ybutton=y;
B.statusbutton=1;
B.name=s;
}
void ShowButton(Button B)
{
if(B.statusbutton==1)
{
textbackground(15);
gotoxy(B.xbutton,B.ybutton);textcolor(LIGHTRED);cprintf("%s",B.name);
textbackground(BLACK);
}
}
void EnableButton(Button& B)
{
B.statusbutton=1;
}
void DisableButton(Button& B)
{
textbackground(15);
gotoxy(B.xbutton,B.ybutton);textcolor(LIGHTGRAY);cprintf("%s",B.name);
B.statusbutton=0;
textbackground(BLACK);
}
int ClickButton(Button B,int xchuot,int ychuot)
{
if(B.statusbutton==1)
{
if(xchuot>=(B.xbutton-1)*8&&xchuot<=(B.xbutton+strlen(B.name)-1-1)*8&&ychuot==(B.ybutton-1)*8)
return 1;
}
return 0;
}
void EffectClick(Button B)
{
gotoxy(B.xbutton,B.ybutton);textcolor(LIGHTCYAN+BLINK);cprintf("%s",B.name);
delay(200);
ShowButton(B);
}
void EffectClickNonButton(int x,int y)
{
gotoxy(x,y);textbackground(WHITE);textcolor(WHITE);cprintf(".");
textbackground(BLACK);
delay(200);
}

mouse.inc:
define MV_LBUTTON 1
#define MV_RBUTTON 2
#define MV_BBUTTON 3

int NUMBER_BUTTONS = 2;
int MOUSE_SIZE = 16;

unsigned char MOUSE_THERE;
unsigned char MOUSE_VISIBLE;

unsigned char LBUTTON_DOWN,
RBUTTON_DOWN,
BBUTTON_DOWN,
LBUTTON_UP,
RBUTTON_UP,
BBUTTON_UP,
CURSOR_MOVED;

int CMX = 0,
CMY = 0;
int BSTATE = 0;

union REGS mregs;
struct SREGS msegregs;

unsigned char EGA_REG_READ = 1;
unsigned char lbutton, rbutton;
int xmouse, ymouse;

void reset_mouse()
{
MOUSE_THERE = 0;
MOUSE_SIZE = 16;
MOUSE_VISIBLE = 0;
if (getvect(0x33) != 0L)
{
mregs.x.ax = 0;
int86(0x33, &mregs, &mregs);
if (mregs.x.ax != 0)
{
MOUSE_THERE = 1;
NUMBER_BUTTONS = mregs.x.bx;
LBUTTON_DOWN = 0;
RBUTTON_DOWN = 0;
BBUTTON_DOWN = 0;
}
}
}

void show_mouse()
{
if (MOUSE_THERE)
{
mregs.x.ax = 1;
int86(0x33, &mregs, &mregs);
MOUSE_VISIBLE = 1;
}
}

void hide_mouse()
{
if (MOUSE_THERE && MOUSE_VISIBLE)
{
mregs.x.ax = 2;
int86(0x33, &mregs, &mregs);
MOUSE_VISIBLE = 0;
}
}

void get_mouse_button(unsigned char *lbutton, unsigned char *rbutton, int *x, int *y)
{
if (MOUSE_THERE)
{
mregs.x.ax = 3;
int86(0x33, &mregs, &mregs);
*lbutton = (mregs.x.bx == 1) ? 1 : 0;
*rbutton = (mregs.x.bx == 2) ? 1 : 0;
if (mregs.x.bx == 3)
*rbutton = *lbutton = 1;
*x = mregs.x.cx;
*y = mregs.x.dx;
}
}

unsigned char get_button_rls_info(int butt_no, int *count, int *x, int *y)
{
if (MOUSE_THERE)
{
mregs.x.ax = 6;
mregs.x.bx = butt_no;
int86(0x33, &mregs, &mregs);
*count = mregs.x.bx;
*x = mregs.x.cx;
*y = mregs.x.dx;
if (butt_no == 0)
return (!(mregs.x.ax & 1));
else
return (!((mregs.x.ax & 2) >> 1));
}
return 0;
}

void set_event_handler(int call_mask, void (far *location)(void))
{
if (MOUSE_THERE)
{
mregs.x.ax = 12;
mregs.x.cx = call_mask;
mregs.x.dx = FP_OFF(location);
msegregs.es = FP_SEG(location);
int86(0x33, &mregs, &mregs);
}
}

void set_hide_bound(int x1, int y1, int x2, int y2)
{
if (MOUSE_THERE)
{
mregs.x.ax = 16;
mregs.x.cx = x1;
mregs.x.dx = y1;
mregs.x.si = x2;
mregs.x.di = y2;
int86(0x33, &mregs, &mregs);
}
}

void set_mouse_position(int x,int y)
{
if (MOUSE_THERE)
{
mregs.x.ax = 4;
mregs.x.cx = x;
mregs.x.dx = y;
int86(0x33, &mregs, &mregs);
}
}

void set_mouse_hlimits(int x1,int x2)
{
if (MOUSE_THERE)
{
mregs.x.ax = 7;
mregs.x.cx = x1;
mregs.x.dx = x2;
int86(0x33, &mregs, &mregs);
}
}

void set_mouse_vlimits(int y1,int y2)
{
if (MOUSE_THERE)
{
mregs.x.ax = 8;
mregs.x.cx = y1;
mregs.x.dx = y2;
int86(0x33, &mregs, &mregs);
}
}

/*void far event_handler(void);

void near event_processor(int event_status, int button_status, int x, int y)
{
if ((CMX != x) || (CMY != y))
{
CURSOR_MOVED = 1;
CMX = x;
CMY = y;
}

BSTATE = button_status;

if (event_status & 2)
LBUTTON_DOWN = 1;
if (event_status & 8)
RBUTTON_DOWN = 1;
if (((event_status & 2) || (event_status & 8)) && (button_status == 3))
BBUTTON_DOWN = 1;
if ((NUMBER_BUTTONS == 3) && (event_status & 32))
BBUTTON_DOWN = 1;
if (event_status & 4)
LBUTTON_UP = 1;
if (event_status & 16)
RBUTTON_UP = 1;
if (LBUTTON_UP & RBUTTON_UP)
BBUTTON_UP = 1;
if ((NUMBER_BUTTONS == 3) && (event_status & 64))
BBUTTON_UP = 1;
}

void reset_event_handler(void)
{
CURSOR_MOVED = 0;
LBUTTON_DOWN = LBUTTON_UP = 0;
RBUTTON_DOWN = RBUTTON_UP = 0;
BBUTTON_DOWN = BBUTTON_UP = 0;
}

void install_event_handler(void)
{
if (NUMBER_BUTTONS == 3)
set_event_handler(127, event_handler);
else
set_event_handler(31, event_handler);
}*/

Các bạn lưu 3 file trên vào mục BIN của BORLAND C, sau đó chạy thử để xem kết quả. Nếu bạn dùng WIN 7 thì bạn phải để BORLAND C vào mục cài win mới chạy được. Thường là ổ C. Sau đó bạn open with tệp menu và đặt mặc định mở các file code là BC trong BIN nhé!!
Hi vọng bài hướng dẫn này sẽ giúp các bạn hiểu hơn về cách thiết kế để chương trình C++ của các bạn nhận click chuột.

0 comments:

Post a Comment

domain, domain name, premium domain name for sales

Popular Posts