2014-09-23 1 views
0

사용자가 마우스 클릭으로 선택하면 프로 시저를 실행해야합니다.사용자가 마우스로 클릭 한 후 프로 시저를 실행하는 방법 [파스칼]

프로그램이 표시됩니다 :

소수점 이진

바이너리 진수로

종료

사람이 다음 진수 바이너리에 경우이 절차를 dectobin 실행 후 이진에 진수를 클릭하면 bintodec 프로 시저를 실행하고 Exit를 클릭하면 프로그램이 종료됩니다.

메뉴 프로 시저가 실행 된 후이를 작동 시키려면 IF 문을 입력해야합니까?

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. 

답변

0

콘솔 창의 텍스트 줄은 단추 나 시각적 컨트롤처럼 작동하지 않습니다. 그들은 단지 텍스트 문자열입니다.

시각적 컨트롤 (단추, 텍스트 상자 등)을 사용하는 응용 프로그램에서이 컨트롤은 이벤트라는 특별한 신호를 프로그램의 이벤트 핸들러로 보내고 프로그램은 주어진 이벤트에 대해 수행해야 할 작업을 찾습니다. 이러한 컨트롤은 컴퓨터 화면에서 좌표 및 경계를 인식하고 해당 경계 내에서 마우스 단추를 클릭하면 감지 할 수 있습니다.

콘솔 창에서이 작업을하려면 각 메뉴 요소의 위치를 ​​알아야합니다. 콘솔 창에서 마우스 Y는 행 번호 (0부터 시작)이고 마우스 X는 왼쪽에서 오른쪽 (0부터 시작)의 문자입니다. 첫 번째 줄의 첫 번째 문자는 마우스 X = 0, 마우스 Y = 0을 제공합니다.

모든 메뉴 요소가 항상 같은 줄로 끝나도록 메뉴를 설정 한 경우 마우스 Y가 메뉴 요소의 줄 번호와 일치하는지 테스트 할 수 있습니다.

(이 메뉴 항목 길이 테스트하지 않습니다 - 마우스 X 좌표) 내 간단한 예를 참조하십시오

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. 

당신이 생각 : 콘솔에서

더 경향을 얻을 희망 사용자 친화적 인 메뉴에서 항목을 선택하기 위해 숫자를 사용합니다. 달성하기가 더 쉽습니다.

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

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

추신 : 콘솔 양식의 경우, 이벤트 시스템이 내장되어 있으면 NCURSES 라이브러리를 살펴보십시오. 나는 그것을 사용하지 않았지만 사람들은 기뻐할 것 같다.

+0

비디오/마우스/키보드 장치는 이미 * nix의 ncurses/terminfo와 인터페이스한다. OP는 이미 마우스를 사용하고 있으며 다른 두 개를 더 잘 사용합니다 (기존 Crt는 사용하지 마십시오). Free Pascal과 함께 제공되는 tetris 및 samegame 콘솔 데모를보십시오. 같은 게임은 마우스로 작동합니다. –

관련 문제