Search code examples
cwindowswinapicontrols

I want to draw a graph in my winapi C program, is there any control?


Does the system come with a standard control that can be used for drawing a diagram like this?

If not, how would I go about creating a custom control to do so?

enter image description here


Solution

  • You definitely should search for any kind of such library at CodeProject in the "Desktop Development -> Miscellaneous" section. But as to me, I'd use "A 2D data visualisation class" which provides a very nice set of features (from the article):

    • Normal and scatter graph modes
    • Autoscale
    • Zooming
    • Fit width, height and page
    • Mouse coordinates tracing
    • Ability to add and display interactively up to 15000 points per second (depends on the speed of the computer)
    • Panning support - just press and hold shift key while moving mouse with left button pressed Graph could be drawn on any device context, including printer's one

    Another option is to use ready-to-go MFC or ActiveX components. Very good example of first one is "Plot Graphic Library" and an example for the second one is "GDI+ Plot ActiveX Control". As you already mentioned, all this libraries based on GDI Windows facility. BTW, you can draw this simple graph by yourself, because GDI (and GDI+) facility in Windows is very powerful.

    Also, see "SoftIntegration Graphical Library (SIGL)". The library contains a lot of different plots and surfaces.

    But, again, it's only about GDI. Another variant is to use DirectX or OpenGL. For the latter you should definitely try SDL (Simple DirectMedia Layer) which:

    is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. As you can see it's a crossplatform library.

    Good SO questions to get familiar with are:

    UPDATE:

    I forgot to mention "ROOT" - it is the most powerful graph library, I've ever seen. Actually, it's not a library, it's "A Data Analysis Framework". The library's creators is a guys from CERN. With this library you can build almost all kind of plots and surfaces and graphics you've ever imagined. It's crossplatform and written in C++. ROOT has both: console version (where you can write command in C++, - ROOT has own C++ compiler) and a huge set of libraries to link with your projects. You can build MFC applications with its help both in MSVS 2008 and MSVS 2010.