I wanna make a large toolbar with support of icons with more colors depth than default in Visual Studio. I am using Visual Studio 2005 and the Toolbar is on a CDialog.
I used the Code found : here
but did not work.
int CSalariesForm::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDR_MAINFRAME1), IMAGE_BITMAP,
0,0, LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
CBitmap bm;
bm.Attach(hBitmap);
CImageList m_imagelist;
m_imagelist.Create(20, 20, ILC_COLOR8, 4, 4);
m_imagelist.Add(&bm, (CBitmap*) NULL);
cToolBar.Create(this);
cToolBar.GetToolBarCtrl().SetImageList(&m_imagelist);
cToolBar.ShowWindow(SW_SHOW);
cToolBar.SetBarStyle(CBRS_TOOLTIPS | CBRS_FLOATING | CBRS_ALIGN_TOP | CBRS_FLYBY);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
return 0;
}
And when I call the dialog there is no Toolbar shown. What is wong with the code?
Thank You
Assigning an image list to a toolbar does not create any toolbar buttons. Also your image list should be a class member, not a local variable.
// header file
private:
CImageList m_imagelist;
CToolBar m_toolbar;
// source file
enum { width = 20, height = 20 }; // width and height of one button image
m_toolbar.Create(this);
// create the image list
m_imagelist.Attach(
ImageList_LoadImage(
AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1), width, 4,
CLR_DEFAULT, IMAGE_BITMAP, LR_CREATEDIBSECTION | LR_LOADTRANSPARENT )
);
// set button and image sizes (copied from CToolBar::LoadToolBar)
m_toolbar.SetSizes(CSize(width+7,height+7), CSize(width,height));
// set image list
m_toolbar.GetToolBarCtrl().SetImageList(&m_imagelist);
// define command ids for each button
const UINT cmdIds[] = {
IDOK,
0, // separator
IDCANCEL,
};
// assign ids to the toolbar
m_toolbar.SetButtons(cmdIds, sizeof(cmdIds)/sizeof(UINT));
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
IDB_BITMAP1 is a 40x20 24bit color bitmap (two 20x20 buttons). If you need more control over the creation of the buttons, you can use CToolBarCtrl::SetButtons() instead. Refer to ImageList_LoadImage for more details on loading the image list.
Related
I have an issue Controlling CTabControl Inner tab items with TAB and Arrow keys.
here is my code and a few screenshots:
OnInitDialog() method of the main dialog window:
BOOL PressetsDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// AUTO GENERATED MFC DIALOGUE CODE HERE
...
// ..
// TODO: Add extra initialization here
CTabCtrl* pTabCtrl = (CTabCtrl*)GetDlgItem(IDC_TAB1);
m_one.Create(IDD_TAB_ONE, pTabCtrl);
CTabCtrl* pTabCtrl2 = (CTabCtrl*)GetDlgItem(IDC_TAB1);
m_two.Create(IDD_TAB_TWO, pTabCtrl2);
TCITEM item1, item2, item3;
item1.mask = TCIF_TEXT | TCIF_PARAM;
item1.lParam = (LPARAM)&m_one;
item1.pszText = L"Normal Presets";
pTabCtrl->InsertItem(0, &item1);
item2.mask = TCIF_TEXT | TCIF_PARAM;
item2.lParam = (LPARAM)&m_two;
item2.pszText = L"Movement Presets";
pTabCtrl2->InsertItem(1, &item2);
CRect rcItem;
pTabCtrl->GetItemRect(0, &rcItem);
m_one.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
CRect rcItem2;
pTabCtrl2->GetItemRect(0, &rcItem2);
m_two.SetWindowPos(NULL, rcItem2.left, rcItem2.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
m_one.ShowWindow(SW_SHOW);
m_two.ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
}
and the OnTcnSelchangeTab method:
void PressetsDlg::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int nSelect = m_Tab.GetCurSel();
if (nSelect == 0)
{
m_one.ShowWindow(SW_SHOW);
m_two.ShowWindow(SW_HIDE);
}
else if (nSelect == 1)
{
m_two.ShowWindow(SW_SHOW);
m_one.ShowWindow(SW_HIDE);
}
else
{
m_one.ShowWindow(SW_SHOW);
m_two.ShowWindow(SW_HIDE);
}
*pResult = 0;
}
[to see the tab design click here](https://i.stack.imgur.com/Tt8c1.jpg)
I've set the tab order with Ctrl + D for each dialogue resource and set Tabstop property to either True or False and still nothing happens.
At first I thought that this feature is supposed to be supported automatically but it seems that it's not.
the dialogue window moves between tabs and buttons that placed on it but as soon as I try to move to "inner Items" of each tab, it doesn't reach them.
I suspect the reason is probably that each tab is a separate window and that's probably the reason that the inner items are unreachable..
I've made a few changes and now Moving with TAB and arrow keys functions properly.
first of all I set the Control Property of both child dialogs to True.
You go to Resource View >> (Solution name) >> (project name) >> IDD + (the id you gave to the dialog) doble click on it >> Properties >> Control.
Essentially what it did is to add the flag WS_CONTROL as mentioned here early to each of the Child windows so that they could be accessed from the main dialog window that contains them.
Of course that alone didn't do much because I also had a few bugs in the code, after days of searching for it I found an example online which helped me solve the bugs.
Then I've changed my code to this: and it started working:
// first we create two modeless dialogs and embed them as child windows
// of CTabControlTutorialDlg.
// Have a look using Spy++ to see the layout of the controls as they
// appear to windows
m_one.Create(IDD_TAB_ONE, this);
m_two.Create(IDD_TAB_TWO, this);
// next we get the captions of the dialogs and use these as the caption
// for the tab window. Of course we could just load a string from the
// resources or hard code a string for the text of the tab.
TCITEM item1, item2;
item1.mask = TCIF_TEXT | TCIF_PARAM;
item1.lParam = (LPARAM)&m_one;
item1.pszText = L"Normal Presets";
m_Tab.InsertItem(0, &item1);
item2.mask = TCIF_TEXT | TCIF_PARAM;
item2.lParam = (LPARAM)&m_two;
item2.pszText = L"Custom Presets";
m_Tab.InsertItem(1, &item2);
// finally we set the tab order correctly for the so that we can tab through the dialogs and into
// the cancel and ok buttons. If we don;t do this then the tab order is tab control, ok button, cancel
// button embedded dialogs.
CRect rcItem;
m_Tab.GetItemRect(0, &rcItem);
m_one.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 5, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER);
CRect rcItem2;
m_Tab.GetItemRect(0, &rcItem2);
m_two.SetWindowPos(NULL, rcItem2.left, rcItem2.bottom + 5, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER);
m_one.ShowWindow(SW_SHOW);
I found reference in this site:https://www.codeproject.com/Articles/4408/Creating-embedded-dialogs-in-MFC but in order to get their code example you need to register to the website.
It works!
I am making dialog based programs with MFC.
How do I change the text of the edit control to bold or change the size?
Where and what kind of code should I write?
Please tell me specifically.
In your OnInitDialog, create a CFont object (declared in your dialog)
m_pFont=new CFont;
int lfHeight;
lfHeight = (int) -(dSize* 90 / 72.);
int nWeight=FW_NORMAL;
if ( isBold )
nWeight=FW_BOLD;
pFont->CreateFont( lfHeight , 0, 0, 0, nWeight, (BYTE)isItalic, (BYTE)isUnderline, 0, (BYTE)nCharSet, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, _T("Arial") );
And then call SetFont(m_pFont) for your edit control - delete it in your destructor.
I created an Outlook bar with tree controls and would like to have it auto size to always fully display the text of the tree control. Ideally the border in the picture would move so that "Healthcare Merchandising" is fully visible.
newDimbar is a CMFCOutlookBar object created in CMainFrame.
I have tried stretching it:
void CTreeDrill::OnTvnItemexpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
HTREEITEM hItem = pNMTreeView->itemNew.hItem;
RECT treeRect;
GetItemRect(GetChildItem(hItem), &treeRect, FALSE);
CMainFrame *pMain = (CMainFrame*)AfxGetMainWnd();
int iStretch = treeRect.right;
pMain->m_ctlNewDimBar.StretchPane(iStretch, FALSE);
EnsureVisible(GetChildItem(hItem));
}
and using move:
void CTreeDrill::OnTvnItemexpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
RECT treeRect;
RECT newRect;
RECT dimRect;
GetItemRect(GetChildItem(hItem), &treeRect, FALSE);
CMainFrame *pMain = (CMainFrame*)AfxGetMainWnd();
pMain->m_ctlNewDimBar.GetWindowRect(&dimRect);
newRect = dimRect;
newRect.right = treeRect.right;
pMain->m_ctlNewDimBar.MoveWindow(newRect);
EnsureVisible(GetChildItem(hItem));
}
without luck. What am I missing?
Solution
I'm happy i found it because this is something i would have to do too in the future, you need to use CWnd::SetWindowPos to change the CMFCOutlookBar's size, check this tutorial that has more infos, after you change the size of CMFCOutlookBar you will have to use CFrameWndEx::RecalcLayout method of the frame that contains the CMFCOutlookBar.
Why using CWnd::SetWindowPos?
i don't know, it's the only one that worked for me
Why use CFrameWndEx::RecalcLayout and not only call RecalcLayout() of the CMFCOutlookBar?
Because if you just recalculate the layout of the CMFCOutlookBar only the CMFCOutlookBar will be updated and then if you have anything attached to the CMFCOutlookBar it will not recieve the changes, so you might end with your CMFCOutlookBar overlapping some other control or your document's view, calling CFrameWndEx::RecalcLayout will make the whole frame reacalculate and so if you have tabbed document views they will be updated/resized accordingly.
Your case
you will have to calculate the whole width of the tree, not only the item, and then use the CWnd::SetWindowPos on the CMFCOutlookBar with the updated value width value but keeping the height of the CMFCOutlookBar.
newDimbar.GetWindowRect(pos);
ScreenToClient(&pos);
UINT flags = SWP_NOZORDER | SWP_NOMOVE;
newDimbar.SetWindowPos(NULL, 0, 0, iNewWidth, pos.Height(), flags);
How do you add Images to a ClistCtrl in MFC? I have tried and found that it's quite difficult.
I used CImageList to add images and then passed it to the CListCtrl. Can you provide some samples?
m_sentToCListCtrl.InsertColumn(0, _T("Item Name"), LVCFMT_LEFT,nColInterval*3);
m_sentToCListCtrl.InsertColumn(1, _T("Value"),LVCFMT_LEFT, nColInterval);
m_sentToCListCtrl.InsertColumn(2, _T("Time"), LVCFMT_LEFT, rect.Width()-4*nColInterval);
ListView_SetExtendedListViewStyle(m_sentToCListCtrl.m_hWnd,LVS_EX_CHECKBOXES );
// Create 256 color image lists
HIMAGELIST hSentToList =ImageList_Create(84,71, ILC_COLOR8 |ILC_MASK , 8, 1);
m_sentToImageList.Attach(hSentToList);
You need to add some bitmaps to your CImageList after you have created it. Something like this:
m_myImageList.Create(84,71, ILC_COLOR8 |ILC_MASK , 8, 1);
CBitmap bm;
bm.LoadBitmap(IDB_BITMAP1);
m_myImageList.Add(&bm, RGB(0, 0, 0));
bm.LoadBitmap(IDB_BITMAP2);
m_myImageList.Add(&bm, RGB(0, 0, 0));
Then, attach it to the CListCtrl:
m_sentToCListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);
Finally, you add items to your CListCtrl by using the InsertItem method:
LVITEM lvItem;
lvItem.iItem = 0;
lvItem.iImage = 0; // image index that refers to your image list
lvItem.pszText = L"Item 1";
lvItem.mask = LVIF_TEXT;
m_sentToCListCtrl.InsertItem(&lvItem);
For more info refer to CListCtrl documentation. There are examples too.
I have prepared a screen in which I am allowing user to create an account. as shown in the first image I have used an image(bg_BB.png image) as MainScreen Background, after that i have taken another VFM and painting that white Background (white_bg2.png)on that vertical field manager and ADDING ALL MY FIELD ON THAT VFM.
But the problem arises when the keyboard is pops-up. All the fields apears to be floating over the background as shown in the second pic.
Below is the code which I am using:
Bitmap backGroundImage = Bitmap.getBitmapResource("bg_BB.png");
((VerticalFieldManager) getMainManager()).setBackground(BackgroundFactory.createBitmapBackground(backGroundImage));
final Bitmap tabBackGroundImage = Bitmap.getBitmapResource("white_bg2.png");
_mainVfm = new VerticalFieldManager(Field.USE_ALL_WIDTH) {
protected void paint(Graphics graphics) {
int y = CreateUserAccountScreen.this.getMainManager().getVerticalScroll();
graphics.drawBitmap(0, y,
tabBackGroundImage.getWidth(),
tabBackGroundImage.getHeight(),
tabBackGroundImage,
0, 0 );
super.paint( graphics );
}
};
replace your code with:
Bitmap tabBackGroundImage = Bitmap.getBitmapResource("white_bg2.png");
VerticalFieldManager _mainVfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL |
Manager.VERTICAL_SCROLLBAR|
Manager.USE_ALL_WIDTH);
_mainVfm.setBorder( BorderFactory.createBitmapBorder(
new XYEdges(12,12,12,12), tabBackGroundImage
)
);
make sure that your border image have white background.
i use this method and it works perfectly.