Hot Combo Box Control

By making minor changed to the way that your application looks you can produce great looking applications which still feel the same to the user.

Everybody loves the new gadgets in Windows that look flat normally but become three dimensional when the mouse cursor moves over them. Unfortunately there doesn't seem to be any documentation on how to create these kinds of controls so I decided to make my own. I started with an edit control, this seemed to be the most simple. This article however deals with the HotComboBox control.

This is what the control looks like normally:

...and when the border is three dimensional, it looks like this:

The HotComboBox control also takes into account the different styles that can be applied to it so that when the border is three dimensional it looks just like a regular Windows control would. So you can still design your dialog boxes as you would normally but with the added benefit of the 'hot' control.


void CHotComboBox::DrawBorder(bool fHot)
{
 CDC *pDC = GetDC();
 CRect rcItem;
 DWORD dwExStyle = GetExStyle();

 GetWindowRect(&rcItem);
 ScreenToClient(&rcItem);

 if (!IsWindowEnabled()) {
  fHot = true;
 }

 // make sure that rcItem start at the smallest point
 if (dwExStyle & (WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME)) {
  if (dwExStyle & WS_EX_STATICEDGE) {
   rcItem.DeflateRect(1, 1, 0, 0);
  }
  if (dwExStyle & WS_EX_DLGMODALFRAME) {
   rcItem.DeflateRect(1, 1, 0, 0);
  }
  rcItem.DeflateRect(1, 1, 0, 0);
  } else {
  rcItem.DeflateRect(1, 1);
 }

 if (fHot) {
  pDC->Draw3dRect(rcItem, m_clr3DDkShadow, m_clr3DLight);
  rcItem.InflateRect(1, 1);
  pDC->Draw3dRect(rcItem, m_clr3DShadow, m_clr3DHilight);

  if (dwExStyle & WS_EX_DLGMODALFRAME) {
   pDC->Draw3dRect(rcItem, m_clr3DShadow, m_clr3DFace);
   rcItem.InflateRect(1, 1, 0, 0);
   if (dwExStyle & WS_EX_STATICEDGE) {
    rcItem.DeflateRect(0, 0, 2, 2);
   } else {
    rcItem.DeflateRect(0, 0, 1, 1);
   }
   pDC->Draw3dRect(rcItem, m_clr3DDkShadow, m_clr3DDkShadow);
  }

  if (dwExStyle & WS_EX_STATICEDGE) {
   if (dwExStyle & WS_EX_DLGMODALFRAME) {
    rcItem.InflateRect(1, 1);
   } else {
    rcItem.InflateRect(1, 1, 0, 0);
   }
   pDC->Draw3dRect(rcItem, m_clr3DShadow, m_clr3DHilight);
  }
 } else {
  pDC->Draw3dRect(rcItem, m_clr3DFace, m_clr3DFace);
  rcItem.InflateRect(1, 1);
  pDC->Draw3dRect(rcItem, m_clr3DFace, m_clr3DFace);

  if (dwExStyle & WS_EX_DLGMODALFRAME) {
   rcItem.InflateRect(1, 1, 0, 0);
   if (dwExStyle & WS_EX_STATICEDGE) {
    rcItem.InflateRect(0, 0, 1, 1);
    pDC->Draw3dRect(rcItem, m_clr3DFace, m_clr3DFace);
    rcItem.DeflateRect(0, 0, 3, 3);
   } else {
    rcItem.DeflateRect(0, 0, 1, 1);
   }
   pDC->Draw3dRect(rcItem, m_clr3DFace, m_clr3DFace);
  }

  if (dwExStyle & WS_EX_STATICEDGE) {
   if (dwExStyle & WS_EX_DLGMODALFRAME) {
    rcItem.InflateRect(1, 1);
   } else {
    rcItem.InflateRect(1, 1, 0, 0);
   }
   pDC->Draw3dRect(rcItem, m_clr3DFace, m_clr3DFace);
  }
 }

 ReleaseDC(pDC);
}
Various message handlers are overridden in order to set various internal flags which dictate whether the control is hot or not. These flags affect whether the controls border is displayed or not.

In order to check whether the control is hot or not, a timer is started whenever mouse movement is detected. By overriding the OnMouseMove and OnNcMouseMove message handlers we are able to detect when the mouse cursor is over the control.


void CHotComboBox::OnNcMouseMove(UINT nHitTest, CPoint point) 
{
 if (!m_fTimerSet) {
  DrawBorder();
  SetTimer(1, 10, NULL);
  m_fTimerSet = true;
 }

 CComboBox::OnNcMouseMove(nHitTest, point);
}

You will notice that an internal flag is set when the timer is set to avoid setting the timer if the timer has already been set. The timer is set to an interval of 10 milliseconds, you can change this to be faster or slower if you feel you need to.

Now, every 10 milliseconds we receive a notification that the timer has been triggered. This will allow us to check whether the mouse cursor is still over the control. While the mouse cursor is over the control, the three-dimensional border is displayed.


void CHotComboBox::OnTimer(UINT nIDEvent) 
{
 POINT pt;
 GetCursorPos(&pt);
 CRect rcItem;
 GetWindowRect(&rcItem);

 // if the mouse cursor within the control?
 if(!rcItem.PtInRect(pt)) {
  KillTimer(1);

  m_fTimerSet = false;

  if (!m_fGotFocus) {
   DrawBorder(false);
  }
  return;
 }

 CComboBox::OnTimer(nIDEvent);
}
As soon as the mouse is no longer over the control the timer is killed and the border is made flat again.

It is also worth noting at this point that the border is permanently displayed as three-dimensional when the control has focus. Overriding the OnSetFocus and OnKillFocus message handlers allows us to set another internal flag which records whether the control has focus or not.

Download demo project - 34 KB

Download source - 3 KB

Date Last Updated: February 4, 1999



Comments

  • Non-actual

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

    Originally posted by: Sergey Zolotorev

    For this effect see Cool Controls Manager (real cool manager) by Bogdan Ledwing... :-(

    Reply
  • How do you make the dropdown button flat?

    Posted by Legacy on 07/12/1999 12:00am

    Originally posted by: Isaac

    The code looks great, but how do you make the dropdown button flat when the mouse is not over the combobox, and then 3-D when it is?

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds