1. 通过双缓冲解决CJobSlotGrid控件闪烁问题
| | |
| | | #include "stdafx.h" |
| | | #include "JobSlotGrid.h" |
| | | #include "ServoMemDC.h" |
| | | |
| | | #ifdef _DEBUG |
| | | #define new DEBUG_NEW |
| | |
| | | |
| | | void CJobSlotGrid::OnPaint() { |
| | | CPaintDC dc(this); |
| | | DrawGrid(&dc); |
| | | CServoMemDC memDC(&dc); |
| | | DrawGrid(&memDC); |
| | | } |
| | | |
| | | void CJobSlotGrid::DrawGrid(CDC* pDC) |
| | | { |
| | | CRect rect; |
| | | GetClientRect(&rect); |
| | | pDC->FillSolidRect(&rect, ::GetSysColor(COLOR_3DFACE)); // å®å
¨èæ¯è²ï¼ä»
å
åç»å¶ |
| | | |
| | | if (m_nCols == 0 || m_nRows == 0) { |
| | | return; |
| | |
| | | // 鿩填å
é¢è² |
| | | COLORREF fillColor = m_vSlotStatus[i][j] ? m_colorHasJob : m_colorNoJob; |
| | | if (IsSlotClickable(i, j)) { |
| | | if (bIsClicking) |
| | | if (bIsClicking) { |
| | | fillColor = COLOR_CLICK; |
| | | else if (bIsHover) |
| | | } |
| | | else if (bIsHover) { |
| | | fillColor = COLOR_HOVER; |
| | | } |
| | | } |
| | | |
| | | // ç»å¶èæ¯ï¼é«ææ¿ä»£ CBrushï¼ |
| | |
| | | // ç»å¶è¾¹æ¡ |
| | | pDC->DrawEdge(&cellRect, EDGE_SUNKEN, BF_RECT); |
| | | |
| | | // 妿æ¯å¯ç¹å»æ ¼åï¼å¨å·¦ä¸è§ç»ä¸ä¸ªå°åç¹ |
| | | if (IsSlotClickable(i, j)) { |
| | | constexpr int DOT_RADIUS = 3; |
| | | int cx = cellRect.left + 5; |
| | | int cy = cellRect.top + 5; |
| | | |
| | | // ä¿åæ§ç¬åå·å |
| | | CBrush brushDot(RGB(0, 120, 215)); |
| | | CBrush* pOldBrush = pDC->SelectObject(&brushDot); |
| | | CPen penDot(PS_SOLID, 1, RGB(0, 120, 215)); |
| | | CPen* pOldPen = pDC->SelectObject(&penDot); |
| | | |
| | | // ç»å¶åç¹ |
| | | pDC->Ellipse(cx - DOT_RADIUS, cy - DOT_RADIUS, cx + DOT_RADIUS, cy + DOT_RADIUS); |
| | | |
| | | // æ¢å¤ |
| | | pDC->SelectObject(pOldBrush); |
| | | pDC->SelectObject(pOldPen); |
| | | } |
| | | |
| | | // è·åæåï¼å®å
¨ï¼ |
| | | CString strText; |
| | | if (i < m_vSlotText.size() && j < m_vSlotText[i].size()) { |
| | |
| | | <ClInclude Include="ServoCommo.h" /> |
| | | <ClInclude Include="ServoDlg.h" /> |
| | | <ClInclude Include="ServoGraph.h" /> |
| | | <ClInclude Include="ServoMemDC.h" /> |
| | | <ClInclude Include="stdafx.h" /> |
| | | <ClInclude Include="targetver.h" /> |
| | | <ClInclude Include="TerminalDisplayDlg.h" /> |
| | |
| | | <ClCompile Include="Servo.cpp" /> |
| | | <ClCompile Include="ServoDlg.cpp" /> |
| | | <ClCompile Include="ServoGraph.cpp" /> |
| | | <ClCompile Include="ServoMemDC.cpp" /> |
| | | <ClCompile Include="stdafx.cpp"> |
| | | <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> |
| | | <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> |
| | |
| | | <ClCompile Include="CSlot.cpp" /> |
| | | <ClCompile Include="CRobotTaskDlg.cpp" /> |
| | | <ClCompile Include="PageTransferLog.cpp" /> |
| | | <ClCompile Include="ServoMemDC.cpp" /> |
| | | </ItemGroup> |
| | | <ItemGroup> |
| | | <ClInclude Include="AlarmManager.h" /> |
| | |
| | | <ClInclude Include="CSlot.h" /> |
| | | <ClInclude Include="CRobotTaskDlg.h" /> |
| | | <ClInclude Include="PageTransferLog.h" /> |
| | | <ClInclude Include="ServoMemDC.h" /> |
| | | </ItemGroup> |
| | | <ItemGroup> |
| | | <ResourceCompile Include="Servo.rc" /> |
| ¶Ô±ÈÐÂÎļþ |
| | |
| | | #include "stdafx.h" |
| | | #include "ServoMemDC.h" |
| | | |
| | | CServoMemDC::CServoMemDC(CDC* pDC, const CRect* pRect) |
| | | : CDC(), m_pOldBitmap(nullptr), m_pDC(pDC), m_bMemDC(FALSE) |
| | | { |
| | | ASSERT(pDC != nullptr); |
| | | |
| | | if (pRect == nullptr) |
| | | pDC->GetClipBox(&m_rect); |
| | | else |
| | | m_rect = *pRect; |
| | | |
| | | if (CreateCompatibleDC(pDC)) { |
| | | m_bMemDC = TRUE; |
| | | m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height()); |
| | | m_pOldBitmap = SelectObject(&m_bitmap); |
| | | SetWindowOrg(m_rect.left, m_rect.top); |
| | | } |
| | | } |
| | | |
| | | CServoMemDC::~CServoMemDC() |
| | | { |
| | | if (m_bMemDC) { |
| | | // å°å
å DC æ·è´ååå§çªå£ DC |
| | | m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(), |
| | | this, m_rect.left, m_rect.top, SRCCOPY); |
| | | SelectObject(m_pOldBitmap); |
| | | } |
| | | } |
| | | |
| | | CServoMemDC* CServoMemDC::operator->() |
| | | { |
| | | return this; |
| | | } |
| | | |
| | | CServoMemDC::operator CDC* () |
| | | { |
| | | return this; |
| | | } |
| ¶Ô±ÈÐÂÎļþ |
| | |
| | | #pragma once |
| | | #include <afxwin.h> // MFC æ ¸å¿å¤´æä»¶ |
| | | |
| | | // CServoMemDCï¼åç¼å²ç»å¾å°è£
ç±»ï¼æ¶é¤éªçï¼ |
| | | class CServoMemDC : public CDC |
| | | { |
| | | public: |
| | | CServoMemDC(CDC* pDC, const CRect* pRect = nullptr); // æé ï¼ä¼ å
¥ç®æ DC ååºå |
| | | ~CServoMemDC(); // ææï¼èªå¨æ·è´å°ç®æ DC |
| | | |
| | | CServoMemDC* operator->(); |
| | | operator CDC* (); |
| | | |
| | | private: |
| | | CBitmap m_bitmap; // ç¨äºå
å DC çä½å¾ |
| | | CBitmap* m_pOldBitmap; // æ§ä½å¾ |
| | | CDC* m_pDC; // åå§ç»å¾ DCï¼çªå£ï¼ |
| | | CRect m_rect; // ç»å¾åºå |
| | | BOOL m_bMemDC; // æ¯å¦å¯ç¨äºå
å DCï¼CreateCompatibleDC æåï¼ |
| | | }; |