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 KbDownload 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
ReplyTerminate Whining And Begin a personal adidas shoes Call campaign Preferably
Posted by expopmerm on 03/18/2013 01:54amHard 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
ReplyGood . How to continue normal mouse operation.
Posted by vglaiju on 05/30/2005 03:55amNow 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.
ReplyCImageList class
Posted by Legacy on 06/05/2001 12:00amOriginally 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