الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ادخال الماوس في بيئة C++

مغلق
بدأه medo_elkorsan في 23 أكتوبر 2004 · 16 رد · 2,876 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

ترددت كثيرا قبل ان ابدا في هذا الموضوع خصوصا ان انا ببأي بعيد عن جهازي وانا في المنتدي عشان معندناش تليفون (هنادم عليه اريب)

المهم انا اساسا تعبان اوي مع c++ عشان نفسي اوصل لابعد مدي ممكن وتطوير بيئة العمل عشان تكون بتستخدم الماوس وال true color وكمان اللغه العربيه اللي للاسف المنتدي مش لالها حل ..ما علينا

Theory:- 
To initiate the mouse and other stuff, we make interrupts to the processor using interrupt no 33H. 33H interrupt is dedicated to mouse handling. Depending on the value in the AH register, different operations on the mouse is performed. If dont have any idea what is interrupt and registers, is okay, but knowing them certainly helps you understand the theory better.

The Various Interrupt Services:-
All the following Services are under 33H Interrupt.

To Inititate Mouse:-
	AX = 0
	Return:-
	AX= FFFFH if mouse support is available
	AX= 0  if mouse support is unavailable

To Show Mouse on the screen:-
	AX = 1
	Return :- Nothing

To Hide Mouse:-
	AX = 2
	Return :- Nothing

To Get Mouse Coordinates and Button Status:-
	AX =3
	Return:-
	BX = mouse button status (Bit encoded)
	Bit Significance:-
  0 - left button is pressed
  1 - right button is pressed
  2 - center button is pressed
	CX = x coordinate
	DX = y coordiante

To Set Mouse Position
	AX = 4
	CX = x coordinate
	DX = y coordinate
	Return :- Nothing

To Set horizontal limits for pointer:- 
	AX = 7
	CX = minimum x coordinate
	DX = maximum x coordinate
	Return :- Nothing

To Set vertical limits for pointer:-
	AX = 8
	CX = minimum y coordinate
	DX = maximum y coordiante
	Return :- Nothing

To generate interrupt the function used is 'geninterrupt()'
To see how the above information is used to put in practice, refer the tutorial1.cpp file.

Note:- When the mouse in used in dos without graphics i.e. text mode, it appears as a rectangular block. When it is used in graphics mode, it appears as the normal arrow pointer you see in windows.

وده مثال علي الكلام اللي اتقال فوق

//The Compiler used is Turbo C, I dont know if it will compile in others also.
//Didn't Compile in Microsoft C Compiler. Make the respective change then.
//The function geninterrupt() generates interrupts, it is defined in dos.h
//_AX,_BX,_CX,_DX are defined in dos.h & correspond to ax,bx,cx,dx registers respectively.
//Make a header file out of this one and use it in your programs.
#include<dos.h>//this header file is necessary
#include<stdio.h>//only for displaying messages
#include<conio.h>//for other stuff like getch() etc.
unsigned initmouse()
{
	_AX=0;
	geninterrupt(0x33);	//Generate Inpterrupt
	return _AX;
}

void showmouse()
{
	_AX=1;
	geninterrupt(0x33);
}

void hidemouse()
{
	_AX=2;
	geninterrupt(0x33);
}

void restrictmouse(int x1,int y1,int x2,int y2)
{
	_AX=7;
	_CX=x1;
	_DX=x2;
	geninterrupt(0x33);
	_AX=8;
	_CX=y1;
	_DX=y2;
	geninterrupt(0x33);
}

void getmouse(int *button,int *x,int *y)
{
	_AX=3;
	geninterrupt(0x33);
	*button=_BX;
	*x=_CX;
	*y=_DX;
}

void setmouse(int x,int y)
{
	_AX=4;
	_CX=x;
	_DX=y;
	geninterrupt(0x33);
}

void main()
{
	int x,y,button;
	clrscr();
	gotoxy(29,18);
	textbackground(GREEN);
	cprintf("EXIT");
	gotoxy(1,1);
	if(initmouse()==0)
	{
  printf("Mouse support unavailable");
  return;
	}
	printf("\n\nPress any key to Show Mouse");
	getch();
	showmouse();
	printf("\nPress any key to Hide Mouse");
	getch();
	hidemouse();
	printf("\nPress any key to Show Mouse again");
	getch();
	restrictmouse(208,80,416,160);
	showmouse();
	printf("\nMouse has been restricted");
	printf("\nPress on Exit button to quit the Program");
	while(1)
	{
  getmouse(&button,&x,&y);
  gotoxy(35,1);
  printf("Coordinates:-(%d,%d)  ",x,y);
  gotoxy(35,2);
  if(button&1)//<- Here '&' means ANDing, remember bitwise operators of C/C++
 	 printf("Left button pressed  ");
  else if(button&2)
 	 printf("Right button pressed ");
  else if(button&4)
 	 printf("Center button pressed");
  else
 	 printf("No button Pressed    ");
  if(x>=224&&x<=248&&y==136&&button==1)//checks if exit button is pressed or not
 	 break;
	}
}

اما هنا بقي بنغير شكل الماوس

Prologue:- Welcome to the second tutorial of Mouse Programming. I hope you have read my first tutorial. In this tutorial we are going to learn how to change the mouse cursors, like the windows does. We can change the shape of the mouse cursor from a arrow to anthing we can imagine. We can't change the size though. Also you cannot the change the shape or size if you are using mouse in text mode.

Theory:- Mouse cursor in graphical mode occupies 16 X 16 pixels. By highlighting or de-highlighting the pixels, we can change the mouse pointer. If I ask you how many bits will be required by the mouse, you will say 16 X 16 = 256bits(32 bytes), since each pixel will occupy 1 bit in memory. Good, but instead it requires another 32 bytes, so total of 64 bytes is required by the mouse. The first 32 bytes contain a bitmap which is ANDed with the screen, the second 32 bytes is XORed with the screen. If you dont understand this, dont pull you hair, theories have always been that way. But do try to understand if you can.

In the second tutorial, we have made an interrupt to the processor, but in a different way. The reason that I did not use this methord in the first tutorial is that the second methord is realtively tough that the previous one. Any way, you dont need to know much about it expect the following details:-

	AX = 9    This is the service number
	BX = 0
	CX = 0
	DX = offset address of the pointer image
	ES=DS 	 segment address of pointer
You will also notice two statements in the tutorial2.cpp:-
	unioun REGS i,o;
	struct SREGS s;
These are already defined in dos.h. They contain the registry variables. Thats all you need to know. Use them as they are. Please refer tutorial2.cpp. This file can be compiled to make a program that changes the standard arrow pointer to an hour glass. 

The function used to generate interrupt is 'int86x()' which is defined in dos.h. One question that may come to you mind as which methord of generating interrupt is better. I will say, use 'geninterrupt()' because it is simple and works everytime. As far as 'int86x()', this is the only place where I have used this function to generate interrupt. So if you ever need to generate interrupt, I am sure 'geninterrupt()' will work.

Note:- I have compiled tutorial1.cpp and tutorial2.cpp in Borland Turbo C/C++. Tutorial1.cpp may compile in other compilers since it does not use graphics. Tutorial2.cpp may not compile as many other compilers dont have graphics.h file and other graphics files. So dont blame me. Make the respective changes.

Please give me your feed back. See ya.

وده برضه مثال

#include<dos.h>
#include<conio.h>
#include<mouse.h>
#include<graphics.h>	//Graphics header file required to start graphics
union REGS i,o;
struct SREGS s;

void changecursor(int *shape)
{
	i.x.ax=9;    //Service number
	i.x.bx=0;    
	i.x.cx=0;
	i.x.dx=(unsigned)shape;//Offset address of cursor
	segread(&s); 	 //Stores the segment register values in structure SREGS s
	s.es=s.ds;    //segment address of pointer
	int86x(0x33,&i,&o,&s);	//Generate Interrupt
}

/*BITMAP IMAGE OF AN HOUR GLASS
Binary representation      Hexadecimal reprentation
1111111111111111   	 0xffff
1111111111111111   	 0xffff
1111111111111111   	 0xffff
1111111111111111   	 0xffff
0111111111111110   	 0x7ffe
0011111111111100   	 0x3ffc
0000111111110000   	 0x0ff0
0000001111000000   	 0x03c0
0000001111000000   	 0x03c0
0000111111110000   	 0x0ff0
0011111111111100   	 0x3ffc
0111111111111110   	 0x7ffe
1111111111111111   	 0xffff
1111111111111111   	 0xffff
1111111111111111   	 0xffff
1111111111111111   	 0xffff
*/           	 // '~' is the complement operator in C/C++	
int cursor[32]={	~0xffff,~0xffff,~0xffff,~0xffff,//hour glass screen mask ( in hex)
      ~0x7ffe,~0x3ffc,~0x0ff0,~0x03c0,
      ~0x03c0,~0x0ff0,~0x3ffc,~0x7ffe,
      ~0xffff,~0xffff,~0xffff,~0xffff,

      0xffff,0xffff,0xffff,0xffff,    //hour glass bitmap	(in hex)
      0x7ffe,0x3ffc,0x0ff0,0x03c0,
      0x03c0,0x0ff0,0x3ffc,0x7ffe,
      0xffff,0xffff,0xffff,0xffff	};
void main()
{
	int gd=DETECT,gm;       //DETECT = autodetects available graphics mode
	initgraph(&gd,&gm,"");	//start graphics mode
	initmouse();    //initiate mouse
	changecursor(cursor);	//change cursor
	showmouse();    //display mouse
	getch();
	closegraph();    //shut down graphics and return to text mode
}

انا هنا باي عاوز اعرف ايه العلاقه بين

clrscr();
	gotoxy(29,18);
	textbackground(GREEN);
	cprintf("EXIT");

و ده

if(x>=224&&x<=248&&y==136&&button==1)//checks if exit button is pressed or not
 	 break;

لو انا عاوز اغير الكان اذاي اخدد المكان اللي هيضغط عليه الماوس ..انا جربت كتير ملقتش اي حل .

الموضوع ده كله جاي من

planet-source-code

لا اله الا الله
#2

تشكر جزيلا يا Medo ,, فعلا شيء ممتاز وسنضيفه كرابط لأهم المواضيع ,,

لكن توجد مشكلة !!

الكود يعمل فقط ضمن مصرف ال borland c وليس ال Visual c++ لأن ال Includes files أمثال

#include <dos.h>
#include <graphics.h>

غير موجودة في ال Visual C++ ,,

وهذا ال dos يسهل العبث بالمسجلات والوصول المباشر لعناوين الذاكرة المطلوبة في الكثير من الأحوال ,,

عموما عمل جبار كما يبدو لأن يلم أجربه حتى الآن ,, وليس لدي مصرف Borland للأسف ,, لكن من يجرب البرنامج ويعطينا Screen Shot للشاشة ,, ونرى البرنامج وقت العمل ,,

وجزيتم خيرا ,,

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#3

السلام عليكم

هناك جزئيه مهمه لم يشرحها الكاتب الا وهى علاقه الفونت بحجم الشاشه, حيث انه يضع النص فى النقطه x=29 و y=18 ثم يبحث بالماوس فى المجال x=224 إلى x=248 و y=136. انا حاولت بعمليات حسابيه إيجاد العلاقه لكنى لم افلح للاسف. من كان لديه مصرف c فياريت يطلعنا على النتيجه كى نعرف العلاقه بينهم

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#4

فعلا الحلو ميكملش ..انا مش لاقي اي علاقه بين مكان الكلمه ومكان ضغط الماوس .

اخ احمد مفيش علاقه للفونت بالموضوع انت لو صغرت البرنامج بعد ما تنفذه هيظهر الماوس بتاع الوندوز ولو ضغط علي الكلمه البرنامج هيقفل .

حاجه كمان .. البرنامج ده لما تشغله من بيئة دوس الماوس مبيظهرش .أكيد في فايل للماوس عشان يشتغل .انا جربت احط start up ديسك واشغل البرنامج منه برده مبيشتغلش علي اساس ان start up disk بيشغل ال set up بتاع الوندوز وبيكون فيه ماوس أشمعنه بأى!

لا اله الا الله
#5

السلام عليكم

يا ميدو ابعت الفايل إلى إنت عامل له كومبايل عشان نجربه, لان انا ماعنديش الكمبايلر.

انا مش قادر اتخيل منظره.

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#6

ان تعبت اوي عشان احط البرنامج في ديسك بعدين اجيبه السايبر

و كمان عرفت ازاي اغير المكان ..

بعد ما تشغل البرنامج بتيجي عند exit وتحط الماوس تلاقي فوق احداثي x , y بتيجي ند ال x في اول الكلمه وبعدين تيجي علي ال x في اخر الكلمه و ال y بتفضل ثابته

في البرنامج انا غيرت احداثي الكلمه ذودت احداثي في ال x وفي الy واشتغل صح ذي متهتشوفو ان شاء الله

Noname00.zip

لا اله الا الله
#7

السلام عليكم ورحمة الله وبركاته

فكرة البرنامج جداً رااااااااااااااااااااااااائعه ...ومجهود جبار صراحه

ومالي غير أقول الله يعطيك ألف عافيه عليه>....وبالتوفيق

تم تعديل هذه المشاركة بواسطة al_aman في 28 أكتوبر 2004 في 05:52

#8

حاجه كمان عاوز اضفها اتماما للفائده رغم ان محدش معبر ني وانا كنت متوقع تفاعل اكتر مع الموضوع وانا مستغرب جدا لمشرفين المنتدي اللي معندهمش borland امال بتشتغلو c++ ازاي يا رجاله !

ما علينا في ال text mode ال cursor بتاع الماوس بيبقي علي شكل مستطيل 16*16 بكسل عشان يتحرك بيعدي 8 بكسل سواء الحركه كانت x او y اما في ال graphic mode بيعدي بكسل واحد بس وكده طبعا بيبقي تحديد ال مكان ال mouse press اصعب بس ادق ..سلام.

لا اله الا الله
#9

السلام عليكم

انا مش بشتغل سى ++ انا شغلى كله على الاسمبلى, والموضوع فعلاً جميل جداً, بس انا سبت البرمجه على الدوس من يجى 8 سنين, وشغال على الوندوز من ساعتها.

فكرتنا بايام شبابنا, والله كانت ايام.

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#10

ده حلم عندي يا استاذ احمد (بدام انت كبير كده ) اني اقدر اعمل نظام تشغيل متهيقلي مش هعملو under windows !

لو حد يعرف الفايل اللي بيعمل support للموس under dos ياريت يدهولي

لا اله الا الله
#11

وانا متهيئلى يا ميدو يابنى(ادام انت صغير كدا :) ) إن إلى عايز يعمل نظام تشغيل لازم يعمله فى الاسمبلى, وفى درس محترم اوى عمله الاخ jaas فى قسم الاسمبلى ودا عنوانه /index.php?showtopic=48912

وركز معى الاخ jaas لانه فعلاً ممكن يفيدك فى الموضع دا بازات, وعامل مواضيع كتيره فى قسم الاسمبلى.

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#12

السلام عليكم

ارجعتنا لذكريات الدوس .

اتذكر اني استخدمت Turbo c وعملت برنامج يستخدم graphics مع امكانية ظهور الماوس ولم تكن هناك مشاكل مع win98 حيث يتم اضافة شئ بسيط في احدى ملفات النظام ,مش متذكر (صار لي زمان منه ). ان تذكرت حاكتب لك رد ولو اني اعتقد ان مشكله الماوس في dos سهلة وانا مجربها زمان .

في مواضيع تحدثت عن هذا الشئ من قبل . راجع المواضيع القديمة وستجد ما يفيد او google it فهناك الكثيرين ممن كتبوا عن هذا الشئ واتذكر انه هناك برنامج مكتوب بالباسكال يستعمل graphics وهو مثال بسيط عن عمل نوافذ .

هذا الكلام كله كان قبل كم سنة , لا توجد لدي الان اي معلومات عنه و كان بودي المساعدة لكن انا بعيد عن البرمجة حاليا , حتى ان الكود لم اقرأه .

تحياتي

CIONO1

هناك حتى الأحلام أصبحت ممنوعة ...

إنه لعار أن ننتمي لهكذا أوطان ... لكن ... ربما العار أن نكون نحن أبناء لتلكم أوطان .. من يدري ؟!!

ليعلم أولئك ... إنّ الشعوب إنْ هي استيقظت تسحق ظُلامََهَا ...

There, even in dreams u r wanted

To be a programmer, how a nice dream it was

Leaving ...

أعيدوا لإسمي لونه المفضل

#13

انا دخلت علي ملفات الوندوز ال source ابل ما يبقي علي ال c - الدرايف - ودورت علي mouse ملقيتش نتيجه .

دخلت علي الملفات المضغوطه لقيت mouse.drv حطيته جمب البرنامج..........ومحصلش اي حاجه! احبط اوي الصراح .الموضوع ده مبيفكركش بحاجه بدام انت ناسي .

استاذ احمد ...يعني الاسمبلي ينفع لبرمجه الانظمه والس لأ .

ده تحيذ بأه!

لا اله الا الله
#14

السلام عليكم

لا يا ميدو لا تحيز ولا حاجه بس للكود دا

unsigned initmouse()
{
_AX=0;
geninterrupt(0x33); //Generate Inpterrupt
return _AX;
}

دا مش سى دا اسمبلى وعملين له مكياج, يعنى مش اى مبرمج سى يعرف الكلام دا, عشان تكتب function زى دا لازم تكون بتعرف اسمبلى.

فهمت اصدى.

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#15

انا أخيرا عرفت أجيب العلاقه بين مكان الكلمه وتحديد مكان الضغط من غير ما تفتح البرنامج مثلا

gotoxy(29,18);
textbackground(GREEN);
cprintf("EXIT");

كلمة exit في ال x عند 29 ننقص 1 وبعدين نضرب في 8 يكون الناتج 224 وبعدين نزود 3 (كلمات exit اربع كلمات بس احنا واقفين علي حرف) ونضرب ال3 في 8 ونزود علي 224 يبقي الناتج 248 وال y مكانها 18 ننقص واحد ونضرب في 8 يبقي الناتج 136 وده مكان ال y .

تبقي جملة if

 if(x>=224&&x<=248&&y==136&&button==1)//checks if exit button is pressed or not
  break;

(اعتمدو علي ذكائكم في الفهم عشان الكلام ملغبط وانا فهم الموضوع ده النهارده بس)

مجدش عنده borland c++ بس فيه support لل graphics ونعمل زي مفكره للاسماء وأرقام العناوين تكون ب 256 color وبتعتمد علي (gui) بس (لاحظو ان كل الكلام اللي فوق في ال text mode)

لا اله الا الله
#16

اهو البرنامج مع تغيير شكل الماوس في ال graphic mode

NONAME00.EXE

لا اله الا الله
#17

لو شغلتو البرنامج هتلاقو شكل الماوس زي مهو بس حركته هتلاقي فيها انسيابيه

وشكله هيفضل زي مهو الانك محتاج الفايل ده عشان الجرافيك يشتغل

EGAVGA.BGI

لا اله الا الله

هذا الموضوع مغلق.

مواضيع مشابهة