|
- How to change the size of a toolbar?
- How to add/remove a image of a toolbar?
- Can you help?
- Regards,
BigMouth
|
|
|
|
|
Use SetSizes() to change the size of toolbar buttons.
The height of a toolbar will be the height of the buttons.
And the width will be the width of the containing window
For images use an image list and send a TB_SETIMAGELIST message. If you want to change the images, change the image list and send the TB_SETIMAGELIST message again
Nish
Sonork ID 100.9786 voidmain
www.busterboy.org
If you don't find me on CP, I'll be at Bob's HungOut
|
|
|
|
|
- Thanks pal!
- I still have a question. Can you show me an example about the usage of TB_SETIMAGELIST message?
- Have a nice day,
BigMouth
|
|
|
|
|
You can actually modify the imagelist directly - m_wndToolbar.GetToolbarCtrl().GetImageList() returns a pointer to the image list, and you can then use ExtractIcon to pull an image out, or Add or Replace to add an image or replace one respectively.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks pal!
- I use the following codes, but it seems that it does not work.
void CMainFrame::OnEditCopy()
{
// TODO: Add your command handler code here
CImageList* list1=this->m_wndToolBar.GetToolBarCtrl().GetImageList();
list1->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
}
- Can you help me out?
- Regards,
Maer
|
|
|
|
|
I did this
m_wndToolBar.GetToolBarCtrl().GetImageList()->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
CWindowDC w(NULL);
w.DrawIcon(0, 0, m_wndToolBar.GetToolBarCtrl().GetImageList()->ExtractIcon(list1->GetImageCount()-1));
AfxMessageBox("");
in OnCreate on a vanilla SDI app, and the icon is there, plain as day. You didn't expect to see it in the toolbar, did you ? It's in the image list, but you need to create a new button or change the image index of an existing button for the toolbar to show it.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks pal!
- I use the following codes. But it has no effect.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
CImageList* list1=this->m_wndToolBar.GetToolBarCtrl().GetImageList();
m_wndToolBar.GetToolBarCtrl().GetImageList()->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
CWindowDC w(NULL);
w.DrawIcon(0, 0, m_wndToolBar.GetToolBarCtrl().GetImageList()->ExtractIcon(list1->GetImageCount()-1));
AfxMessageBox("");
return 0;
}
- Can you help?
- Have a nice weekend,
BigMouth
|
|
|
|
|
What do you mean by no effect ? Like I said, this will add the image to the imagelist, and draw it on the top left of your screen in order to prove that it's there. But you've still not added a button with this image, so it's still not going to be on the toolbar. You need to fill a TBBUTTON structure with info including the index of this image, then call AddButton to add that button to the toolbar.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks, Christian pal!
- Sorry, I have misunderstood your meaning before. I am a newbie of CToolBar. I have never modified it before. Now I find it is a hard work for me. So, if it does not take very much of your time, could you show me a simple example of your method?
- Have a nice weekend,
BigMouth
|
|
|
|
|
Sure - this is how you'd add a button with the bitmap we've been putting into the image list, which calls OnFileNew:
int index = m_wndToolBar.GetToolBarCtrl().GetImageList()->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
TBBUTTON btn;
btn.fsStyle = TBBUTTON;
btn.fsState = TBSTATE_ENABLED;
btn.idCommand = ID_FILE_NEW;
btn.iBitmap = index;
m_wndToolbar.GetToolBarCtrl().AddButtons(1, &btn);
Note we store the index of the image and pass it to the button, and we pass in the ID of the command we want the button to execute.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks, Christian pal!
- I think the statement 'btn.fsStyle = TBBUTTON;' should be written like this 'btn.fsStyle = TBSTYLE_BUTTON;'. IMHO
- And there is still a trouble. When I am testing your codes, the new toolbar button has no image attached to it.
- Can you help?
- Regards,
BigMouth
|
|
|
|
|
1. Absolutely correct, I posted in a hurry.
2. Then you've not set the image index to be the index of your new image in the image list.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks, warm-hearted Christian pal!
- Can you tell me how to set the image index to be the index of my new image in the image list? I find the work is easy to think, but it is hard to implement for a newbie, like me.
- Can you help?
- Regards,
BigMouth
|
|
|
|
|
OK - the line:
m_wndToolBar.GetToolBarCtrl().GetImageList()->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
returns the index of the new image, and so that is the index iBitmap needs to be set to when creating the new button. The code I posted previously actually did that, you're saying it's not working ?
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks, Christian!
- The method still has a problem. The newly added toolbar button has no image on it.
- Such are my codes: (OnCreate())
int index = m_wndToolBar.GetToolBarCtrl().GetImageList()->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
TBBUTTON btn;
btn.fsStyle = TBSTYLE_BUTTON;
btn.fsState = TBSTATE_ENABLED;
btn.idCommand = ID_FILE_NEW;
btn.iBitmap = index;
m_wndToolBar.GetToolBarCtrl().AddButtons(index, &btn);
m_wndToolBar.GetToolBarCtrl().GetImageList()->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
- Can you help me out?
- Regards,
BigMouth
|
|
|
|
|
I don't understand what the last line is for, why add the icon again ?
I also don't get why you need to do it this way, given that GetImageList returns a pointer, but here is what you need:
CImageList * pList = m_wndToolBar.GetToolBarCtrl().GetImageList();
int index = pList->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
m_wndToolBar.GetToolBarCtrl().SetImageList(pList);
TBBUTTON btn;
btn.fsStyle = TBSTYLE_BUTTON;
btn.fsState = TBSTATE_ENABLED;
btn.idCommand = ID_FILE_NEW;
btn.iBitmap = index;
m_wndToolBar.GetToolBarCtrl().AddButtons(1, &btn);
Note that you pass the number of buttons to add to addbuttons, not the index ( which is not even necessarily the index of the button you're adding ).
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks Christian!
- I tried your codes in the OnCreate function of an MDI app. It does not have the desired effect.
- Such is the effect:
1) It removes the 'help' toolbar button.
2) No new toolbar button appears.
3) All the toolbar button becomes a bit bigger.
- I do not know why? Have you tried your codes?
- Can you help, pal?
- Best regards and Merry Christmas to you,
BigMouth
|
|
|
|
|
I tested this code on an app at work, and again now on another app at home. It works fine.
Try it again, and try also to take the time top understand *why* it works. That is the path to not having to ask next time, and also to being able to figure stuff out for yourself.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks, Christian pal!
- Today, I run the app again. I find everything is Ok. But I never change the code!!! Just like yesterday!!!
- Ok, I am puzzled.
- Best regards,
BigMouth
|
|
|
|
|
- Christian pal!
- I still have a question. How to do the same task from another class? Like the view class? Can you give me a sample code?
- Can you help me?
- Best regards,
BigMouth
|
|
|
|
|
#include "MainFrm.h" at the top of the class, then
CMainFrame * pMain = (CMainFrame*)AfxGetMainWnd();
Then do it all the same, just access m_wndToolbar via pMain-> You may have to make it public, or make the view class a friend of CMainFrame.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks, Christian pal!
- I do not know why the method sometimes does not run stablly. Sometimes it runs ok, sometimes it does not run ok.
- Can you tell me why it happens? Need I refresh something manually?
- Best regards,
BigMouth
|
|
|
|
|
You need to run it when CMainFrame has been constructed and not destroyed.
When does it crash ?
You can do this:
CMainframe * pMain = dynamic_cast<cmainframe*>(AfxGetMainWnd());
if (pMain)
{
// use it here
}
This is how C++ does run time type information, and although it should *always* work in this case, it gives us a chance to catch if it doesn't. Whenever you request a pointer you should check it first, even if it's just with ASSERT.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
- Thanks, Christian pal!
- I still have a question. I think your codes are added to the function OnCreate, so it is sure that the CMainFrame has been constructed.
- So I think there is no need to test whether the "pMain" is NULL. IMHO
- Can you help?
- Merry Christmas,
BigMouth
|
|
|
|
|
I have a dialog based application which the user accesses through a password. Now there are 2 categories of users and depending on the login and password the application has to display 2 different user screens.
How do I do this?
Also when I first wrote this application I had just one user in mind so all the code is present in the dialog class CApplicationDlg. Is it possible for me to hide all the controls on this dialog box and create a new user screen on the same dialog box when the 2nd user type logs in?
Thanks
Mike.
|
|
|
|