Recent

Author Topic: One more question. Can you display a picture in a console program?  (Read 701 times)

SW48

  • New Member
  • *
  • Posts: 36
One more question.  Can you display a picture in a console program?

And if not, how hard is it to convert a console program to an application?

440bx

  • Hero Member
  • *****
  • Posts: 1928
Re: One more question. Can you display a picture in a console program?
« Reply #1 on: April 29, 2020, 07:10:12 pm »
One more question.  Can you display a picture in a console program?
The answer I am going to give you is for Windows, I don't know in other O/Ss.

Yes but, I only know how to do it using the Windows API.  I have no idea how it's done the "Lazarus way".

Using the Windows API, you create a window that is a child of the console window.  You also create a thread to service that child window's messages (e.g, to repaint the picture when necessary).  That's about all there is to it but, you will probably need to create a way for the main thread and the secondary thread to communicate.  For simple cases, passing messages from one thread to another is often all you need.

And if not, how hard is it to convert a console program to an application?
That depends on the console program.  It ranges from quite easy to difficult and cumbersome.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

SW48

  • New Member
  • *
  • Posts: 36
Re: One more question. Can you display a picture in a console program?
« Reply #2 on: April 29, 2020, 07:26:47 pm »
Do you know of any sample code to do the Windows API way?

thanks

440bx

  • Hero Member
  • *****
  • Posts: 1928
Re: One more question. Can you display a picture in a console program?
« Reply #3 on: April 29, 2020, 07:45:45 pm »
Do you know of any sample code to do the Windows API way?

thanks
Sure do.

Attached is a simple "proof of concept"/example.  Enable the "ShowMessages" to better see what happens.  The example compiles with Delphi 2 and FPC in both 32bit and 64bit (64bit is FPC only.)

[attachment]
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

SW48

  • New Member
  • *
  • Posts: 36
Re: One more question. Can you display a picture in a console program?
« Reply #4 on: April 29, 2020, 08:21:56 pm »
thanks!

440bx

  • Hero Member
  • *****
  • Posts: 1928
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8590
  • FPC developer.
Re: One more question. Can you display a picture in a console program?
« Reply #6 on: April 29, 2020, 08:41:00 pm »
(you might want to use wingraph in the windows only case)

Warfley

  • Sr. Member
  • ****
  • Posts: 270
Re: One more question. Can you display a picture in a console program?
« Reply #7 on: April 29, 2020, 08:50:25 pm »
You could create a hybrid Console/GUI app, i.e. a console app that can start start up LCL forms when they are needed:
1. Create a new (GUI) Application
2. Open the projects lpr file and add the following line right after $mode
Code: Pascal  [Select][+][-]
  1. {$AppType Console}
3. Hide your window.
4. Start a Thread that excutes your console code
5. When you need to show a form, use Synchronize (or TThread.Queue) to execute a Form1.show in the main thread

You might need to take some thread synchronization into account, but this is rather easy and cross plattform available
« Last Edit: April 29, 2020, 08:52:22 pm by Warfley »

julkas

  • Hero Member
  • *****
  • Posts: 628
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
Re: One more question. Can you display a picture in a console program?
« Reply #8 on: April 29, 2020, 09:27:04 pm »
One more question.  Can you display a picture in a console program?

And if not, how hard is it to convert a console program to an application?
Yes. On Windows I use ptcgraph and on Linux frame buffer device.
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 1941
  • Compiler Developer
Re: One more question. Can you display a picture in a console program?
« Reply #9 on: April 30, 2020, 09:25:01 am »
One more question.  Can you display a picture in a console program?

And if not, how hard is it to convert a console program to an application?
Yes. On Windows I use ptcgraph and on Linux frame buffer device.

Frame buffer device would only work if your Linux is running in non-GUI mode. If a X or Wayland is running the frame buffer device is a bad idea. Not to mention that your application is just inside a console window as well. You should use ptcgraph there as well.

 

TinyPortal © 2005-2018