Hello!

I'm currently making <<The game of life>> in TP7. Actually, I have already finished everything. The only thing I would like to add is mouse input, so the user could draw the 'cells' by clicking a mousebutton. So, for example a mouseclick on coordinates (50,20) would change grid[50,20] to 'true'. How is this possible? I have found some mouse snippets on the internet, but they don't seem to work with my IDE.

Thanks in advance.

Dani AI

Generated

Two quick clarifications that resolve the original problem and make adding click-to-draw straightforward.

The IDE error “Cannot run a unit” means you opened a unit source (.pas) and pressed Run — the IDE expects a program, not a unit. As noted, open the mouse unit in the IDE and use Compile / Make to produce the compiled unit file, then put that compiled unit where Turbo Pascal looks for units (or add its folder to the unit search path). After that, use the unit from a normal program source (the unit name must match the unit header). If you still get errors, open the unit source to check the exported routine names (Init/Done, Show/Hide, and whatever function returns X, Y and button state).

Practical pattern (adapt names to the unit you installed): initialize the mouse, poll for clicks, convert the mouse coordinates to your grid index, toggle the cell, redraw, and wait for button release (debounce). Example pseudo-Pascal (replace routine names with the ones exported by your unit):

{ replace InitMouse/GetMouseState/GetButtons/DoneMouse with actual names }
var mx,my,btn, cx,cy: Integer;
begin
  InitMouse;
  ShowMouse;
  repeat
    GetMouseState(mx,my,btn);   { mx,my in pixels or text-cells depending on unit }
    if (btn and 1) <> 0 then    { left button pressed }
    begin
      cx := mx div CELL_WIDTH;  { if unit gives pixels; use CELL_WIDTH in pixels }
      cy := my div CELL_HEIGHT;
      if (cx>=0) and (cx<COLS) and (cy>=0) and (cy<ROWS) then
      begin
        Grid[cx,cy] := not Grid[cx,cy];
        DrawCell(cx,cy);
      end;
      while (GetMouseButtons and 1) <> 0 do ; { wait until release }
    end;
    Delay(10);
  until ExitCondition;
  HideMouse;
  DoneMouse;
end;

Important tips:

  • Some units report coordinates starting at 0, some at 1; test by printing values at known positions and adjust (subtract 1 if needed).
  • Units may return text-cell coordinates (1..cols/rows) or pixel coordinates (0..639/199 etc.). Use the proper CELL_WIDTH/CELL_HEIGHT conversion.
  • If using DOSBox or another emulator, ensure mouse passthrough is enabled; mouse capture quirks can hide clicks even though the cursor moves.
  • If unsure which functions to call, inspect the unit source or the example .pas that came with the download to match the API and see any helper routines (many units include a small demo program).

Checklist: compile/make the unit, add its folder to TP7 unit search path, use the unit in your program, adapt coordinate math to the unit's coordinate system, and debounce clicks before toggling grid cells.

Recommended Answers

All 5 Replies

I have checked the article, downloaded the unit and the example .pas file. However, when trying to run it, I get an error "Cannot run a unit". I have put the unit file in my TP7\units folder, and set the unit directory to ..\units
What could be the problem? Because I get this error with any mouse unit files. (Only mouse unit files)


I downloaded the program too.
Find the TG_Mouse.Pas file and copy to your C:\BP\UNITS\ dir
Run the Turbo Pascal ide load the TG_Mouse.pas,this is a unit's sourcefile.
Go to the Compile option and compile it and press the make option
this option make a TG_Mouse.TPU(this is the compiled unit)
then you can use this unit like others
program something;
Uses Crt,TG_Mouse,Dos;
.....
......
:D


I downloaded the program too.
Find the TG_Mouse.Pas file and copy to your C:\BP\UNITS\ dir
Run the Turbo Pascal ide load the TG_Mouse.pas,this is a unit's sourcefile.
Go to the Compile option and compile it and press the make option
this option make a TG_Mouse.TPU(this is the compiled unit)
then you can use this unit like others
program something;
Uses Crt,TG_Mouse,Dos;
.....
......
:D

Thanks! Everything works now :D

#Edit: How do I mark this post as solved?

down on this page there is a writting named MARK AS SOLVED just press it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.