Home > Mobile >  How do i put mouse cursor in a program in c?
How do i put mouse cursor in a program in c?

Time:09-28

As simple as it gets, I just want to put mouse cursor in my system for my project using C. I have just started my study in programming recently and we are learning basic C programming right now. So, our project is based on C programming.(OS:Windows 10)

CodePudding user response:

Mouse input (unlike eg. stdin) is not a C feature, therefore this can't be realistically done in C alone.

What I can recommend instead is to create a brand new program for mouse-enabled, GUI-oriented environment - like Windows or Gnome. But that's neither a C question nor "adding to your existing program".

/edit:

For the Windows way, I wholeheartedly recommend Charles Petzold's book "Programming Windows", 4th or 5th edition, from 1998 (don't go for 6th, he switched to C#). It's about Win32 C API and general transition from writing commandline programs into GUI apps. Although it's not very modern as well, it gives great insight into how a GUI-oriented system works under the hood. IMHO it's a must for every C programmer.

CodePudding user response:

This is not standard language functionality. You'll need to use either a graphics or GUI library to use this interface to the OS. It's not just "put mouse in" - you either change your window mode from text (what you usually start from) to graphical (where you need to specify coordinates to draw things, cannot just output to stdout), or to forms application (which is a wrapper around graphical mode).

For GUI the simplest would be to make a forms / graphical window application native to your OS. Visual Studio has a wizard for this.

Arguably better practice would be to use a cross-platform library, such as Qt (C ) or GTK (C). There are others. It may be non-trivial for a beginner to use such external libraries.

To just get a graphical window which would allow you to query mouse coordinates (but note that e.g. stdout will then not show on screen - you'll need to use text drawing functions) a standard library to use would be OpenGL. Personally I also have had good experience with the Allegro library which has a simple enough interface, but again - your app will become graphical.

Finally, if you wanted to retain a textual interface and add mouse input capability, you could use the widely used ncurses - but this is most likely nontrivial to use for a beginner, especially on Windows.

Thus your most realistic options in my opinion seem to be either Windows Forms, or a graphical library, such as Allegro.

You can also see the related (unfortunately, closed) SO question: Cross Platform C library for GUI Apps?

  •  Tags:  
  • c
  • Related