How to minimize to system tray in C - c

How can I minimize my app to the system tray as soon as it starts in C?
I am new to C.
Thanks.

Are you talking about Windows and the taskbar status area? If so, check http://msdn.microsoft.com/en-us/library/windows/desktop/bb762159.aspx for the Shell_NotifyIcon function. There are plenty of references, and even some samples linked on how to use it.
Also Notifications and the Notification Area: http://msdn.microsoft.com/en-us/library/windows/desktop/ee330740.aspx

C, all by itself, is not capable of doing what you want. The language was designed to work on as many possible architectures as possible (microwave ovens ... air bag systems ... mouse movement control ...) and not all such architectures know what a "system tray" is.
You need to use specific libraries (which augment the capabilities of Standard C). There are lots and lots (and lots) of external libraries. Most libraries to do the same thing on different platforms are not compatible between each other ... so we need to know what is the target of your code: Windows? Windows Vista? DOS? microwave oven? sattelite solar panel deployer? ... :-)

Create a window but don't show it.
Use Shell_NotifyIcon to create the icon in the notification area.
In order to perform step 2 you will need the window created in step 1.
If you have never programmed in C before and never used the Win32 API before this is an ambitious first project. First of all you should master the basics of showing windows, programming a message loop, handling messages etc. I recommend Programming Windows by Petzold.

Related

How to directly access the display for drawing

Context
I've been programming mainly as a hobby for some time now, mostly in C# and Java. I made many application (Windows Forms or Java Forms) that required animated content. In Java I would use Graphics.drawX() and redraw in function of time. When the animations were happening frequently the resolution would diminish or the application would slow down. I never gave it thought until I played a video game on the same computer that had so much trouble rendering a simple Java app. How can my computer instantly render a complex moving environment but rush a displaying a home-made 2048 game? I figured it must be because either I am misusing the draw functions, either because those functions are not optized for real-time render.
Question :
How can I directly access the display without having to go through preprogrammed functions?
I realize this maybe hard in higher level languages so let's say in C on a Windows OS. (But I would appreciate any answer relating to any language and/or OS)
I know it's a really vague question but I can't seem to find the right words to Google it appropriatly. Thank you very much for your help!
You can't (or maybe I should say should never) try to access the graphics driver directly on Windows. You used to have write directly to video memory to do graphics prior to Windows as DOS did not support graphics or display management and the stability of those programs were always a bit dicey. On Windows, it owns the screen and you have to work through it to access it.
The very concept of a Windows-based OS is that the OS owns the display and gives application access to a virtual display so that the OS can hide it or move it around. In most cases this does not cause a speed problem; but, in certain cases like gaming you need more speed; so, DirectX allows you tor transfer some of those task to the graphics card to get you the speed you need.
For more info on DirectX, check out Microsoft's Graphics and Gaming Resources

How Can I control another programs?

my question is general for all languages, but I'm using only C, so, I would like to get answer in this one.
My question is, How Can I control the behavior of another applications that I didn't write?
For example:
How Can I fill this entry box (in
this site) using C? Do I need to
control the browser? (I'm using
firefox.)
Still in this example, How Can I open
another tab in this browser using C?
(see, I want to control this
application such another one)
How Can I embed a program im my one?
How Can I fill a database program using your gui, doing it by c?
and so on...
Thanks a lot!
There's no concrete or single answer to your (multiple) questions because every program varies. Short of the desired application having an API, you can resort to using low level Win32 commands to identify handles of processes and windows within those processes which you want to change/read. It's by no means a straightforward or scalable process though.
You'd have to interact with that programs API.
Some programs are scriptable and publish APIs that allow other programs to send commands to them. If your "target" app does something like this, then that will typically be your best bet.
If the app doesn't have a C API, then you probably can't control it in C. That doesn't mean it's impossible, only that you might need a different language. If your app is a Windows GUI app, you can use AutoIt to interact with the GUI programmatically.
Typically you use a program's application programming interface (API) to gain access to publicly available functions that let you accomplish tasks within that program. However, not every application has an API that you can use.
You should start by looking at the documentation of the application you're intending to take control of with your program and seeing if they have an API suitable for your needs.
Most of the time application need API to work with each other.
There is another way around to automatise actions done to applications (like an user would). For example on Windows there is an "language" named AutoIt which can interact with your computer.
I'm not really sure if this is what your seeking but it can do almost all the thing you asked for.
question is, How Can I control the behavior of another applications that I didn't write
Answer: that depends on the application and platform (linux, windows, mac, ...). It generally does not depend on your language of choice.
As an example, quite a lot of gtk/ kde programs on linux can be partially controlled via the dbus messaging bus. Those apps are designed to be controlled in that way.
I think firefox has a command line option to open a new website using an already running browser.
Applescript or automator on a mac can be used to control some apps too, I believe.
In short, make a separate question on what you want to do exactly, stating both platform and app you need to control.

Font Metrics in C

I wanted to know if there is any library in C available for calculating the Font Metrics (Basically i wanted to know the width of a string of Particular Font).
QT has QFontMetrics. Is there any way I can get similar data in C.
I wouldn't say just use FreeType, unless you are on a system that uses X as the graphics display. If you are on Windows, use the Windows API to get font metrics information and on Mac use whatever Cocoa provides.
It might also help if you told us what you are trying to do.
EDIT: Since the output of your library is intended to be consumed by a particular GUI app, you will probably want to use the same GUI library to get the font metrics information as the app is using. Even better would be to have the app provide metrics information to the library, or a callback method that can provide that information. Then the library doesn't even have to know how the font metrics were derived, reducing an unnecessary dependency. This also means that you can use Qt (C++) in your GUI app, but still write your library in C and not have to figure out a way to call C++ from C, which is very difficult, especially if you are trying to make it cross-platform.
C is (just) a programming language. By design C has no embedded functions at all, not even for File I/O.
So you will have to indicate what Graphics/GUI platform you are using.
You can use freetype2 : http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Glyph_Metrics
Check out FreeType: http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html

Linux/Unix Console Graphics/Control

I would like to know how to create console applications with better control of input and output using C. For example, how does the text-editor nano display the file contents in a certain portion of the screen and then keep a list of commands stationary on the bottom. I know you can use the win32 api to do this in Windows, but what is the Unix/Linux equivalent?
More than likely there's a ncurses interface that controls the screen drawing and placement of items on the screen in a certain position. The api is standard across the linux systems and on unix also, some may be implementation defined depending on the variant of curses used under the commercial variants of unix but otherwise some standard ncurses functionality are compatible.
Besides ncurses and depending on the task at hands you may find newt, a library for color text mode, widget-based user interfaces, a suitable alternative also. Sometimes visual results are better with newt.
If you just want to do the low level stuff, you probably want to use the termcap or terminfo library.
If you want to do it the way nano and just about every other interactive terminal app does it, you probably want to use ncurses. That way you will spend less time implementing terminal control logic, and more time on the actual app you are developing.

OS independent clipboard copy/paste text in C

I'm working on a project that's supposed to work on both Windows and Linux (with an unofficial Mac port as well) that emulates a true colour system console.
My problem is that recently there appeared a request for textfield support (yes, console-based) and it would be cool to add the possibility of copying text to clipboard and pasting from it. Is there a way of achieving this that will:
be done in C (not C++),
work in both Windows and in Linux (preprocessor macros are an option if there's no platform-independent code),
require no extra libraries to link to?
Thanks in advance for your help.
If you're not using a cross platform UI library (like wx or something), then it sounds like you're just going to have to write native clipboard code for each platform you want to support.
Remember, on Macintoshes, you copy with Command-C, not Ctrl+C :)
The clipboard is inherently an operating system defined concept. The C language itself has no knowledge of what a clipboard is or how to operate on it. You must either interface directly with the OS, or use a portability library that does this on your behalf. There is no way around this.
Personally I would define my your own function
getClipboardText();
That is defined in two different header files (linux_clipboard.h, windows_clipboard.h, etc) and then do pre-proccessor stuff to load the appropriate one accordingly. I don't really code in C/C++ so I'm sorry if that didn't make any sense or is bad practice but that's how I'd go about doing this.
#if WIN32
#include windows_clipboard.h
#endif
That sort of thing
Remember:
For linux you have to deal with different window managers (Gnome, KDE) all with different ways of managing the clipboard. Keep this in mind when designing your app.
You may be able to communicate to the clipboard by using xclip. You can use this python script here to do this job via communicating with 'dcop' and 'klipper' here. That is for KDE, I do not know how it would be done under GNOME... You may also be able to do this independantly of either GNOME/KDE by using DBUS, although I cannot say 100% confidently on that either...
Just be aware, that for a truly cross-platform job, you have to take into account of the different GUI's such as under Linux, X is the main window manager interface and either GNOME/KDE sits on top of it..I am not singling out other GUI's such as FluxBox, WindowMaker to name but a few, and that there will be a lot of platform dependant code, and also in conjunction, you will be dealing with Windows clipboard as well..all in all, a big integrated code...
Have you not considered looking at the raw X programming API for clipboard support? Maybe that might be better as I would imagine, GNOME/KDE etc are using the X's API to do the clipboard work...if that is confirmed, then the work would be cut out and be independant of the major GUI interfaces...(I hope that would be the case as it would make life easier for your project!)
Perhaps using compile-time switches, for each platform...WIN, KDE, GNOME, MAC or use the one that is already pre-defined..
Hope this helps,
Best regards,
Tom.

Resources