InvalidateRect doesn't nescessarily update the background immediately, that's why i used RedrawWindow with RDW_ERASENOW and RDW_UPDATENOW.
Printable View
InvalidateRect doesn't nescessarily update the background immediately, that's why i used RedrawWindow with RDW_ERASENOW and RDW_UPDATENOW.
Hi Marc G, what is a good book you would recommand for someone like me to read?
Thanks for your help again.
Jiac
Sorry, I can't recommend any book, because I never really read any book myself. I did once had a look at "teach yourself visual c++ in 21 days for VC 4", but that was it.
I learned everything by examining samples from and browsing through MSDN, codeguru, codeproject.
Hi Marc G,
(If you are around) or anyone, can you help? Basically with Marc G's code for OnEraseBkgnd, if I have the app open, and have another window overlapped on top of it, I would see the text being fregmented, meaning the text to where the overlapped window was would not get painted when the app becomes the active window. It will repaint itself if I hide/reappear the window.
What do I need to do?
Thank you.
Jiac
Try this version of MyStatic:
mystatic.h
mystatic.cppCode:#pragma once
class CMyStatic : public CStatic
{
DECLARE_DYNAMIC(CMyStatic)
public:
CMyStatic();
virtual ~CMyStatic();
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
BOOL m_bMouseHover;
};
Code:#include "stdafx.h"
#include ".\mystatic.h"
IMPLEMENT_DYNAMIC(CMyStatic, CStatic)
CMyStatic::CMyStatic()
: m_bMouseHover(FALSE)
{
}
CMyStatic::~CMyStatic()
{
}
BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
END_MESSAGE_MAP()
// CMyStatic message handlers
void CMyStatic::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bMouseHover)
{
CRect rcClient;
GetClientRect(&rcClient);
if (!rcClient.PtInRect(point))
{
m_bMouseHover = FALSE;
ReleaseCapture();
Invalidate(TRUE);
}
}
else
{
m_bMouseHover = TRUE;
Invalidate(TRUE);
SetCapture();
}
CStatic::OnMouseMove(nFlags, point);
}
BOOL CMyStatic::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CMyStatic::OnPaint()
{
// Let the parent window draw itself
CRect rcWnd;
GetWindowRect(&rcWnd);
CWnd* pParent = GetParent();
pParent->ScreenToClient(&rcWnd);
pParent->RedrawWindow(&rcWnd, 0, RDW_INVALIDATE | RDW_UPDATENOW);
// Draw our text
CPaintDC dc(this); // device context for painting
if (m_bMouseHover)
dc.SetTextColor(RGB(255,0,0));
else
dc.SetTextColor(RGB(0,0,0));
dc.SetBkMode(TRANSPARENT);
CFont font;
DWORD dwVersion = GetVersion();
if (dwVersion < 0x80000000) // Windows NT or later
font.CreateStockObject(DEFAULT_GUI_FONT);
else
font.CreateStockObject(ANSI_VAR_FONT);
CFont* pOldFont = dc.SelectObject(&font);
dc.TextOut(0,0,"Test string");
dc.SelectObject(pOldFont);
// Do not call CStatic::OnPaint() for painting messages
}