📄 grid.c
字号:
rcClient.right+2, GRID_HDR_TOP + pGridData->nHeadHeight, PIXEL_lightgray, up);#endif } //row header while (pRow) { if((pRow->y + pRow->nHeight - pGridData->nOriginalY > pGridData->nHeadHeight) || (pRow->y + pRow->nHeight - pGridData->nOriginalY < GetGridHeight(pGridData))) { if(pRow == pGridData->pRowSelected) up = FALSE; #ifdef _FLAT_WINDOW_STYLE DrawFlatControlFrameEx (hdc, GRID_HDR_LEFT, pRow->y - pGridData->nOriginalY+1, GRID_HDR_LEFT + pGridData->nHeadWidth, pRow->y - pGridData->nOriginalY + pRow->nHeight -1 , PIXEL_lightgray, 0, up);#else Draw3DControlFrame (hdc, GRID_HDR_LEFT, pRow->y - pGridData->nOriginalY + 1, GRID_HDR_LEFT +pGridData->nHeadWidth, pRow->y - pGridData->nOriginalY + pRow->nHeight - 1, PIXEL_lightgray, up);#endif up = TRUE; if (pRow->flags & GRIDHF_CENTERALIGN) format = DT_SINGLELINE | DT_CENTER | DT_VCENTER; else if(pRow->flags & GRIDHF_RIGHTALIGN) format = DT_SINGLELINE | DT_RIGHT | DT_VCENTER; else format = DT_SINGLELINE | DT_LEFT | DT_VCENTER; GridDrawText (hdc, GRID_HDR_LEFT + 2, pRow->y - pGridData->nOriginalY + 2, pGridData->nHeadWidth - 4, pRow->nHeight - 4, pRow->pTitle, format); //draw the cell line if(pRow->y + pRow->nHeight - pGridData->nOriginalY > pGridData->nHeadHeight) { MoveTo(hdc, pGridData->nHeadWidth, pRow->y + pRow->nHeight - pGridData->nOriginalY-1); LineTo(hdc, GetGridWidth(pGridData), pRow->y + pRow->nHeight - pGridData->nOriginalY-1); } } pRow = pRow->pNext; } //draws the down most unused header if ( !GRID_V_OUTWND(pGridData, rcClient) ) {#ifdef _FLAT_WINDOW_STYLE DrawFlatControlFrameEx (hdc, GRID_HDR_LEFT, GetGridHeight (pGridData)-1, GRID_HDR_LEFT + pGridData->nHeadWidth, rcClient.bottom, PIXEL_lightgray, 0, up);#else Draw3DControlFrame (hdc, GRID_HDR_LEFT, GetGridHeight (pGridData) -1, GRID_HDR_LEFT + pGridData->nHeadWidth, rcClient.bottom, PIXEL_lightgray, up);#endif } //draw the empty header SetBkColor (hdc, PIXEL_lightgray); SetBrushColor (hdc, PIXEL_lightgray); FillBox (hdc, rcClient.left, rcClient.top, pGridData->nHeadWidth+1, pGridData->nHeadHeight+1);}static void GridDrawCells(HWND hwnd, HDC hdc){ PGRIDDATA pGridData; PGRIDCOLHDR pCol; PGRIDROWHDR pRow; PCELLDATA pCell; //RECT rcClient; int format; pGridData = (PGRIDDATA) GetWindowAdditionalData2 (hwnd); pRow = pGridData->pGridRowHdr; pCol = pGridData->pGridColHdr; if((pGridData->flags & GRID_COL_PRIORTY) == GRID_COL_PRIORTY) {//column priorty while(pCol) { if((pCol->x +pCol->nWidth - pGridData->nOriginalX > pGridData->nHeadWidth) || (pCol->x +pCol->nWidth - pGridData->nOriginalX < GetGridWidth(pGridData))) { pRow = pGridData->pGridRowHdr; pCell = pCol->pHeadCell; if (pCol->flags & GRIDHF_CENTERALIGN) format = DT_SINGLELINE | DT_CENTER | DT_VCENTER; else if (pCol->flags & GRIDHF_RIGHTALIGN) format = DT_SINGLELINE | DT_RIGHT | DT_VCENTER; else format = DT_SINGLELINE | DT_LEFT | DT_VCENTER; if (pCol != pGridData->pColSelected) { SetBrushColor (hdc, GetWindowBkColor(pGridData->hWnd)); SetBkColor (hdc, GetWindowBkColor(pGridData->hWnd)); SetTextColor (hdc, pCell->nTextColor); } else { SetBkColor (hdc, pGridData->bkc_selected); SetBrushColor (hdc, pGridData->bkc_selected); SetTextColor (hdc, PIXEL_lightwhite); } while(pRow) { if((pRow->y + pRow->nHeight - pGridData->nOriginalY > pGridData->nHeadHeight) || (pRow->y + pRow->nHeight - pGridData->nOriginalY < GetGridHeight(pGridData))) { //check pcell flags here if(pCell != pGridData->pCellEdit) { if(pCell == pGridData->pCellFocus) { MoveTo(hdc, pCol->x - pGridData->nOriginalX, pRow->y - pGridData->nOriginalY); LineTo(hdc, pCol->x - pGridData->nOriginalX + pCol->nWidth -2, pRow->y - pGridData->nOriginalY); LineTo(hdc, pCol->x - pGridData->nOriginalX + pCol->nWidth -2, pRow->y - pGridData->nOriginalY + pRow->nHeight - 2); LineTo(hdc, pCol->x - pGridData->nOriginalX, pRow->y - pGridData->nOriginalY + pRow->nHeight - 2); LineTo(hdc, pCol->x - pGridData->nOriginalX, pRow->y - pGridData->nOriginalY); } else FillBox (hdc, pCol->x - pGridData->nOriginalX+1, pRow->y - pGridData->nOriginalY+1, pCol->nWidth - 2, pRow->nHeight - 2); if(pCell->pszInfo) GridDrawText (hdc, pCol->x - pGridData->nOriginalX+2, pRow->y - pGridData->nOriginalY + 2, pCol->nWidth - 4, pRow->nHeight - 4, pCell->pszInfo, format); } else { MoveWindow(pGridData->hwnd_edit, pCol->x - pGridData->nOriginalX + 2, pRow->y - pGridData->nOriginalY + 2, pCol->nWidth - 4, pRow->nHeight -4, TRUE); } } pRow = pRow->pNext; pCell = pCell->pNext; } } pCol = pCol->pNext; } } else { //row priorty while(pRow) { if((pRow->y + pRow->nHeight - pGridData->nOriginalY > pGridData->nHeadHeight) || (pRow->y + pRow->nHeight - pGridData->nOriginalY < GetGridHeight(pGridData))) { pCol = pGridData->pGridColHdr; pCell = pRow->pHeadCell; if (pRow->flags & GRIDHF_CENTERALIGN) format = DT_SINGLELINE | DT_CENTER | DT_VCENTER; else if (pCol->flags & GRIDHF_RIGHTALIGN) format = DT_SINGLELINE | DT_RIGHT | DT_VCENTER; else format = DT_SINGLELINE | DT_LEFT | DT_VCENTER; if (pRow != pGridData->pRowSelected) { SetBrushColor (hdc, GetWindowBkColor(pGridData->hWnd)); SetBkColor (hdc, GetWindowBkColor(pGridData->hWnd)); SetTextColor (hdc, pCell->nTextColor); } else { SetBkColor (hdc, pGridData->bkc_selected); SetBrushColor (hdc, pGridData->bkc_selected); SetTextColor (hdc, PIXEL_lightwhite); } while(pCol) { if((pCol->x +pCol->nWidth - pGridData->nOriginalX > pGridData->nHeadWidth) || (pCol->x +pCol->nWidth - pGridData->nOriginalX < GetGridWidth(pGridData))) { //check pcell flags here if(pCell != pGridData->pCellEdit) { if(pCell == pGridData->pCellFocus) { MoveTo(hdc, pCol->x - pGridData->nOriginalX, pRow->y - pGridData->nOriginalY); LineTo(hdc, pCol->x - pGridData->nOriginalX + pCol->nWidth -2, pRow->y - pGridData->nOriginalY); LineTo(hdc, pCol->x - pGridData->nOriginalX + pCol->nWidth -2, pRow->y - pGridData->nOriginalY + pRow->nHeight - 2); LineTo(hdc, pCol->x - pGridData->nOriginalX, pRow->y - pGridData->nOriginalY + pRow->nHeight - 2); LineTo(hdc, pCol->x - pGridData->nOriginalX, pRow->y - pGridData->nOriginalY); } else FillBox (hdc, pCol->x - pGridData->nOriginalX+1, pRow->y - pGridData->nOriginalY+1, pCol->nWidth - 2, pRow->nHeight - 2); if(pCell->pszInfo) GridDrawText (hdc, pCol->x - pGridData->nOriginalX+2, pRow->y - pGridData->nOriginalY + 2, pCol->nWidth - 4, pRow->nHeight - 4, pCell->pszInfo, format); } else { MoveWindow(pGridData->hwnd_edit, pCol->x - pGridData->nOriginalX + 2, pRow->y - pGridData->nOriginalY + 2, pCol->nWidth - 4, pRow->nHeight -4, TRUE); } } pCol = pCol->pNext; pCell = pCell->pRight; } } pRow = pRow->pNext; } }}static void GridOnDraw (HWND hwnd, HDC hdc){ RECT rcClient; PGRIDDATA pGridData; pGridData = (PGRIDDATA) GetWindowAdditionalData2 (hwnd); GetClientRect (hwnd, &rcClient); SetBkColor (hdc, PIXEL_lightwhite); SetBrushColor (hdc, PIXEL_lightwhite); FillBox (hdc, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top); if(pGridData->pRowSelected) pGridData->flags &= ~GRID_COL_PRIORTY; else pGridData->flags |= GRID_COL_PRIORTY; // draws cells GridDrawCells(hwnd, hdc); // draws header GridDrawHeader(hwnd, hdc);}/*************************** scroll info ***********************************/static int GridSetVScrollInfo (PGRIDDATA pGridData){ SCROLLINFO si; RECT rect; GetClientRect (pGridData->hWnd, &rect); if ((rect.bottom - rect.top) > GetGridHeight(pGridData)) { ShowScrollBar (pGridData->hWnd, SB_VERT, FALSE); pGridData->nOriginalY = 0; return 0; } if (pGridData->nOriginalY < 0) pGridData->nOriginalY = 0; si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS; si.nMax = GetGridHeight(pGridData) - pGridData->nHeadHeight; si.nMin = 0; si.nPage = rect.bottom - rect.top - pGridData->nHeadHeight; si.nPos = pGridData->nOriginalY; if (si.nPos > si.nMax - si.nPage) { si.nPos = si.nMax - si.nPage; pGridData->nOriginalY = si.nPos; } SetScrollInfo (pGridData->hWnd, SB_VERT, &si, TRUE); ShowScrollBar (pGridData->hWnd, SB_VERT, TRUE); return 0;}static int GridSetHScrollInfo (PGRIDDATA pGridData){ SCROLLINFO si; RECT rect; GetClientRect (pGridData->hWnd, &rect); if ( !GRID_H_OUTWND(pGridData, rect) ) { ShowScrollBar (pGridData->hWnd, SB_HORZ, FALSE); return 0; } si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS; si.nMax = GetGridWidth (pGridData) - pGridData->nHeadWidth; si.nMin = 0; si.nPage = rect.right - rect.left - pGridData->nHeadWidth; si.nPos = pGridData->nOriginalX; if (si.nPos > si.nMax - si.nPage) { si.nPos = si.nMax - si.nPage; pGridData->nOriginalX = si.nPos; } SetScrollInfo (pGridData->hWnd, SB_HORZ, &si, TRUE); ShowScrollBar (pGridData->hWnd, SB_HORZ, TRUE); return 0;}static void GridVScroll (HWND hwnd, WPARAM wParam, LPARAM lParam){ int scrollHeight = 0; RECT rect; int scrollBoundMax; int scrollBoundMin; int vscroll = 0; PGRIDDATA pGridData; pGridData = (PGRIDDATA) GetWindowAdditionalData2 (hwnd); GetClientRect (hwnd, &rect); scrollBoundMax = GetGridHeight(pGridData) - (rect.bottom - rect.top); scrollBoundMin = 0; //decides the desired value to scroll if (wParam == SB_LINEUP || wParam == SB_LINEDOWN) vscroll = VSCROLL; else if (wParam == SB_PAGEUP || wParam == SB_PAGEDOWN) vscroll = rect.bottom - rect.top - pGridData->nHeadHeight; //scroll down if ( (wParam == SB_LINEDOWN || wParam == SB_PAGEDOWN) && pGridData->nOriginalY < scrollBoundMax ) { if ((pGridData->nOriginalY + vscroll) > scrollBoundMax) { scrollHeight = pGridData->nOriginalY - scrollBoundMax; pGridData->nOriginalY = scrollBoundMax; } else { pGridData->nOriginalY += vscroll; scrollHeight = -vscroll; } } //scroll up else if ( (wParam == SB_LINEUP || wParam == SB_PAGEUP) && pGridData->nOriginalY > scrollBoundMin ) { if ((pGridData->nOriginalY - vscroll) > scrollBoundMin) { pGridData->nOriginalY -= vscroll; scrollHeight = vscroll; } else { scrollHeight = pGridData->nOriginalY - scrollBoundMin; pGridData->nOriginalY = scrollBoundMin; } } //dragging else if ( wParam == SB_THUMBTRACK ) { int scrollNewPos = (int) lParam; if (((scrollNewPos - pGridData->nOriginalY) < 5) && ((scrollNewPos - pGridData->nOriginalY) > -5) && (scrollNewPos > 5) && ((scrollBoundMax - scrollNewPos) > 5)) return; if ((scrollNewPos < pGridData->nOriginalY) && (scrollNewPos <= VSCROLL)) { scrollHeight = pGridData->nOriginalY - 0; pGridData->nOriginalY = 0; } else { if ((scrollNewPos > pGridData->nOriginalY) && ((scrollBoundMax - scrollNewPos) < VSCROLL)) { scrollHeight = pGridData->nOriginalY - scrollBoundMax; pGridData->nOriginalY = scrollBoundMax; } else { scrollHeight = pGridData->nOriginalY - scrollNewPos; pGridData->nOriginalY = scrollNewPos; } } } if (scrollHeight != 0) { InvalidateRect (hwnd, NULL, FALSE); GridSetVScrollInfo (pGridData); }}static void GridHScroll (HWND hwnd, WPARAM wParam, LPARAM lParam){ int scrollWidth = 0; int scrollBoundMax; int scrollBoundMin; RECT rect; int hscroll = 0; PGRIDDATA pGridData; pGridData = (PGRIDDATA) GetWindowAdditionalData2 (hwnd); GetClientRect (hwnd, &rect); scrollBoundMax = GetGridWidth (pGridData) - (rect.right - rect.left); scrollBoundMin = 0; //decides the desired value to scroll if (wParam == SB_LINERIGHT || wParam == SB_LINELEFT) hscroll = HSCROLL; else if (wParam == SB_PAGERIGHT || wParam == SB_PAGELEFT) hscroll = rect.right - rect.left - pGridData->nHeadWidth; //scroll right if ( (wParam == SB_LINERIGHT || wParam == SB_PAGERIGHT) && pGridData->nOriginalX < scrollBoundMax ) { if ((pGridData->nOriginalX + hscroll) > scrollBoundMax) { scrollWidth = pGridData->nOriginalX - scrollBoundMax; pGridData->nOriginalX = scrollBoundMax; } else { pGridData->nOriginalX += hscroll; scrollWidth = -hscroll; } } //scroll left else if ( (wParam == SB_LINELEFT || wParam == SB_PAGELEFT) && pGridData->nOriginalX > scrollBoundMin) { if ((pGridData->nOriginalX - hscroll) > scrollBoundMin) { pGridData->nOriginalX -= hscroll; scrollWidth = hscroll; } else { scrollWidth = pGridData->nOriginalX - scrollBoundMin; pGridData->nOriginalX = scrollBoundMin; } } //draging else if (wParam == SB_THUMBTRACK) { int scrollNewPos = (int) lParam; if (((scrollNewPos - pGridData->nOriginalX) < HSCROLL) && ((scrollNewPos - pGridData->nOriginalX) > -HSCROLL) && (scrollNewPos > HSCROLL) && ((scrollBoundMax - scrollNewPos) > HSCROLL)) return; if ((scrollNewPos < pGridData->nOriginalX) && (scrollNewPos <= HSCROLL)) { scrollWidth = pGridData->nOriginalX - 0; pGridData->nOriginalX = 0; } else { if ((scrollNewPos > pGridData->nOriginalX) && ((scrollBoundMax - scrollNewPos) < HSCROLL)) { scrollWidth = pGridData->nOriginalX - scrollBoundMax; pGridData->nOriginalX = scrollBoundMax; } else { scrollWidth = pGridData->nOriginalX - scrollNewPos; pGridData->nOriginalX = scrollNewPos; } } } if (scrollWidth != 0) { InvalidateRect (hwnd, NULL, FALSE); GridSetHScrollInfo (pGridData); }}static int GridMakeCellVisible (HWND hwnd, PGRIDDATA pGridData, PCELLDATA pCell){ //int scrollHeight = 0, scrollWidth = 0; RECT rect; int area_height, area_width; int temp = 0; PGRIDCOLHDR pCol = pCell->pColHdr; PGRIDROWHDR pRow = pCell->pRowHdr; GetClientRect (hwnd, &rect); area_height = rect.bottom - rect.top;// - pGridData->nHeadHeight; area_width = rect.right - rect.left;// - pGridData->nHeadWidth; if(pRow->y < pGridData->nOriginalY + pGridData->nHeadHeight) { pGridData->nOriginalY = pRow->y-pGridData->nHeadHeight; temp = 1; } else if((pRow->y + pRow->nHeight - pGridData->nOriginalY) > area_height) { pGridData->nOriginalY = pRow->y + pRow->nHeight - area_height; temp = 1; } if(temp) GridSetVScrollInfo(pGridData); if(pCol->x < pGridData->nOriginalX + pGridData->nHeadWidth) { pGridData->nOriginalX = pCol->x-pGridData->nHeadWidth; temp = 2; } else if((pCol->x + pCol->nWidth - pGridData->nOriginalX) > area_width) { pGridData->nOriginalX = pCol->x + pCol->nWidth - area_width; temp = 2; } if(temp == 2) GridSetHScrollInfo(pGridData); if(temp > 0) { InvalidateRect(hwnd, NULL, FALSE); return 1; } return -1;}static int sGridProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam){ PGRIDDATA pgriddata = NULL; DWORD dwStyle; int mouseX, mouseY; int nCols, nRows; if(message != MSG_CREATE) pgriddata = (PGRIDDATA)GetWindowAdditionalData2(hwnd); switch(message) { case MSG_CREATE: dwStyle = GetWindowStyle(hwnd); if((pgriddata = NewGrid(hwnd, 1, 1)) == NULL) return -1; SetWindowAdditionalData2(hwnd, (DWORD)pgriddata); GridSetHScrollInfo(pgriddata);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -