Solved

Attaching a CToolbar to a CWin pointer from an HWND that was created in Visual Basic 6

Posted on 2003-10-29
10
300 Views
Last Modified: 2013-11-25
Sorry if the title is a little confusing.  I thought I would hit the keywords at least.  Using a C++ dll created in Microsoft Visual Studio 6, I would like to be able to attach a toolbar to a HWND object that is being passed in from a Visual Basic 6 application.  Is this possible?  Any code would be greatly appreciated if this is possible.  
Thanks.
0
Comment
Question by:Taitor
  • 5
  • 4
10 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9647865
Yes you can do

Create the tool bar with the hWnd like

m_wndToolbar.Create(CWnd::FromHandle(hVBDlgWnd),---)

Or try CToolbarCtrl

Good Luck
0
 

Author Comment

by:Taitor
ID: 9651405
roshmon,

when I try implementing the suggested code for creating the toolbar, I get an error when the borders for the toolbar are being set.  

---------------------------
Microsoft Visual C++
---------------------------
Unhandled exception in Tester2.exe (MFC42D.DLL): 0xC0000005: Access Violation.
---------------------------
OK  
---------------------------


CControlBar::SetBorders(int 0, int 0, int 0, int 0) line 86 + 6 bytes

The specific line that it seems to error on is in when the call is made to set m_cxLeftBorder = cxLeft;  cxLeft = 0.  
      
Is this something I can resolve or should I try going with CToolbarCtrl?   Thanks

0
 

Author Comment

by:Taitor
ID: 9652172
I have increased the points because I am determined to get this to work.  Here is my rountine that creates the toolbar in the dll.

CBasicToolbarApp::AddToolbarsToHWND(HWND vbHwnd)
{
      
      if (!m_wndToolBar.CreateEx(CWnd::FromHandle(vbHwnd), TBSTYLE_FLAT , WS_CHILD | WS_VISIBLE | CBRS_TOP
            | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
            !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
      {
            TRACE0("Failed to create toolbar\n");
            return -1;      // fail to create
      }
      
      return 0;
}

The code in other application calls this routine and passes the forms hWnd property.  

Thanks in advance.  
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9664608
I think that you will need to go with CToolBarCtrl becuas CToolBar probably assumes that it is being placed onto a CFrameWnd-derived window.  For instance, it contains the logic to automatically resize itself based upon messages and/or expected variables of the parent.

Another note:
There may be a problem with ther LoadToolBar() fn becasue the code might look into the wrong resource file.

Finally,
I am not at all confident that the scheme will work.  The toolbar typically sends WM_COMMAND message to the parent window which then knows how to handle them.  I have no idea if that will hold together when the parent window is managed by the VB-generated code.

Why don't you just use the toolbar that is fully-supported by VB?

-- Dan
0
 

Author Comment

by:Taitor
ID: 9800357
How about I just start over with the question, Does anyone know how to create a CToolBar in a c++ dll that will show up in any kind of window of a c++ exe that calls the dll?  
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 49

Expert Comment

by:DanRollins
ID: 9800646
Does the targetted C++ EXE *know about* the toolbar?  If not, you need to kludge a system of floating a toolbar window over the EXE's window and then work out how to handle the clicks.  It's not gunna happen automatically -- it's like asking "Can put a toilet in my office?" without thinking about the pumbing...

Unless of course, the EXE provides a snap-in capability, as does IE.
0
 

Author Comment

by:Taitor
ID: 9801196
The C++ exe will be aware of the toolbar.  I was kind of hoping that the event handling would happen on the dll side.  I understand that it is not real common or easy do and that is why I am asking the experts.  

Thanks.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 9801277
In that case, It's a snap.
Just make a DLL that exports a class.  Have the class be an object derived from CToolBar.  Write all of the customizing code that you want.

Set the EXE to link to the .LIB that represents the DLL.  In the EXE source, replace the cases in the CMainFrame (or whereever) that normally create and use a CToolBar to instead refer to your customized derived toolbar.

-- Dan
0
 

Author Comment

by:Taitor
ID: 9814656
Dan,

Where is the toolbar suppose to actually be created?  Is the toolbar getting a window from the calling application?  

Thanks,

Tait
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9814742
Each application creates its own toolbar.  This is generally done in the CMainFrame class.
-- Dan
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now