void CSudokuView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
void CSudokuView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit(FALSE);
}
private:
bool m_bInitialised;
CButton m_arWndButtons[81];
};
CSudokuView::CSudokuView()
: CFormView(CSudokuView::IDD)
, m_bInitialised(false)
{
if(!m_bInitialised)
{
//Set the flag - prevent reentry and duplication of effort.
m_bInitialised = true;
//Attach the controls in the dialog editor to our array of buttons
for(int i = 0; i < 81; i++)
m_arWndButtons[i].SubclassDlgItem(IDC_BUTTON1 + i, this);
//Get width and height of a button
CRect rcBtn;
GetDlgItem(IDC_BUTTON1)->GetWindowRect(rcBtn);
int iBtnWidth = rcBtn.Width();
int iBtnHeight = rcBtn.Height();
//Get the position of the frame
CRect rcFrame;
GetDlgItem(IDC_STATIC_BOUNDARY)->GetWindowRect(rcFrame);
//Convert from screen to client area of the application coordinates
ScreenToClient(&rcFrame);
//Size of the 'mini' Frame blocks
int iOffsetX = rcFrame.Width() / 3;
int iOffsetY = rcFrame.Height() / 3;
//Calculate the spacing inside the mini blocks
int iSpaceX = (iOffsetX - 3*iBtnWidth)/2;
int iSpaceY = (iOffsetY - 3*iBtnHeight)/2;
//First row is offset from the frame
int x, y = rcFrame.top + iSpaceY;
//Position one row at a time
for(int row = 0; row < 9; row++)
{
//Location of first button, offset a bit from the frame
x = rcFrame.left + iSpaceX;
//Now process the nine buttons in the row
for(int col = 0; col < 9; col++)
{
//NULL - A window used in the tab ordering, SWP_NOZORDER tells system to ignore that parameter
m_arWndButtons[row * 9 + col].SetWindowPos(NULL, x, y, iBtnWidth-1, iBtnHeight-1, SWP_NOZORDER);
//This is to provide a little extra spacing to display groups of three
if(col == 2)
x = rcFrame.left + iOffsetX + iSpaceX;
else if (col == 5)
x = rcFrame.left + 2*iOffsetX + iSpaceX;
else
x += iBtnWidth;
}
//This is to provide a little extra spacing to display groups of three
if(row == 2)
y = rcFrame.top + iOffsetY + iSpaceY;
else if(row == 5)
y = rcFrame.top + 2*iOffsetY + iSpaceY;
else
y += iBtnHeight;
}
}
void CSudokuView::OnDraw(CDC* /*pDC*/)
{
// TODO: Add your specialized code here and/or call the base class
}
void CSudokuView::OnDraw(CDC* pDC)
{
CRect rcFrame;
GetDlgItem(IDC_STATIC_BOUNDARY)->GetWindowRect(&rcFrame);
ScreenToClient(&rcFrame);
int iOffsetX = rcFrame.Width() / 3;
int iOffsetY = rcFrame.Height() / 3;
pDC->MoveTo(rcFrame.left + iOffsetX, rcFrame.top);
pDC->LineTo(rcFrame.left + iOffsetX, rcFrame.bottom);
pDC->MoveTo(rcFrame.left + 2*iOffsetX, rcFrame.top);
pDC->LineTo(rcFrame.left + 2*iOffsetX, rcFrame.bottom);
pDC->MoveTo(rcFrame.left, rcFrame.top + iOffsetY);
pDC->LineTo(rcFrame.right, rcFrame.top + iOffsetY);
pDC->MoveTo(rcFrame.left, rcFrame.top + 2*iOffsetY);
pDC->LineTo(rcFrame.right, rcFrame.top + 2*iOffsetY);
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
WINDOWPLACEMENT wp;
if(ReadWindowPlacement(&wp))
{
wp.length = sizeof(wp);
SetWindowPlacement(&wp);
}
else
{
CenterWindow();
}
protected:
bool ReadWindowPlacement(WINDOWPLACEMENT *pWP);
};
bool CMainFrame::ReadWindowPlacement(WINDOWPLACEMENT *pWP)
{
CRegKey regKey;
long lRes;
CString szRegKey;
szRegKey.Format("%s\\%s", "SOFTWARE\\AndyAinscow", "Sudoku");
char szBuffer[sizeof("-32767")*8 + sizeof("65535")*2 + sizeof(" ")];
ZeroMemory(szBuffer, sizeof(szBuffer));
DWORD dwSize;
lRes = regKey.Open(HKEY_CURRENT_USER, szRegKey, KEY_READ);
if(lRes == ERROR_SUCCESS)
{
dwSize = sizeof(szBuffer)/sizeof(TCHAR);
lRes = regKey.QueryStringValue("MainWindow", szBuffer, &dwSize);
if(lRes != ERROR_SUCCESS)
{
return false;
}
}
else
return false;
char szFormat[] = "%u,%u,%d,%d,%d,%d,%d,%d,%d,%d";
int nRead = sscanf_s(szBuffer, szFormat,
&pWP->flags, &pWP->showCmd,
&pWP->ptMinPosition.x, &pWP->ptMinPosition.y,
&pWP->ptMaxPosition.x, &pWP->ptMaxPosition.y,
&pWP->rcNormalPosition.left, &pWP->rcNormalPosition.top,
&pWP->rcNormalPosition.right, &pWP->rcNormalPosition.bottom);
if (nRead != 10)
return false;
return true;
}
void CMainFrame::OnClose()
{
WINDOWPLACEMENT wp;
wp.length = sizeof wp;
if (GetWindowPlacement(&wp))
WriteWindowPlacement(&wp);
CFrameWnd::OnClose();
}
void CMainFrame::WriteWindowPlacement(WINDOWPLACEMENT *pWP)
{
char szFormat[] = "%u,%u,%d,%d,%d,%d,%d,%d,%d,%d";
char szBuffer[sizeof("-32767")*8 + sizeof("65535")*2 + sizeof(" ")];
sprintf_s(szBuffer, sizeof(szBuffer), szFormat,
pWP->flags, pWP->showCmd,
pWP->ptMinPosition.x, pWP->ptMinPosition.y,
pWP->ptMaxPosition.x, pWP->ptMaxPosition.y,
pWP->rcNormalPosition.left, pWP->rcNormalPosition.top,
pWP->rcNormalPosition.right, pWP->rcNormalPosition.bottom);
CRegKey regKey;
long lRes;
CString szRegKey;
szRegKey.Format("%s\\%s", "SOFTWARE\\AndyAinscow", "Sudoku");
lRes = regKey.Create(HKEY_CURRENT_USER, szRegKey);
if(lRes == ERROR_SUCCESS)
{
lRes = regKey.SetStringValue("MainWindow", szBuffer);
if(lRes != ERROR_SUCCESS)
{
ASSERT(FALSE);
}
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)