1.SetWindowPos 设置窗口位置,尺寸,Z-order
/*
Changes the size, position, and Z-order of a pane.
virtual HDWP SetWindowPos(
const CWnd* pWndInsertAfter,
int x,
int y,
int cx,
int cy,
UINT nFlags,
HDWP hdwp = NULL
);
*/
//SetWindowPos(NULL, 200, 200, rect.Width(), rect.Height(), SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
2.CalcWindowRect 调用此成员函数,根据所需的客户端矩形大小计算窗口矩形的所需大小。
给定一个包含窗口客户区坐标的CRect对象的指针,CalcWindowRect 会计算出相应的窗口矩形,矩形的宽度和高度会传递给CWnd::SetWindowpos,进而得到合适的窗口大小
//Call this member function to compute the required size of the window rectangle based on the desired client-rectangle size.
virtual void CalcWindowRect(
LPRECT lpClientRect,
UINT nAdjustType = adjustBorder
);
CMainWnd::CMainWnd()
{
::ZeroMemory(m_nGameGrid, 9 * sizeof(int));
m_NextChar = -1;
CString wndclass=AfxRegisterWndClass(CS_DBLCLKS, AfxGetApp()->LoadStandardCursor(IDC_ARROW), (HBRUSH)(COLOR_3DFACE+1), AfxGetApp()->LoadStandardIcon(IDI_WINLOGO));
CreateEx(0,wndclass, _T("TicTac App"), WS_CAPTION | WS_SYSMENU | WS_BORDER | WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL);
CRect rect(0, 0,768, 768);
CalcWindowRect(&rect);
//SetWindowPos(NULL, 200, 200, rect.Width(), rect.Height(), SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
MoveWindow(200, 200, rect.Width(), rect.Height());
}
3.PtInRect 函数确定指定的点是否位于指定的矩形内。如果一个点位于矩形的左侧或顶部,或者在所有四条边内,那么它就在矩形内。右边或底边的一个点被认为是在矩形之外。
//The PtInRect function determines whether the specified point lies within the specified rectangle. A point is within a rectangle if it lies on the left or top side or is within all four sides. A point on the right or bottom side is considered outside the rectangle.
BOOL PtInRect( CONST RECT *lprc, // rectangle
POINT pt // point
);
/
int CMainWnd::GetRectID(CPoint point)
{
for (int i=0;i<9;i++)
{
if (m_rcSquare[i].PtInRect(point))
return i;
}
return -1;
}
4.DeflateRect 使切边向中心移动,使切边偏转
/*
DeflateRect deflates CRect by moving its sides toward its center.
void DeflateRect(
int x,
int y
) throw( );
void DeflateRect(
SIZE size
) throw( );
void DeflateRect(
LPCRECT lpRect
) throw( );
void DeflateRect(
int l,
int t,
int r,
int b
) throw( );
*/
void CMainWnd::DrawX(CDC* pdc, int nPos)
{
//利用数组判断在第几个矩形内
CPen pen(PS_SOLID, 16, RGB(255, 0, 0));
CPen* OldPen = pdc->SelectObject(&pen);
CRect rect = m_rcSquare[nPos];
rect.DeflateRect(16, 16);
pdc->MoveTo(rect.left, rect.top);
pdc->LineTo(rect.right, rect.bottom);
pdc->MoveTo(rect.right, rect.top);
pdc->LineTo(rect.left, rect.bottom);
pdc->SelectObject(OldPen);
}
5.GetPixel 获得光标下的像素颜色.检索像素在x和y指定点的RGB颜色值。
- 一般所有windows PC都支持纯黑RGB(0,0,0)和纯白RGB(255,255,255).这些颜色不会抖动
/*
Retrieves the RGB color value of the pixel at the point specified by x and y.
COLORREF GetPixel(
int x,
int y
) const;
COLORREF GetPixel(
POINT point
) const
*/
void CMainWnd::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CClientDC dc(this);
if (dc.GetPixel(point) == RGB(0, 0, 0))
RestGame();
}
创建基于CWnd的派生类窗口对象
- 在BEGIN_MESSAGE_MAP()宏中确保CWnd 作为基类,而不是CFrameWnd.
BEGIN_MESSAGE_MAP(CMainWnd,CWnd)
...
END_MESSAGE_MAP()
- 在窗口构造函数中使用 AfxRegisterWndClass 注册WNDCLASS
/*
Allows you to register your own window classes.
LPCTSTR AFXAPI AfxRegisterWndClass(
UINT nClassStyle,
HCURSOR hCursor = 0,
HBRUSH hbrBackground = 0,
HICON hIcon = 0
);
Class Styles
The class styles define additional elements of the window class. Two or more styles can be combined by using the bitwise OR (|) operator. To assign a style to a window class, assign the style to the style member of the WNDCLASSEX structure. The class styles are as follows.
CString wndclass=AfxRegisterWndClass(CS_DBLCLKS, AfxGetApp()->LoadStandardCursor(IDC_ARROW),
(HBRUSH)(COLOR_3DFACE+1), AfxGetApp()->LoadStandardIcon(IDI_WINLOGO));
The pointer returned by this function can be used to access application information such as the main message-dispatch code or the topmost window.
CWinApp* AFXAPI AfxGetApp( );
3. 创建窗口
/*
Creates a control (a child window) and associates it with the CAnimateCtrl object.
virtual BOOL CreateEx(
DWORD dwExStyle,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID
);
*/
CreateEx(0,wndclass, _T("TicTac App"), WS_CAPTION | WS_SYSMENU | WS_BORDER | WS_OVERLAPPED,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, NULL);
4.删除窗口对象
/*
使用CWnd 自带的 virtual void PostNcDestroy( );
或者Exitinstance()
*/
void CMainWnd::PostNcDestory()
{
delete this;
}
/*
virtual int ExitInstance( );
*/
int CMainWnd::Exitinstance()
{
delete this;
return 0;
}