Histogram control

histogram control  Download Source Code and Example


This custom control displays a left scrolling spike with range configurable by calling SetRange(0,100), the first parameter is the min(bottom) value and the second parameter is max(top) value.

The control is designed to work just like the progress control but with a twist. Every SetPos() will shows a spike and the next SetPos() will scroll the previous one to the left.

The CHistogramCtrl class is derived from CWnd, the header and code are in HistogramCtrl.h and HistogramCtrl.cpp files.

Important

In your dialog box, you must create the control as static control (use "picture" from control toolbar). You must set the properties to

  • Type: Rectangle, and
  • Color: black. (I think black background looks better)

Don't worry about the height, width and border, the control will ajust itself and draw the border for you.

First add a member to your dialog class like this:

class CHistogramTestDlg : public CDialog
{
...
// Implementation
protected:
	CHistogramCtrl m_HistogramCtrl;
...
};

And in the initial dialog box...

BOOL CHistogramTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	...

	CRect rect;
	GetDlgItem(IDC_STATIC_HISTOGRAM)->GetWindowRect(rect);
	ScreenToClient(rect);

	m_HistogramCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this, 100);
	m_HistogramCtrl.SetRange(0,100);
  
	return TRUE;  // return TRUE  unless you set the focus to a control
}

And the rest use SetPos(UINT nValue).

The custom control uses unique painting technique which is a little bit to long to discuss here. If you want to understand more please email your FAQ to klen@bellatlantic.net thanks. (The code is not as long to read)

Here is an example of how an application looks like with the control.

histogram control

Last updated: 17 May 1998



Comments

  • How to setup Histogram code in Visual C++ 6.0

    Posted by ringram2077 on 02/14/2007 04:46pm

    Not having any luck getting Histogram set up in VC++. Could someone layout the steps to get it setup to compile ? Thanks

    Reply
  • Another GOOD GDI example

    Posted by Legacy on 03/10/2003 12:00am

    Originally posted by: motty brami

    This is very usefull example, demonstraiting what can be done using GDI, fantasy and programmer's experience.
    
    Well done. Thanks to programmer!!!

    Reply
  • Control in Task Manager

    Posted by Legacy on 12/09/2001 12:00am

    Originally posted by: jj

    In Window's Task Manager, under the "performance" sheet,
    there're two graphs showing the "cpu usage" and "memory usage".

    Anyone knows which control can be used to realise this
    kind of drawing?

    thanks

    Reply
  • Good job!

    Posted by Legacy on 11/04/2001 12:00am

    Originally posted by: Shiuh Liew

    Ken,

    You have done a good job. The code is simple, clean, and efficient.

    Shiuh

    Reply
  • How to add code

    Posted by Legacy on 08/01/2001 12:00am

    Originally posted by: winne

     Your project -HistogramTest .
    
    I want to add Fonts and Display in Rectangle.
    How I do?
    Please ,Tell me!
    Thank you!

    Reply
  • Neat

    Posted by Legacy on 06/07/2001 12:00am

    Originally posted by: Chokri Oueslati

    It is neat and clear. Very easy to handle.

    Reply
  • How ot preview and print in a doc/view program?

    Posted by Legacy on 05/15/2000 12:00am

    Originally posted by: Joshua Liu

    I create the control in a view;but it cannot preview and
    print.How should I do?
    Thanks very much!
    My Email is joshua0137@sina.com

    Reply
  • about histogram......

    Posted by Legacy on 11/05/1999 12:00am

    Originally posted by: abc88

    below is Borland c++ builder source code for histogram.Now, i want to do this with visual c++ and the output r also same with what i have done with BCB. Could anyone help me here???provide me visual c++ source codes and command line for histogram as below if possible......
    
    


    void __fastcall TForm1::Open1Click(TObject *Sender)
    {
    while (true)
    {
    if (OpenPictureDialog1->Execute())
    {
    POINT tPoint[256]; //coordinates
    int nHist[256];
    //clear all
    memset(nHist,0, sizeof(nHist));
    memset(tPoint,0, sizeof(tPoint));
    //create Hist: for all pixels
    for (int i = 0; i < Image1->Picture->Bitmap->Width; i++)
    {
    for (int j = 0; j < Image1->Picture->Bitmap->Height; j++)
    {
    long tColor1 = Image1->Picture->Bitmap->Canvas->Pixels[i][j];
    BYTE tColor = (BYTE)tColor1;
    nHist[tColor]++; //+1
    }
    //find Max of nHist
    int nMax = nHist[0];
    for (int i = 1; i < 256; i++)
    nMax = max(nMax,nHist[i]);
    //make point with coordinates
    for (int i = 0; i < 256; i++)
    {
    tPoint[i].x = (float)Image3->Width/ (float)(255)*(float)i;
    tPoint[i].y = (float)Image3->Height*(1. - (float)nHist[i]/(float)nMax);
    }
    }
    //display image on frame Image1
    Image1->Picture->Bitmap;
    //display graph
    Image3->Canvas->Polyline((const TPoint*)tPoint, 255);
    }
    else // user cancelled
    {
    CGauge1->Progress = 0;
    break;
    }
    }
    }

    Reply
  • Resizing a Custom Control

    Posted by Legacy on 04/17/1999 12:00am

    Originally posted by: M. Qureshi

    I tried to resize the control as the dialog is resized using the following code but it didn't work.

    CMyDialog::OnSize(...)
    {
    if(m_HistogramCtrl.GetSafeHwnd())
    {
    CRect rect;
    GetClientRect(rect);
    m_HistogramCtrl.MoveWindow(rect);
    }

    I might use this control with little modification if I could resize it.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds