Bài giảng Lập trình Windows - Chương 4: GDI+

Tài liệu Bài giảng Lập trình Windows - Chương 4: GDI+: Lập trình Windows Chương 4. GDI+1Nội dungGDI+Các cấu trúc lưu trữ cơ bảnLớp Graphics Sự kiện PaintLàm việc với Pen và Brush Làm việc với Color, Font và Text Làm việc với Image GDI+GDI+ cung cấp tập các lớp để hiện thực các chức năng đồ họa trong Windows FormsGDI+ nằm trong System.Drawing.dllWhat is GDI+?Tổng quan các lớpNamespaceusing System.Drawingusing System.Drawing.Drawing2D Các lớp cơ bảnPoint/PointsRectangle/RectangleFSize/SizeFColorPen/Pens/SystemPensBrush/Brushes/SystemBrushesFont/FontFamilyBitmap/Image/IconGraphicsCác cấu trúc lưu trữ cơ bảnCấu trúc PointProperties X: intY: int IsEmpty: boolMethodsvoid Offset(int dx, int dy)PointPoint p=new Point(2,5);Cấu trúc PointFProperties X: floatY: floatIsEmpty: boolPointFPointF p=new PointF(2,5);Cấu trúc SizePropertiesWidth: intHeight: intIsEmpty: boolSizeSize s=new Size(2,5);Cấu trúc SizeFPropertiesWidth: floatHeight: floatIsEmpty: boolMethods:Size ToSize()PointF ToPointF()SizeFSize s=new Size(2,5);Cấu trúc Rectangle/ RectangleFProper...

pptx35 trang | Chia sẻ: putihuynh11 | Lượt xem: 437 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng Lập trình Windows - Chương 4: GDI+, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Lập trình Windows Chương 4. GDI+1Nội dungGDI+Các cấu trúc lưu trữ cơ bảnLớp Graphics Sự kiện PaintLàm việc với Pen và Brush Làm việc với Color, Font và Text Làm việc với Image GDI+GDI+ cung cấp tập các lớp để hiện thực các chức năng đồ họa trong Windows FormsGDI+ nằm trong System.Drawing.dllWhat is GDI+?Tổng quan các lớpNamespaceusing System.Drawingusing System.Drawing.Drawing2D Các lớp cơ bảnPoint/PointsRectangle/RectangleFSize/SizeFColorPen/Pens/SystemPensBrush/Brushes/SystemBrushesFont/FontFamilyBitmap/Image/IconGraphicsCác cấu trúc lưu trữ cơ bảnCấu trúc PointProperties X: intY: int IsEmpty: boolMethodsvoid Offset(int dx, int dy)PointPoint p=new Point(2,5);Cấu trúc PointFProperties X: floatY: floatIsEmpty: boolPointFPointF p=new PointF(2,5);Cấu trúc SizePropertiesWidth: intHeight: intIsEmpty: boolSizeSize s=new Size(2,5);Cấu trúc SizeFPropertiesWidth: floatHeight: floatIsEmpty: boolMethods:Size ToSize()PointF ToPointF()SizeFSize s=new Size(2,5);Cấu trúc Rectangle/ RectangleFProperties Width: int/floatHeight : int/floatLeft, Top, Right, Bottom: int/floatX, Y: int/floatIsEmpty: boolMethodsbool Contains(Point(F) p)bool Contains(Rectangle(F) r)RectangleFRectangle rect2 = new Rectangle(20, 30, 30, 10); Cấu trúc ColorĐược dùng để tạo màu cho các graphics trong GDI+. 141 màu: Color.AliceBlue Color.AntiqueWhite Color.Yellow Color.YellowGreen Pha màu: Color Color.FromArgb(int r, int g, int b)Color Color.FromArgb(int a, int r, int g, int b)Lớp GraphicsLớp GraphicsLớp Graphics: thể hiện bề mặt vẽ GDI+ (control, bitmap)Lấy đối tượng graphicsCách 1: Override phương thức OnPaint()Cách 2: Bắt sự kiện PaintCách 3: Dùng phương thức ctr.CreateGraphic()Cách 4: Dùng phương thức tĩnh của Graphics: FromImage, FromHwnd, và FromHdc để vẽ trên bitmap, window handle và window handles của device context Lớp Graphicsprotected override void OnPaint(PaintEventArgs e){ Graphics g=e.Graphics; base.OnPaint(e);}private void mainForm_Paint(object sender, PaintEventArgs e){ Graphics g=e.Graphics; }Lớp Graphicsprivate void PaintMe(Control testcontrol){ Graphics g=testcontrol.CreateGraphics(); . . . g.Dispose();}protected override void OnPaint(PaintEventArgs e){ Bitmap bmp=new Bitmap("Water Lilies.jpg"); Graphics g = Graphics.FromImage(bmp); ...}Lớp GraphicsLưu và phục hồi trạng thái đối tượng graphicsGraphics g = GraphicsState state = g.Save(); // Thao tácg.Restore(state); Lớp GraphicsPhương thức vẽ của lớp GraphicsDrawArcDrawIconDrawPathDrawBezierDrawIconUnstretchedDrawPieDrawBeziersDrawImageDrawPolygonDrawClosedCurveDrawImageUnscaledDrawRectangleDrawCurveDrawLineDrawRectanglesDrawEllipseDrawLinesDrawStringPhương thức tô của lớp GraphicsFillClosedCurveFillPieFillRectanglesFillEllipseFillPolygonFillRegionFillPathFillRectangle Pen và BrushLớp Pen/PensĐược dùng để chỉ màu, width, styles của nét vẽKhông cho thừa kếPen ourpen=new Pen(Color.Blue,5);Pens cung cấp 141 pen Pens.AliceBlue Pens.AntiqueWhite Pens.Yellow Pens.YellowGreen Lớp Brush/BrushesĐược dùng để tô màu hình. Lớp Abstract nên không thể tạo đối tượngLớp dẫn xuấtHatchBrushLinearGradientBrushPathGradientBrushSolidBrushTextureBrushSolidBrush myBrush = new SolidBrush(Color.Blue);Lớp Brush/BrushesBrushes cung cấp 141 brush Brushes.AliceBlue Brushes.AntiqueWhite Brushes.Yellow Brushes.YellowGreen SolidBrush: màuSolidBrush brush = new SolidBrush(Color);TextureBrush: ảnhTextureBrush b = new TextureBrush(bmp);WrapMode: Clamp: Vẽ 1 lần Tile: default TileFlipX/TileFlipY:Lật 1 hướng X/Y TileFlipXY: Lật 2 hướngLớp Brush/BrushesHatchBrushHatchStyle: 56 kiểuMàu ForeColorMàu BackColorHatchBrush brush = new HatchBrush(HatchStyle.Cross, Color.LawnGreen);Lớp Brush/BrushesGradient BrushMàu bắt đầuMàu kết thúcHướng gradientVùng gradient (option)Lớp FontTên Font, kích thước, kiểu dáng (đậm, nghiêng, )Tạo Fontf = new Font(Font fName, FontStyle style);f = new Font(string fontName, float size);f = new Font(string fontName, float size, FontStyle style);Một số phương thức vẽ cơ bảnPhương thức DrawLine()Phương thức DrawLine() của lớp Graphics được dùng để vẽ một đường thẳng trên màn hìnhpublic void DrawLine(Pen, Point, Point);public void DrawLine(Pen, PointF, PointF);public void DrawLine(Pen, int, int, int, int);public void DrawLine(Pen, float, float, float, float);Danh sách Overloaded:Phương thức DrawString()Hiện text lên màn hình không cần phải thông qua controlpublic void DrawString(string, Font, Brush, PointF);public void DrawString(string, Font, Brush, RectangleF);public void DrawString(string, Font, Brush, PointF, StringFormat);public void DrawString(string, Font, Brush, RectangleF, StringFormat);public void DrawString(string, Font, Brush, float, float);public void DrawString(string, Font, Brush, float, float, StringFormat);a,b,cDanh sách Overloaded:StringFormat sf = new StringFormat(); sf.FormatFlags = StringFormatFlags. DirectionVertical; sf.Alignment = StringAlignment.Center;Phương thức DrawImage()Được dùng để vẽ các image dùng bất kỳ đối tượng image nào (gif, jpg, bmp, )public void DrawImage(Image, Point)public void DrawImage(Image, Point[])public void DrawImage(Image, PointF)public void DrawImage(Image, PointF[])public void DrawImage(Image, Rectangle)Constructors:Vẽ ảnh JPGprotected override void OnPaint(PaintEventArgs p_event){int x_coord,y_coord;Image testimage=Image.FromFile("Water lilies.jpg");Graphics graf=p_event.Graphics;x_coord=10; y_coord=10;graf.DrawImage(testimage, x_coord, y_coord);}InvalidateInvalidate( )Invalidate Vùng vùng của controlInvalidate(Boolean)Nếu Boolean là true thì invaludate các control conInvalidate(Rectangle)Invalidates vùng được chỉ bởi hình chữ nhậtInvalidate(Region)Invalidates region chỉ raInvalidate(Rectangle, Boolean)Invalidates vùng Rectangle, và nếu Boolean là true thì invalidate các control conInvalidate(Region, Boolean)Invalidates vùng region, và nếu Boolean là true thì invalidate các control con Hủy các đối tượngKhi dùng xong các đối tượng thì chúng ta phải hủy các đối tượngCú pháp: obj.Dispose()Chú ý: Hủy các đối tượng nào được tạo bằng từ khóa newDữ liệu EnumChuyển các giá trị enum thành chuỗiString[] s = Enum.GetNames(typeof(KieuEnum));Chuyển chuỗi sang enumKieuEnum value = (KieuEnum)Enum.Parse(typeof(KieuEnum), s);Q&A35

Các file đính kèm theo tài liệu này:

  • pptx_huflit_chuong4_gdi_3761_1985424.pptx
Tài liệu liên quan