Here is some snippet of code regarding dynamic creation of buttons (at runtime):
BOOL CSubmarinesGUIDlg::CreateDynamicButtons()
{
int numberOfButton = 1100;
CWnd *w=CWnd::FromHandle(GetSafeHwnd());
matrix = new CBitmapButton**[maxx];
for (int k = 0; k < maxx; k++)
matrix[k] = new CBitmapButton*[maxy];
for (int i=1;i<=maxx;i++)
for (int j=1; j<=maxy; j++)
{
CPoint p;
p.SetPoint(j*50,i*50);
CBitmapButton* btn = new CBitmapButton();
btn->Create(NULL,WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,CRec t(0+p.x,0+p.y,40+p.x,20+p.y),w ,numberOfButton);
btn->LoadBitmaps(IDB_BITMAP1,IDB_BITMAP1,IDB_BITMAP1 );
matrix[i-1][j-1] = btn;
numberOfButton++;
}
return TRUE;
}
Here is how to catch the click messages:
LRESULT CSubmarinesGUIDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if ((message == WM_COMMAND)) {
if (wParam>=1100) //ids for our buttons
{
int id = (int)wParam - 1100;
int i = id/maxy;
int j = id%maxy; // compute the coordinates using the id
matrix[i][j]->LoadBitmaps(IDB_BITMAP2,IDB_BITMAP2,IDB_BITMAP2 );
}
}
return CDialog::WindowProc(message, wParam, lParam);
}
|