How to run a procedure after user clicks on it wit

2019-09-09 16:36发布

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

Program will display:

decimal to binary

binary to decimal

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.

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.

1条回答
Animai°情兽
2楼-- · 2019-09-09 16:56

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.

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.

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.

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.

Hope you get the idea :)

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: 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 :)

查看更多
登录 后发表回答