📄 tanchors.cpp
字号:
// If the window has not been destroyed and is a child of the given one
if(IsWindow(pAnchor->hWnd) == TRUE && GetParent(pAnchor->hWnd) == hParent)
{
GetWindowRect(pAnchor->hWnd, &winRect);
winRect.left -= ptLT.x;
winRect.top -= ptLT.y;
winRect.right -= ptLT.x;
winRect.bottom -= ptLT.y;
// Apply the left anchor (if any)
x = (int)(clientRect.right * pAnchor->dx) - (pAnchor->width / 2);
if(pAnchor->dwAnchors & akLeft)
x = pAnchor->left;
// Apply the top anchor (if any)
y = (int)(clientRect.bottom * pAnchor->dy) - (pAnchor->height / 2);
if(pAnchor->dwAnchors & akTop)
y = pAnchor->top;
// Apply the right anchor (if any)
cx = pAnchor->width;
if(pAnchor->dwAnchors & akRight)
{
if((pAnchor->dwAnchors & akLeft) == 0)
x = clientRect.right - pAnchor->right - pAnchor->width;
cx = clientRect.right - pAnchor->right - x;
}
// Apply the bottom anchor (if any)
cy = pAnchor->height;
if(pAnchor->dwAnchors & akBottom)
{
if((pAnchor->dwAnchors & akTop) == 0)
y = clientRect.bottom - pAnchor->bottom - pAnchor->height;
cy = clientRect.bottom - pAnchor->bottom - y;
}
// Store the result into the rectangle
NewRect.left = x;
NewRect.top = y;
NewRect.right = cx;
NewRect.bottom = cy;
return TRUE;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Fast-window repaint logic. These function were written because the default
// window repaint login caused ugly flicking effects when resizing window with
// anchored objects.
// This function moves the window origin by dx and dy pixels
void TAnchors::MoveWindowOrg(HDC hDC, int dx, int dy)
{
POINT p;
GetWindowOrgEx(hDC, &p);
SetWindowOrgEx(hDC, p.x - dx, p.y - dy, NULL);
}
// This function retrieves the widths of both borders (X, Y). It was implemented because
// the "GetWindowInfo()" function returns the wrong sizes when WS_EX_CLIENTEDGE and
// WS_EX_STATICEDGE are set.
void TAnchors::GetBorderSize(HWND hWnd, POINT * border)
{
DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
border->x = border->y = 0;
if(dwExStyle & WS_EX_CLIENTEDGE)
{
border->x += 2;
border->y += 2;
}
if(dwExStyle & WS_EX_STATICEDGE)
{
border->x ++;
border->y ++;
}
if(GetWindowLong(hWnd, GWL_STYLE) & WS_BORDER)
{
border->x ++;
border->y ++;
}
}
// This function repaints the window frame. It was not tested for dialogs
// and overlapped windows, but it's normally used only for dialog childs.
// This function was written because the window borders were not
// repainted on some windows (e.g. progress box).
void TAnchors::PaintFrame(HWND hWnd, HDC hDC, RECT * edgeRect)
{
DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
// Draw the client edge, if any
if(dwExStyle & WS_EX_CLIENTEDGE)
DrawEdge(hDC, edgeRect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
// Draw the static edge, if any
if(dwExStyle & WS_EX_STATICEDGE)
DrawEdge(hDC, edgeRect, BDR_SUNKENOUTER, BF_RECT | BF_ADJUST);
// Draw normal border, if any
if(GetWindowLong(hWnd, GWL_STYLE) & WS_BORDER)
DrawEdge(hDC, edgeRect, BDR_OUTER, BF_RECT | BF_ADJUST | BF_MONO);
/*
GetClassName(hWnd, className, _tsizeof(className));
if(!_tcsicmp(className, _T("Edit")))
{
HBRUSH hBrush;
RECT clientRect;
GetClientRect(hWnd, &clientRect);
clientRect.left++; clientRect.top++;
hBrush = CreateSolidBrush(GetSysColor(COLOR_BTNSHADOW));
FrameRect(hDC, &clientRect, hBrush);
DeleteObject(hBrush);
clientRect.left = clientRect.top = 0;
clientRect.right++; clientRect.bottom++;
hBrush = CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT));
FrameRect(hDC, &clientRect, hBrush);
DeleteObject(hBrush);
}
*/
}
// This function repaints all window children (e.g. dialog box controls) into
// a specified device context.
BOOL TAnchors::PaintChildrenTo(HWND hWnd, HDC hDC)
{
POINT border;
POINT pt;
RECT rect;
int nSaveIndex;
// Get left-top window position
pt.x = pt.y = 0;
ClientToScreen(hWnd, &pt);
for(HWND hChild = GetWindow(hWnd, GW_CHILD); hChild != NULL; hChild = GetWindow(hChild, GW_HWNDNEXT))
{
if(IsWindowVisible(hChild) == FALSE)
continue;
// Retrieve the child position and dimension
GetWindowRect(hChild, &rect);
// Save the DC state and prepare it for child drawing
nSaveIndex = SaveDC(hDC);
MoveWindowOrg(hDC, rect.left - pt.x, rect.top - pt.y);
SetRect(&rect, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
IntersectClipRect(hDC, 0, 0, rect.right, rect.bottom);
// Draw the window content. We have to calculate with the border
GetBorderSize(hChild, &border);
MoveWindowOrg(hDC, border.x, border.y);
PaintWindow(hChild, hDC);
// Now draw the frame
MoveWindowOrg(hDC, -border.x, -border.y);
PaintFrame(hChild, hDC, &rect);
RestoreDC(hDC, nSaveIndex);
}
return TRUE;
}
// This function paints a window into a specific context. If the context is NULL,
// the function draws the window into window device context using "double buffering"
// (Whe whole window and its controls are first painted into a memory DC and then copied
// into the window DC).
BOOL TAnchors::PaintWindowTo(HWND hWnd, HDC hDC)
{
// Paint the window
SendMessage(hWnd, WM_ERASEBKGND, (WPARAM)hDC, 0);
SendMessage(hWnd, WM_PAINT, (WPARAM)hDC, 0);
return PaintChildrenTo(hWnd, hDC);
}
BOOL TAnchors::PaintWindow(HWND hWnd, HDC hDC)
{
HBITMAP hBmp; // Compatible bitmap
RECT clientRect; // Parent's client rectangle
HDC hMemDC; // Memory DC
// If already has a paint DC, draw it.
if(hDC != NULL)
return PaintWindowTo(hWnd, hDC);
// Create a compatible bitmap with the size of the client rect
GetClientRect(hWnd, &clientRect);
hDC = GetDC(NULL); // Device context of the entire screen
hBmp = CreateCompatibleBitmap(hDC, clientRect.right, clientRect.bottom);
ReleaseDC(NULL, hDC);
if(hBmp == NULL)
return FALSE;
// Create the compatible DC (Initially with the size of 1x1 pixel) and select the bitmap.
hMemDC = CreateCompatibleDC(NULL);
hBmp = (HBITMAP)SelectObject(hMemDC, hBmp);
// Start painting
hDC = GetDC(hWnd);
IntersectClipRect(hMemDC, 0, 0, clientRect.right, clientRect.bottom);
PaintWindow(hWnd, hMemDC);
// Copy the memory DC into the window DC
BitBlt(hDC, 0, 0, clientRect.right, clientRect.bottom, hMemDC, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hDC);
// Cleanup
hBmp = (HBITMAP)SelectObject(hMemDC, hBmp);
DeleteDC(hMemDC);
DeleteObject(hBmp);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -