用户点击鼠标后如何运行程序[Pascal] [英] How to run a procedure after user clicks on it with mouse [Pascal]

查看:181
本文介绍了用户点击鼠标后如何运行程序[Pascal]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户通过鼠标点击选择它时,我需要运行一个程序。

I need to run a procedure when user selects it with mouse click.

程序将显示:

十进制到二进制

二进制到十进制

退出

如果人们点击十进制到二进制,那么它运行dectobin程序,如果在二进制到十进制,那么它运行bintodec程序,如果他点击Exit然后它退出程序。

If person clicks on decimal to binary then it runs dectobin procedure, if on binary to decimal then it runs bintodec procedure and if he clicks on Exit then it quits program.

执行Menu程序后,我需要在IF语句中键入什么才能使其正常工作?

After the Menu procedure is executed, what do I have to type in IF statement to get this to work?

program menu_with_mouse;
uses crt,mouse,mmsystem;
var n: byte;
var menu_element: array [1..3] of string;
var selected_one_element: boolean;
var mouse_on_element: byte;

procedure Menu;
var sel_el_nr: byte;
    Event: TMouseEvent;
begin
    menu_element[1] := 'decimal -> binary';
    menu_element[2] := 'binary -> decimal';
    menu_element[3] := 'Exit';

    mouse_on_element := 1;
    for n := 1 to 3 do
    begin
       if n = mouse_on_element then textcolor(green)
       else textcolor(LightGray);
       writeln(menu_element[n]);
    end;

    sel_el_nr := 0;
InitMouse;
   Repeat
      GetMouseEvent(Event);

         mouse_on_element := GetMouseY+1;
         for n := 1 to 3 do
         begin
            if (n = mouse_on_element) and
            (GetMouseX < length(menu_element[n])) then textcolor(green)
         else textcolor(LightGray);
            writeln(menu_element[n]);
         end;

     With Event do
     If (Buttons=MouseLeftbutton) and (Action=MouseActionDown) then
     begin
        if mouse_on_element <= 3 then
           selected_one_element := true;
     end;
   Until ((Event.Buttons=MouseLeftbutton) and (Event.Action=MouseActionDown))
   and selected_one_element;
DoneMouse;

end;

procedure dectobin;
var dec: integer;
x: char;
bin: string;
begin
clrscr;
readln(dec);
repeat 
if (dec mod 2 = 0) then bin:='0'+bin 
else bin:='1'+bin; 
dec:= dec div 2;
until dec = 0; 
writeln(bin);
readln;
end;


BEGIN
clrscr;

Menu;

readln;

END.


推荐答案

在控制台窗口中,文本行不起作用像按钮或任何视觉控制。
它们只是文本字符串。

In console window, lines of text don't act like buttons or any visual controls. They are just strings of text.

在使用可视控件(按钮,文本框等等)的应用程序上,这些控件发送特殊信号,称为事件,程序的事件处理程序,然后程序查看它为给定事件做了什么。这些控件在计算机屏幕上知道它们的坐标和边界,并且可以检测是否在这些边界内单击了鼠标按钮。

On applications that use visual controls( buttons, textboxes etc..) these controls send special signals, called events, to program's eventhandler and then program looks what it has to do for event given. Those controls know their coordinates and boundaries on computer screen and can detect if mouse button is clicked within those boundaries.

因此,要使其在控制台窗口上运行,您必须知道每个菜单元素的位置。在控制台窗口中,鼠标Y是行号(从0开始),鼠标X是从左到右的字符(从0开始)。第一行的第一个字符给出鼠标X = 0,鼠标Y = 0。

So, to make this work on console window, you have to know where each menu element is. In console window mouse Y is the line number (starting from 0) and mouse X is the character from left to right(starting from 0). First character on the first line gives mouse X=0,mouse Y=0.

如果您设置菜单以便所有菜单元素总是在同一行上,
您可以测试鼠标Y是否与菜单元素的行匹配数。

If you set up your menu so that all menu elements always end up on the same lines, you can test if the mouse Y matches your menu element's line number.

请参阅我的简单示例(它不测试菜单项长度 - 鼠标X坐标):

See my simple example (it doesn't test for menu item length - the mouse X coordinate):

program Menu;

Uses Mouse;

var
    Event: TMouseEvent;

begin
    writeln('Menu 1');  // line 0
    writeln('Menu 2');  // line 1       
    writeln('Menu 3');  // line 2 

    InitMouse;

    repeat
       GetMouseEvent(Event);
       If (Event.Buttons=MouseLeftbutton) and (Event.Action=MouseActionDown) then
          if GetMouseY = 0 then Writeln('Item 1 pressed')
          else if GetMouseY = 1 then Writeln('Item 2 pressed')
          else if GetMouseY = 2 then Writeln('Item 3 pressed');
    until false;

    DoneMouse;
end.

希望你明白这一点:)

在控制台中,使用数字选择菜单中的项目往往更加用户友好。
它也更容易实现。

In console it tends to be more user friendly to use numbers for selecting items in menu. It's also simpler to achieve.

Menu
-----
  1) Menu A
  2) Menu B
  3) Menu C

What do you want to do ?(1-3):  

PS:对于控制台表单,如果内置了任何事件系统,请查看NCURSES库。我没有用它,但人们似乎很高兴:)

PS: For console forms, look into NCURSES library if that has anykind of event system built into it. I haven't used it, but people seem to be pleased :)

这篇关于用户点击鼠标后如何运行程序[Pascal]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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