eric_m
asked on
customizing CStatusBar
Is there an easy way to change the thickness of the borders
between panes in a status bar? I found m_cxLeftBorder,
m_cxRightBorder, and m_cxDefaultGap as data in CControlBar.
I tried changing them, and the left and right border data
members work just as I expected...they alter the thickness
of the left and right borders. I can't see that
m_cxDefaultGap changes anything, though.
between panes in a status bar? I found m_cxLeftBorder,
m_cxRightBorder, and m_cxDefaultGap as data in CControlBar.
I tried changing them, and the left and right border data
members work just as I expected...they alter the thickness
of the left and right borders. I can't see that
m_cxDefaultGap changes anything, though.
ASKER
Could you please show me an example of how to override the
DrawItem to set the width? Your answer doesn't provide me
with enough clues on how to do that. Thanks!
DrawItem to set the width? Your answer doesn't provide me
with enough clues on how to do that. Thanks!
1. You need to set up the pane of the status bar as owner draw (for whichever pane you want).
GetStatusBarCtrl().SetText ( (LPCTSTR)1 /* Pass as data to the owner draw proc */, 0 /* index of pane */, SBT_OWNERDRAW /* Flag for owner draw */ ) ;
A good place to do this is when you set up the status bar
2. Derive a status bar class from CStatusBar. The Class Wizard can do this for you. Create one of these rather than the default status bar.
3. In your derived class override DrawItem for the Status Bar (if you look in the help you will see only a DrawItem for CStatusBarCtrl, but the same function exists for CStatusBar). In this function you get an LPDRAWITEMSTRUCT (pointer to a draw item structure) which contains the data you need to draw the pane including it's rectangle. [note:if class wizard probably won't let you do it, so you need to manually override]. It should look like this
in a .h
class CMyStatusBar : public CStatusBar
{
// various stuff
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
} ;
in .cpp
virtual void CMyStatusBar::DrawItem(LPD RAWITEMSTR UCT lpDrawItemStruct)
{
// <-- add you're code here
}
In the drawing code paint what ever you want. The most important elements of lpDrawItemStruct are hDC (the DC to draw on) and rcItem (the rectangle of coordinates in pixels).
e.g.
virtual void CMyStatusBar::DrawItem(LPD RAWITEMSTR UCT lpDrawItemStruct)
{
// fill the pane with red for example
CDC::FromHandle(lpDrawItem Struct->hD C)->FillSo lidRect( &(lpDrawItemStruct->rcItem ), RGB(255,0,0) ) ;
}
Inside the brackets you can add as much drawing code as you need using the standard drawing functions like Rectangle, LineTo, MoveToEx/MoveTo, DrawText etc.
Hopefully this is enough to get you going.
GetStatusBarCtrl().SetText
A good place to do this is when you set up the status bar
2. Derive a status bar class from CStatusBar. The Class Wizard can do this for you. Create one of these rather than the default status bar.
3. In your derived class override DrawItem for the Status Bar (if you look in the help you will see only a DrawItem for CStatusBarCtrl, but the same function exists for CStatusBar). In this function you get an LPDRAWITEMSTRUCT (pointer to a draw item structure) which contains the data you need to draw the pane including it's rectangle. [note:if class wizard probably won't let you do it, so you need to manually override]. It should look like this
in a .h
class CMyStatusBar : public CStatusBar
{
// various stuff
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
} ;
in .cpp
virtual void CMyStatusBar::DrawItem(LPD
{
// <-- add you're code here
}
In the drawing code paint what ever you want. The most important elements of lpDrawItemStruct are hDC (the DC to draw on) and rcItem (the rectangle of coordinates in pixels).
e.g.
virtual void CMyStatusBar::DrawItem(LPD
{
// fill the pane with red for example
CDC::FromHandle(lpDrawItem
}
Inside the brackets you can add as much drawing code as you need using the standard drawing functions like Rectangle, LineTo, MoveToEx/MoveTo, DrawText etc.
Hopefully this is enough to get you going.
ASKER
Answers2000:
Thanks for your help so far. I'm still falling short,
though.
The default borders between panes in CStatusBar extend over
the shadows of the status pane. They continue drawing one
pixel over and one pixel under the client area defined as
lpdrawitemstruct.rcItem. I tried to do the same, but
Windows has probably set a clipping region so I don't draw
outside my little box. In other words, I can't draw over
the bottom highlighted rectangle line, like the default
borders do.
The default borders, if they were thick enough, would line
up perfectly with splitter bars, which is what I'm trying to
do. By drawing them myself, I'm left with a one pixel gap
in "no man's land" so to speak.
void MyStatusBar::DrawItem(LPDR AWITEMSTRU CT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItem Struct->hD C);
// arbitrarily chosen
const int leftSide = 101;
const int rightSide = 103;
const int topSide = lpDrawItemStruct->rcItem.t op - 1;
const int bottomSide = lpDrawItemStruct->rcItem.b ottom + 1;
pDC->FillSolidRect(&(lpDra wItemStruc t->rcItem) , afxData.clrBtnFace);
pDC->FillSolidRect(leftSid e - 1,
topSide,
leftSide - 1,
bottomSide,
afxData.clrBtnHilite);
pDC->FillSolidRect(leftSid e,
topSide,
rightSide,
bottomSide,
afxData.clrBtnFace);
pDC->FillSolidRect(rightSi de + 1,
topSide,
rightSide + 1,
bottomSide,
afxData.clrBtnShadow);
pDC->FillSolidRect(rightSi de + 2,
lpDrawItemStruct->rcItem.t op,
lpDrawItemStruct->rcItem.r ight,
lpDrawItemStruct->rcItem.b ottom,
afxData.clrBtnFace);
}
Thanks for your help so far. I'm still falling short,
though.
The default borders between panes in CStatusBar extend over
the shadows of the status pane. They continue drawing one
pixel over and one pixel under the client area defined as
lpdrawitemstruct.rcItem. I tried to do the same, but
Windows has probably set a clipping region so I don't draw
outside my little box. In other words, I can't draw over
the bottom highlighted rectangle line, like the default
borders do.
The default borders, if they were thick enough, would line
up perfectly with splitter bars, which is what I'm trying to
do. By drawing them myself, I'm left with a one pixel gap
in "no man's land" so to speak.
void MyStatusBar::DrawItem(LPDR
{
CDC* pDC = CDC::FromHandle(lpDrawItem
// arbitrarily chosen
const int leftSide = 101;
const int rightSide = 103;
const int topSide = lpDrawItemStruct->rcItem.t
const int bottomSide = lpDrawItemStruct->rcItem.b
pDC->FillSolidRect(&(lpDra
pDC->FillSolidRect(leftSid
topSide,
leftSide - 1,
bottomSide,
afxData.clrBtnHilite);
pDC->FillSolidRect(leftSid
topSide,
rightSide,
bottomSide,
afxData.clrBtnFace);
pDC->FillSolidRect(rightSi
topSide,
rightSide + 1,
bottomSide,
afxData.clrBtnShadow);
pDC->FillSolidRect(rightSi
lpDrawItemStruct->rcItem.t
lpDrawItemStruct->rcItem.r
lpDrawItemStruct->rcItem.b
afxData.clrBtnFace);
}
Hmm didn't know Windows would set a clipping region (it doesn't for most other owner draw controls)
I'm Not working with a sample, but could you SelectClipRgn at the start of DrawItem to remove the Clipping region, and then paint where-ever ?
I'm Not working with a sample, but could you SelectClipRgn at the start of DrawItem to remove the Clipping region, and then paint where-ever ?
ASKER
Answers2000:
Thanks for your help! Adding to the region did the job.
Please re-answer so I can give you the points. :)
CRgn myRegion;
myRegion.CreateRectRgn(lef tSide-1,
topSide,
rightSide + 1,
bottomSide);
pDC->SelectClipRgn(&myRegi on, RGN_OR);
Thanks for your help! Adding to the region did the job.
Please re-answer so I can give you the points. :)
CRgn myRegion;
myRegion.CreateRectRgn(lef
topSide,
rightSide + 1,
bottomSide);
pDC->SelectClipRgn(&myRegi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help and patience. :)
The way around.
1. Create a dummy item between each real item in the status bar, using SetParts (in CStatusBarCtrl - which you can get use GetStatusBarCtrl())
2. Set dummy parts to have no text and no border (SBT_NOBORDERS) using SetText member of CStatusBarCtrl.
3. Control width of "pseudo" borders by changing the width of the dummy items
OR
1. Override DrawItem for the control
2. Paint each item as you like with the borders you want
3. This is more work, but will give you finer control