Okay, I've used the STL vector alot and I've never had any problems until now. I can't figure out why this code isn't working. I use the debugger and when it gets the OnMouseMove() in the WM_MOUSEMOVE code block, it sets the m_bMouseHover to true (if the mouse is over it), but when it gets to WM_PAINT, its back to false.
#include "DevFuncs.h"
#include <vector>
LRESULT CALLBACK GuiInputProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
bool Frame();
class Widget
{
D3DXVECTOR2 m_Position;
UINT m_nWidth, m_nHeight;
bool m_bMouseHover;
public:
void Create(float fX, float fY, UINT nWidth, UINT nHeight)
{
m_Position = D3DXVECTOR2(fX, fY);
m_nWidth = nWidth;
m_nHeight = nHeight;
m_bMouseHover = false;
}
void Render(HDC hDC)
{
RECT Rect;
UINT nEdge = 0;
SetRect(&Rect, m_Position.x, m_Position.y, m_Position.x + m_nWidth, m_Position.y + m_nHeight);
if(m_bMouseHover)
nEdge = EDGE_SUNKEN;
else
nEdge = EDGE_RAISED;
DrawEdge(hDC, &Rect, nEdge, BF_RECT);
}
void OnMouseMove(WPARAM wParam, LPARAM lParam)
{
UINT nX = 0, nY = 0;
nX = LOWORD(lParam);
nY = HIWORD(lParam);
RECT Rect;
SetRect(&Rect, m_Position.x, m_Position.y, m_Position.x + m_nWidth, m_Position.y + m_nHeight);
if(nX > m_Position.x && nY > m_Position.y && nX < (m_Position.x + m_nWidth) && nY < (m_Position.y + m_nHeight))
m_bMouseHover = true;
else
m_bMouseHover = false;
}
};
std::vector<Widget*> g_Controls;
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
Widget* Ctrl1 = new Widget;
Ctrl1->Create(10, 10, 25, 25);
g_Controls.push_back(Ctrl1);
Widget* Ctrl2 = new Widget;
Ctrl2->Create(45, 35, 35, 30);
g_Controls.push_back(Ctrl2);
SetUpSystem(GuiInputProc);
StartLoop(Frame);
ShutDownSystem();
delete Ctrl2;
delete Ctrl1;
return 0;
}
bool Frame()
{
return true;
}
LRESULT CALLBACK GuiInputProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch(nMsg)
{
case WM_DESTROY:
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT Paint;
BeginPaint(hWnd, &Paint);
for(UINT nIndex = 0; nIndex < g_Controls.size(); ++nIndex)
{
Widget* pCtrl = g_Controls.at(nIndex);
pCtrl->Render(Paint.hdc);
}
EndPaint(hWnd, &Paint);
return 0;
}
case WM_MOUSEMOVE:
{
for(UINT nIndex = 0; nIndex < g_Controls.size(); ++nIndex)
{
Widget* pCtrl = g_Controls.at(nIndex);
pCtrl->OnMouseMove(wParam, lParam);
}
return 0;
}
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
Edit: I've also tried an iterator:
std::vector<Widget*>::iterator pItor = g_Controls.begin();
while(pItor != g_Controls.end())
{
Widget* pCtrl = ((Widget*)pItor);
pCtrl->Function();
++pItor;
}
But with the iterator it never even shows up (m_nWidth and m_nHeight are never assigned the values that they should be).
The code in OnMouseMove() works fine (it sets it to true when I'm above it, and false when I'm not), but for some reason it doesn't seem to be keeping the value.
Edit:
With the current setup, all of the values are good (i.e. the width and height are correct), but the changes are only temporary.
Thanks
[Edited by - Programmer16 on June 9, 2005 3:03:21 PM]