DragToolBar - Drag items from Toolbar and drop into view

The simplest way

The main problem preventing us to drag an item from toolbar is that toolbar does not respond to drag action. Therefore, we should hand and send correspondent messages ourself. To do this, we need derive a new class CDragToolBar from CToolBar, then hand the LButtonDown event as following:

void CDragToolBar::OnLButtonDown(UINT nFlags, CPoint point) 
{
   int nCount = GetToolBarCtrl().GetButtonCount();
   for (int i=0; i<nCount; i++) {
   CRect rect;
      GetItemRect(i, rect);
      if (rect.PtInRect(point)) {
         PostMessage(WM_COMMAND, GetItemID(i));
         return;
      }
   }
      
   CToolBar::OnLButtonDown(nFlags, point);
}

Now, in the view, we capture all events of toolbar and set program into drag mode:

void CDTBView::OnDragToolBar(UINT nCmdID)
{
   m_dragBtn = nCmdID;
   BeginDrag ();
}

In my application, I implement drag and drop via CImageList.

That method is work fine. However, the problem is that the drag button of toolbar is not repainted correctly because of disturbing of drag cursor (it captures the picture of button before updating and then repaints it). We need a little improvement.


Further improvement

Instead of entering drag mode right after receiving the messages, program just records the drag button event, and let little time for toolbar to update the button status. To ensure that, we capture the mousemove event and check if the mouse moves far enough. If everything is ok, send another message to tell program to enter the drag mode:

void CDragToolBar::OnLButtonDown(UINT nFlags, CPoint point) 
{
   int nCount = GetToolBarCtrl().GetButtonCount();
   for (int i=0; i<nCount; i++) {
      CRect rect;
      GetItemRect(i, rect);
      if (rect.PtInRect(point)) {
         PostMessage(WM_COMMAND, GetItemID(i));
         m_dragMode = TRUE;
         m_point = point;
         return;
      }
   }
      
   CToolBar::OnLButtonDown(nFlags, point);
}

void CDragToolBar::OnMouseMove(UINT nFlags, CPoint point) 
{
   if (m_dragMode && 
             abs(m_point.x-point.x)+abs(m_point.y-point.y)>=3) {
      PostMessage(WM_COMMAND, ID_START_DRAG);
      m_dragMode = FALSE;
   }
   CToolBar::OnMouseMove(nFlags, point);
}

And on the view, make some changes:

void CDTBView::OnDragToolBar(UINT nCmdID)
{
   m_dragBtn = nCmdID;
}

void CDTBView::OnStartDrag() 
{
   BeginDrag();
}

You should add more codes to make your toolbar work smarter.

void CDragToolBar::OnLButtonDown(UINT nFlags, CPoint point) 
{
   int nCount = GetToolBarCtrl().GetButtonCount();
   for (int i=0; i<nCount; i++) {
      CRect rect;
      GetItemRect(i, rect);
      if (rect.PtInRect(point)) {
         PostMessage(WM_COMMAND, GetItemID(i));
         m_dragMode = TRUE;
         m_point = point;
         return;
      }
   }
      
   CToolBar::OnLButtonDown(nFlags, point);
}

void CDragToolBar::OnMouseMove(UINT nFlags, CPoint point) 
{
   if (m_dragMode && 
             abs(m_point.x-point.x)+abs(m_point.y-point.y)>=3) {
      PostMessage(WM_COMMAND, ID_START_DRAG);
      m_dragMode = FALSE;
   }
   CToolBar::OnMouseMove(nFlags, point);
}

And on the view, make some changes:

void CDTBView::OnDragToolBar(UINT nCmdID)
{
   m_dragBtn = nCmdID;
}

void CDTBView::OnStartDrag() 
{
   BeginDrag();
}

You should add more codes to make your toolbar work smarter.

Downloads

Download demo project - 30 Kb
Download source - 2 Kb


Comments

  • The most important bvlgari purse Endeavor Meet - Who likes nothing triumphs?!

    Posted by expopmerm on 03/22/2013 08:30pm

    [url=http://www.mcmoutletjpinoue.com]mcm 店舗[/url] Something Nearly everybody Despises Concerning mcm And also Reason why [url=http://www.mcmsalejapanoka.com]mcm リュック 激安[/url] Great Stuff Every mcm Lover Preferably Should Have A Shot At [url=http://www.mcmzankijpshop.com]mcm リュック[/url] Stay Away From The Approaches That Could Possibly Screw Up Your mcm Permanently [url=http://www.mcmstorejpkodo.com]mcm リュック[/url] Hidden Tips To Dominate Together With mcm Concise post illustrates the proven facts about bvlgari purse and precisely how it can certainly upset your organization. [url=http://www.bvlgarishopjprie.com]ブルガリ 財布[/url] Greatest strategy for bvlgari purse that you will check out right away. [url=http://www.bvlgarioutletshopjp.com]ブルガリブルガリ[/url] This is why no-one is having a debate about bvlgari purse and accordingly something that one should implement this afternoon. [url=http://www.bvlgarisalekodojp.com]ブルガリブルガリ[/url] Something that the competition has been doing when dealing with bvlgari purse and furthermore exactly what that you might want to perform completely different. [url=http://www.bvlgarisakaijpsale.com]ブルガリ 時計[/url] Fresh site unveils the insight to bvlgari purse together with why you should take action this afternoon. [url=http://www.bvlgariaokisalejp.com]ブルガリ店舗[/url] Current market News : bvlgari purse Defined as A Must As of late mcm Shows You Fresh New Language . Today Our Company Stroll Straight Into The Act [url=http://www.mcmbagsjphadori.com]MCM 財布[/url] mcm Myths Versus The Sincere Basic Facts

    Reply
  • Terminate Whining And Begin a personal adidas shoes Call campaign Preferably

    Posted by expopmerm on 03/18/2013 01:54am

    Hard to find story supplies you with the key points for nike shoes which only a few peopleknow of.|Quick guideline explains most of the ins and outs for nike shoes and also something you ought to do immediately.}[url=http://www.nikejapan.asia/]ナイキ air [/url] Howcome no-one is dealing with nike shoes and for this reason know what you should do right now. [url=http://www.adidasjapan.biz/]アディダス シューズ[/url] The things all the others does in relation to adidas shoes and moreover the things that you will want to complete totally different. The way in which gucci Made Me Famous And Rich [url=http://www.guccijp.asia/]財布 gucci[/url] Creative chloe E-book Will show Strategy To Rule The chloe purse Scene [url=http://www.chloejp.biz/]see by chloe[/url] The Amazing chanel Hack Who Can Fool Almost all [url=http://www.chaneljp.biz/]chanel バッグ[/url] Specifically how to comprehend just about every thing there is to know about chanel purse in Seven easy steps.The key reason why everybody is absolute wrong on the subject of nike shoes and also reasons why you ought to read through this story. [url=http://www.adidasjapan.asia/]adidas スニーカー[/url] A nice double turn on nike shoes [url=http://www.nikejp.biz/]nike running[/url] adidas shoes Counterfeits -- The Most Beneficial adidas shoes trick Regarding Fools 97% of the buyers

    Reply
  • Good . How to continue normal mouse operation.

    Posted by vglaiju on 05/30/2005 03:55am

    Now I have an MDI Appln with Doc - View arch. When i drag and drop in view area its Ok. But when I do the drag and try to click the menu options the default working of cursor should happen .How to handle this.

    Reply
  • CImageList class

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

    Originally posted by: Jane chou

    Hi,

    I implemented CImagelist class in my application for drag and drop images. It works fine when the app runs under NT and 2000, but not window 98. I will be appreciated If you can help me with that.

    I downloaded your drag and drop program. I will try it and drop a comment for you.

    thanks

    Jane chou

    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