CToolBar usage
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: CToolBar usage

  1. #1
    Join Date
    Oct 2003
    Posts
    76

    CToolBar usage

    I am trying to use CToolBar object.

    1) I am using the following set of commands to display it, but it doesnt show up on the window.

    CToolBar toolBar;

    if (!toolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !toolBar.LoadToolBar(IDS_SCALE))
    {
    TRACE0("Failed to create toolbar\n");
    }

    toolBar.LoadBitmap("IDB_VERTICAL_GRAPH_SCALE");
    toolBar.SetButtons(NULL,1);

    This is to have a minimal tool bar thingy working. But it doesnt even show up. What am I missing? Whatever method this is in, I am calling it on OnPaint().


    2) I am using (once I get it working) "CBRS_BOTTOM" this to display the tool bar at the bottom. I want it on (vertical) left too. How do I manage that?

    This is creating a tool bar object and not through class wizard. That's why I am probably confused.

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Problems with this approach:

    1. OnPaint is a wrong place for creating/displaying any control. It is a paint handler as the name specifies and it is a handler which has to be used only for that purpose. Note that it is called several times and whenever windows needs to paint any part of the window due to uncovering, moving or whatsoever. There is no gaurantee that it will be called in a particular order.

    2. So, first off , you need it to move it from OnPaint handler to someother place. The best place would be some handler related to Creation or initialization. I'm not sure what kind of project u r using. Is it a dialog, or is it a SDi/MDI app ? If it is an SDI / MDI app, you could create a sample app with all defaults and will be able to see the toolbar creation in OnCreate method of mainframe.

    Note: Since you are using CToolBar toolbar on the stack in OnPaint handler, when the OnPaint handler exits, the CToolbar object is destroyed ( in turn calling DestroyWindow on the toolbar window ). That is the reason why it doesn't show because by the time it is created, it is destroyed too. So, since you want it to be persistent, you have to make it a member of a window which will have this toolbar as child window.

  3. #3
    Join Date
    Oct 2003
    Posts
    76
    But I created a CMenu object in a method which is called by OnPaint(), and that works. As in it at least shows up.

    And I put the same code in OnInitDialog(), but the problem still persists. It doesnt show up at all.

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    It may work. But it is not a good place.

    So , from what you say, I understand that you are trying to create a toolbar for a dialog, right ?

  5. #5
    Join Date
    Oct 2003
    Posts
    76
    Right.

    But I still have not been able to do it. Is it just about being in the wrong place. Coz it is not showing the tool bar at all.

  6. #6
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    check this out. There are more toolbar articles on CG

    http://www.codeguru.com/Cpp/controls...cle.php/c2545/

  7. #7
    Join Date
    Oct 2003
    Posts
    76
    I did that. It shows the tool bar as it is in OnInitDialog(). But it doesnt repaint it. So if the window is repainted, its not possible to get the tool bar back.

    To avoid this I tried to put it in OnPaint(), where it hangs on execution.

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Did you add a member variable to your dialog class for the toolbar ?
    or probably post just the code for your OnInitDialog()
    Last edited by kirants; April 26th, 2004 at 05:06 PM.

  9. #9
    Join Date
    Oct 2003
    Posts
    76
    If I had not added the member variable, then it would not seem like an OnPaint() issue. It wouldnt compile at all. Right? So I dont think its that. But it could be that I am not adding something else that I am supposed to, no idea what though. Thats my problem.

  10. #10
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Post the code for creating that from OnInitDialog(). Maybe I can paste it to a sample app and see.

  11. #11
    Join Date
    Oct 2003
    Posts
    76
    1) So this goes in the header file:

    CToolBar cToolBar;

    2) This goes in OnInitDialog():

    BOOL CSpectPlotsDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    cToolBar.Create(this);
    cToolBar.LoadToolBar(IDS_SCALE);
    cToolBar.ShowWindow(SW_SHOW);
    cToolBar.SetBarStyle(CBRS_ALIGN_BOTTOM | CBRS_TOOLTIPS | CBRS_FLYBY);
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

    return TRUE;
    }


    3) And this is another funciton as per that link:

    BOOL CSpectPlotsDlg::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
    {
    ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);

    // if there is a top level routing frame then let it handle the message
    if (GetRoutingFrame() != NULL) return FALSE;

    // to be thorough we will need to handle UNICODE versions of the message also !!
    TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
    TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
    TCHAR szFullText[512];
    CString strTipText;
    UINT nID = pNMHDR->idFrom;

    if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
    pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
    {
    // idFrom is actually the HWND of the tool
    nID = ::GetDlgCtrlID((HWND)nID);
    }

    if (nID != 0) // will be zero on a separator
    {
    // AfxLoadString(nID, szFullText);
    strTipText=szFullText;

    #ifndef _UNICODE
    if (pNMHDR->code == TTN_NEEDTEXTA)
    {
    lstrcpyn(pTTTA->szText, strTipText, sizeof(pTTTA->szText));
    }
    else
    {
    _mbstowcsz(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
    }
    #else
    if (pNMHDR->code == TTN_NEEDTEXTA)
    {
    _wcstombsz(pTTTA->szText, strTipText,sizeof(pTTTA->szText));
    }
    else
    {
    lstrcpyn(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
    }
    #endif

    *pResult = 0;

    // bring the tooltip window above other popup windows
    ::SetWindowPos(pNMHDR->hwndFrom, HWND_BOTTOM, 0, 0, 0, 0,SWP_NOACTIVATE|
    SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER); return TRUE;
    }
    }


    This seems to have issues though.

    And this is all I have which shows a gray tool bar sorta thingy at the bottom of the wondow. But on repaint it disappears.

  12. #12
    Join Date
    Oct 2003
    Posts
    76
    Another thing:

    There is something called:

    AfxLoadString(nID, szFullText);

    in the method. But it is undeclared identifier error with that. It has no help on MSDN too. What it that?

  13. #13
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    This is what I did:
    1. Created a sample MFC app of dialog based type
    2. Added a toolbar resource with ID set to IDS_SCALE
    3. Added CToolbar cToolBar object to the dialog .h file
    4. Added the code for OnInitDialog that you have pasted and everything seems to be fine. I don't have any problems whatsoever with painting.

    Note: I did not do that tooltip stuff simply becuase I think you don't need it, right ?

    Try the above steps yourself and see if there is a problem.

  14. #14
    Join Date
    Oct 2003
    Posts
    76
    I am working in someone else's application, which is JUST HUGE. I can't figure out what sort of application it is. But the file I am working in is a dialog file.

    Is that sufficient info for me to develop on it?

    So I cant do this:
    1. Created a sample MFC app of dialog based type

    So I dont really know wat you mean by this:
    2. Added a toolbar resource with ID set to IDS_SCALE

    But I have added IDS_SCALE in string discardable in the resource file.

    I have done the following:
    3. Added CToolbar cToolBar object to the dialog .h file
    4. Added the code for OnInitDialog that you have pasted and everything seems to be fine. I don't have any problems whatsoever with painting.


    So as far as #2 is concerned, what xactly do I have to add in the resouce file, keeping in minf I am not using classwizard?

  15. #15
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Originally posted by tarangkaushal
    So I dont really know wat you mean by this:
    2. Added a toolbar resource with ID set to IDS_SCALE
    I guess that is the problem then. Probably the toolbar is not even initialized because you are passing a string resource when it expects a toolbar resource.

    So, step 1 is to create this.
    1. Go to resource view in your project
    2. On toolbar resources, right click and select Insert...
    3. Select toolbar from the options & click New
    4. You will be presented with a box for drawing the toolbar button. Draw a sample button. Once you are done drawing, another button is added if you look at the top pane. Select that and draw another button. So, for a test, that is enough. You have a toolbar with 2 buttons.
    5. Note the ID for this in the toolbar resources..( default IDR_TOOLBARn )
    6. In your OnInitDialog replace IDS_SCALE with this ID.

    Now test and see what happens.

    6.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center