Solved

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

Posted on 2003-10-29
10
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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
 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

615 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