|
the following code is from the examples on the book
[code]
CPen pen (PS_SOLID, 0, RGB (192, 192, 192));
CPen* pOldPen = dc.SelectObject (&pen);
//do some drawing staff here
dc.SelectObject (pOldPen);
I know once I create a pen, I can do some drawing staff, but why should I add "CPen* pOldPen = dc.SelectObject (&pen)" and "dc.SelectObject (pOldPen)" here? what's the use of that?
|
|
|
|
|
It's called restoring the DC to its original state.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
sorry, what is that mean? could you explain it a little detailed?
what is the difference if I had not use it?
|
|
|
|
|
bloodwinner wrote: what is the difference if I had not use it?
You would get a "resource leak" (a GDI handle would be lost, and if your app runs for a long time, this leak would accumulate, which isn't good - leaks like this can easily crash Win9x for example). Basically, if a GDI object such as a pen, brush or font is still selected into a DC when it is destroyed, you will get a leak:
void Draw(CDC& dc)
{
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
dc.SelectObject(&pen);
...
}
To avoid this, do as your sample code shows (save the previous object when calling SelectObject ), or save/restore the DC state like this:
void Draw(CDC& dc)
{
int nState = dc.SaveDC();
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
dc.SelectObject(&pen);
...
dc.RestoreDC(nState);
}
Last modified: 11hrs 24mins after originally posted --
Kicking squealing Gucci little piggy.
|
|
|
|
|
That type of restore is more costly if I recall correctly. SaveState() saves everything. So, if you're only using a brush for instance, SaveState() will give you lots of overhead. On the other hand, CPU cycles aren't really a scarce resource these days..
--
A Stern Warning of Things to Come
|
|
|
|
|
SaveState()???
I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?
|
|
|
|
|
Nah, Rob and I are just senile, and meant SaveDC/RestoreDC.
(Unless Rob really do know about some undocumented funcions )
--
A Stern Warning of Things to Come
|
|
|
|
|
Ooops. Yes, I meant SaveDC/RestoreDC - it was late when I made the post!
Kicking squealing Gucci little piggy.
|
|
|
|
|
Me too I wrote a program with SaveDC and RestoreDC
|
|
|
|
|
How do I determine which CSlider is sending a message?
1. Dialog CExoSliderDlg with two sliders and two edit boxes.
2. Both sliders work, and both deliver nPos which is used to update edit box variable m_SliderValue.
3. I want IDC_SLIDER to update m_SliderValue
4. I want IDC_SLIDER2 to update m_SliderValue2.
5. I think CScrollBar* pScrollBar can tell me which slider is active.
6. I don't know how to determine which slider is active. How to do it?
IDC_SLIDER_VALUE CString m_SliderValue
IDC_SLIDER_VALUE2 CString m_SliderValue2
IDC_SLIDER CSliderCtrl m_Slider
IDC_SLIDER2 CSliderCtrl m_Slider2
void CExoSliderDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if(nSBCode == SB_THUMBPOSITION)
{
m_SliderValue.Format("%ld", nPos);
UpdateData(false);
}
else
{
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
}
|
|
|
|
|
Oliver123 wrote: 5. I think CScrollBar* pScrollBar can tell me which slider is active.
This is correct.
Oliver123 wrote: IDC_SLIDER_VALUE CString m_SliderValue
IDC_SLIDER_VALUE2 CString m_SliderValue2
Use CEdit instead of CString . Update the edit controls using SetWindowText() .
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
OK. But the fog still lingers a bit.
But how do I determine which slider is active? How do I use CScroll* pScrollBar to determine which of the two sliders is sending the message?
I feel like the answer is right in front of me, but I just can't grab it.
Thanks
|
|
|
|
|
I found some code that I adapted to my application. It works. It allows me to determine which of the two sliders is active, and then set the appropriate edit box to the value generated by the slider. I don't follow the reinterpret_cast<csliderctrl *="">, but it works. Here is the resulting function. (The @ are not in the code. The @ should be replaced by brackets formed with < and >, but I can't get this editor to accept them.):
void CExoSliderDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int iActiveSlider = 0;
CSliderCtrl *Slider, *Slider2;
// Identify the sliders of the dialog box using their IDentifiers
Slider = reinterpret_cast@CSliderCtrl *@(GetDlgItem(IDC_SLIDER));
Slider2 = reinterpret_cast@CSliderCtrl *@(GetDlgItem(IDC_SLIDER2));
if( reinterpret_cast@CSliderCtrl *@(pScrollBar) == Slider )
{
iActiveSlider = 1;
}
if( reinterpret_cast@CSliderCtrl *@(pScrollBar) == Slider2 )
{
iActiveSlider = 2;
}
if(nSBCode == SB_THUMBPOSITION)
{
if (iActiveSlider ==1)
{
m_SliderValue.Format("%ld", nPos);
UpdateData(false);
}
if (iActiveSlider ==2)
{
m_SliderValue2.Format("%ld", nPos);
UpdateData(false);
}
}
else
{
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
}
-- modified at 21:00 Friday 22nd September, 2006
|
|
|
|
|
You can either type in < for < and > for >, or click the checkbox labeled "Ignore HTML tags in this message (good for code snippets)" (it's located just under the smileys).
--
A Stern Warning of Things to Come
|
|
|
|
|
Oliver123 wrote: But how do I determine which slider is active? How do I use CScroll* pScrollBar to determine which of the two sliders is sending the message?
void CExoSliderDlg::OnHScroll( UINT uSBCode, UINT uPos, CScrollBar *pScrollBar )
{
CString strValue;
strValue.Format("%u", uPos);
if ((CSliderCtrl *) pScrollBar == &m_slider1)
m_edit1.SetWindowText(strValue);
else if ((CSliderCtrl *) pScrollBar == &m_slider2)
m_edit2.SetWindowText(strValue);
else
CDialog::OnHScroll(uSBCode, uPos, pScrollBar);
}
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Taset wrote: I'm wishing to tweak the appearance of every form in my application. What I want is to change (for example) its border, scrollbar or caption's width/height...
Wouldn't your users get a little upset that their preferences were being ignored?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hello,
I have created a dialog box and menu in the resource editor. How do I attach the menu to the dialog box? Any help is appreciated.
Mike
|
|
|
|
|
mla154 wrote: How do I attach the menu to the dialog box?
Using the same resource editor that you used to create the dialog and the menu.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Go to Dialog properties->General tab. in Menu option select the id of the menu.
KIRAN PINJARLA
|
|
|
|
|
It worked! Thanks for your help.
Mike
|
|
|
|
|
mla154 wrote: I have created a dialog box and menu in the resource editor. How do I attach the menu to the dialog box? Any help is appreciated.
Select the menu id in the dialog box editor
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief And you
|
|
|
|
|
hi,
I want to handle vertical scrolling messages sent from a CListView control, so I mapped WM_VSCROLL message.. it works fine but I dont receive a message when I scroll the control by draging the scroll box and I do not know why!!!
any ideas?
Mohammad
And ever has it been that love knows not its own depth until the hour of separation
|
|
|
|
|
List controls use the LVM_SCROLL message not WM_HSCROLL and WM_VSCROLL .
|
|
|
|
|
Hi,
thank you, but how can I receive this message, and how is it received in the parent window of the control?? I cant see it in the class wizard..
i tried to use the WM_NOTIFY message of the parent but it doesnt work... and no luck with MSDN..
Can u provide further help??
Thanks
Mohammad
And ever has it been that love knows not its own depth until the hour of separation
|
|
|
|
|
look at "dc.SelectObject(&pen)" and "dc.SelectObject (&brush)",here no matter I use "&" or not it results the same. why is that?
[code]
void CMainWindow::OnPaint ()
{
CPaintDC dc (this);
CBrush brush (RGB (255, 0, 0));
CPen pen (PS_NULL, 0, (RGB (0, 0, 0)));
dc.SelectObject(&pen);
dc.SelectObject (&brush);
dc.Ellipse (0, 0, 200, 100);
}
|
|
|
|