When we use Visual C to develop a database-based software, we often use a large number of tables, while Visual C does not provide the corresponding table control, so we need our programmers to make itself, this article will introduce a simple form control. process. In fact, a form is constructed from a series of cells in a window, so the most important step of making a form control is the design of the cell. From the simplest angle, it is the easiest to make cell classes by retrofitting editing boxes (Edit Box), so the focus of the sample program is to derive a ccell class from the CEDIT class and do appropriate to this class. modify. When writing this unit, it is necessary to pay attention to the user operating cells, and should appropriately change the appearance of the cell to make the user feel more intuitive. We use AppWizard to create a single document program, named CGRIDDEMO. Use ClassWizard to join two new classes: Base class as CCell classes for CEDIT classes and base classes are CGRIDWnd classes for CWND classes. The main code of these two classes is listed below: // The following is the primary code void ccell :: OnsetFocus () // When the user operates cell, change the design of the cell {setfocus = true; // setfocus Bool type variable CDC * PDC = this-> getWindowdc (); this-> oneRaseBkGnd (PDC); // change cell extraraction} BOOL CCell :: OneRaseBkGnd (CDC * PDC) // Change unit Extract, overreloading onrasebkgn Function {RECT RECT; CPEN PEN;
CBRUSH brush;
Logbrush logbrush; if (setfocus == true) // When the user operates a cell, add a black border for cells.
{Pen.createpen (PS_SOLID, 2, RGB (0, 0, 0)); // Set the line width is two pixels logbrush.lbcolor = RGB (0, 0, 0); logbrush.lbstyle = BS_HOLLOW; brush.createbrushindirectirect (&& logbrush); PDC-> SelectObject (&& pen); PDC-> SelectObject (&& brush); this-> getClientRect (&& Rect); PDC-> Rectangle (&& Rect); // is a cellular black border} Else // When users Operate additional cells, eliminate the black border {Pen.createpen (PS_SOLID, 2, RGB (255, 255, 255)); // Create a white PEN to cover the border logbrush.lbcolor = RGB (0, 0, 0); logbrush.lbstyle = BS_HOLLOW; BRUSH.CREATEBRUSHINDIRECT (&& logbrush); PDC-> SelectObject (&& Pen); PDC-> SelectObject (&& brush); this-> getClientRect (&& Rect);
PDC-> Rectangle (&& Rect);} Return Cedit :: OneRaseBkGnd (PDC);} void ccell :: ONKILLFOCUS () // User focus leaves cell, eliminating black border {setfocus = false; cdc * pdc = this-> getWindowdc (); "} //} // below is the main code of the CGridWnd class CGridWnd :: CGridWnd (CWND * PWND, UINT NROW, UINT NCOL) // CGridWnd class constructor {m_pparentWnd = PWnd; // Father Window handle row = nROW; // row number col = ncol; // column number} BOOL CGRIDWND :: Create () // Reserved CREATE function, build a Grid window with the same size as the parent window {Rect; m_pparentWnd-> GetClientRect; Return CWnd :: Create (NULL, NULL, WS_CHILD | WS_VISIBLE, RECT, M_PPARENTWND, NULL, NULL);} BOOL CGRIDWND :: OneRaseBkGnd (CDC * PDC) // This function is based on the line, column Digital Drawn Grid {for (INT I = 0; I