Changeset 193 for branches/highdpi/Packages/DpiControls/UDpiControls.pas
- Timestamp:
- May 7, 2020, 7:05:57 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/highdpi/Packages/DpiControls/UDpiControls.pas
r179 r193 28 28 public 29 29 property OnPaint; 30 procedure Paint; override; 31 end; 32 33 { TControlEx } 34 35 TControlEx = class(TControl) 36 public 30 37 property OnMouseDown; 31 38 property OnMouseUp; 32 39 property OnMouseMove; 33 procedure Paint; override;34 40 end; 35 41 … … 189 195 property Parent: TDpiWinControl read FParent write SetParent; 190 196 property BoundsRect: TRect read GetBoundsRect write SetBoundsRect; 197 property Visible: Boolean read GetVisible write SetVisible; 191 198 published 192 199 property ClientHeight: Integer read GetClientHeight write SetClientHeight; … … 197 204 property Width: Integer read FWidth write SetWidth; 198 205 property Height: Integer read FHeight write SetHeight; 199 property Visible: Boolean read GetVisible write SetVisible;200 206 property Caption: string read GetCaption write SetCaption; 201 207 property Enabled: Boolean read GetEnabled write SetEnabled; … … 221 227 TDpiGraphic = class(TPersistent) 222 228 protected 229 FDpi: Integer; 223 230 function GetVclGraphic: TGraphic; virtual; 231 function GetWidth: Integer; virtual; abstract; 232 function GetHeight: Integer; virtual; abstract; 233 procedure SetWidth(Value: Integer); virtual; abstract; 234 procedure SetHeight(Value: Integer); virtual; abstract; 235 procedure ScreenChanged; virtual; 236 procedure SetDpi(AValue: Integer); virtual; 237 function GetDpi: Integer; virtual; 224 238 public 225 procedure LoadFromFile(const Filename: string); 239 constructor Create; 240 procedure LoadFromFile(const Filename: string); virtual; 241 property Width: Integer read GetWidth write SetWidth; 242 property Height: Integer read GetHeight write SetHeight; 243 property Dpi: Integer read GetDpi write SetDpi; 226 244 end; 227 245 … … 260 278 procedure StretchDraw(const DestRect: TRect; SrcGraphic: TDpiGraphic); virtual; 261 279 procedure FrameRect(Rect: TRect); 262 procedure Rectangle(X1, Y1, X2, Y2: Integer); 280 procedure Rectangle(X1, Y1, X2, Y2: Integer); overload; 281 procedure Rectangle(const ARect: TRect); overload; 263 282 function TextWidth(Text: string): Integer; 264 283 function TextHeight(Text: string): Integer; … … 291 310 VclGraphicControl: TGraphicControl; 292 311 FCanvas: TDpiCanvas; 312 function GetOnPaint: TNotifyEvent; 293 313 procedure SetCanvas(AValue: TDpiCanvas); 294 314 procedure PaintHandler(Sender: TObject); 315 procedure SetOnPaint(AValue: TNotifyEvent); 295 316 protected 296 317 procedure Paint; virtual; … … 298 319 function GetVclGraphicControl: TGraphicControl; virtual; 299 320 procedure UpdateVclControl; override; 300 property OnPaint: TNotifyEvent read FOnPaint write FOnPaint; 321 procedure UpdateVclControlPrivate; virtual; 322 property OnPaint: TNotifyEvent read GetOnPaint write SetOnPaint; 301 323 public 302 324 constructor Create(TheOwner: TComponent); override; … … 481 503 destructor Destroy; override; 482 504 published 505 property Visible; 483 506 end; 484 507 … … 523 546 property ExtendedSelect: Boolean read GetExtendedSelect write SetExtendedSelect default true; 524 547 property BorderStyle: TBorderStyle read GetBorderStyle write SetBorderStyle default bsNone; 548 property Visible; 525 549 end; 526 550 … … 556 580 property Kind: TScrollBarKind read GetKind write SetKind; 557 581 property OnChange: TNotifyEvent read GetOnChange write SetOnChange; 582 property Visible; 558 583 end; 559 584 … … 563 588 private 564 589 FCanvas: TDpiCanvas; 590 FWidth: Integer; 591 FHeight: Integer; 565 592 function GetCanvas: TDpiCanvas; 566 function GetHeight: Integer;567 593 function GetPixelFormat: TPixelFormat; 568 594 function GetScanLine(Row: Integer): Pointer; 569 function GetWidth: Integer;570 procedure SetHeight(AValue: Integer);571 595 procedure SetPixelFormat(AValue: TPixelFormat); 572 procedure SetWidth(AValue: Integer);573 596 protected 597 function GetHeight: Integer; override; 598 function GetWidth: Integer; override; 574 599 function GetVclBitmap: TCustomBitmap; virtual; 575 600 function GetVclRasterImage: TRasterImage; override; 601 procedure SetHeight(AValue: Integer); override; 602 procedure SetWidth(AValue: Integer); override; 603 procedure ScreenChanged; override; 576 604 public 577 605 VclBitmap: TBitmap; 578 606 procedure BeginUpdate; 579 607 procedure EndUpdate; 580 procedure SetSize( Width,Height: Integer);608 procedure SetSize(AWidth, AHeight: Integer); 581 609 constructor Create; 582 610 destructor Destroy; override; … … 617 645 property Stretch: Boolean read FStretch write SetStretch; 618 646 property Picture: TDpiPicture read FDpiPicture write SetPicture; 647 property Visible; 619 648 end; 620 649 … … 623 652 TDpiPaintBox = class(TDpiGraphicControl) 624 653 private 625 function GetOnPaint: TNotifyEvent; 626 procedure SetOnPaint(AValue: TNotifyEvent); 654 procedure UpdateVclControlPrivate; override; 627 655 public 628 656 VclPaintBox: TPaintBox; … … 631 659 destructor Destroy; override; 632 660 published 661 property OnPaint; 662 property Visible; 633 663 end; 634 664 … … 666 696 public 667 697 VclJpeg: TJPEGImage; 698 procedure LoadFromFile(const Filename: string); override; 668 699 end; 669 700 … … 676 707 public 677 708 VclPng: TPortableNetworkGraphic; 709 procedure LoadFromFile(const Filename: string); override; 678 710 end; 679 711 … … 709 741 710 742 procedure Register; 711 function DpiBitBlt(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC; XSrc, YSrc: Integer; Rop: DWORD): Boolean; 743 function DpiBitBlt(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; 744 function DpiBitBltCanvas(Dest: TDpiCanvas; X, Y, Width, Height: Integer; Src: TDpiCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; 712 745 function DpiCreateRectRgn(X1, Y1, X2, Y2: Integer): HRGN; 713 746 function ScaleToVcl(Value: Integer): Integer; … … 735 768 end; 736 769 770 function DpiBitBltCanvas(Dest: TDpiCanvas; X, Y, Width, Height: Integer; 771 Src: TDpiCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; 772 begin 773 Result := DpiBitBlt(Dest.Handle, X, Y, Width, Height, Src.Handle, XSrc, YSrc, Rop); 774 end; 775 737 776 function DpiCreateRectRgn(X1, Y1, X2, Y2: Integer): HRGN; 738 777 begin … … 792 831 793 832 function DpiBitBlt(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC; XSrc, 794 YSrc: Integer; Rop: DWORD ): Boolean;833 YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; 795 834 begin 796 835 Result := BitBlt(DestDC, ScaleToVcl(X), ScaleToVcl(Y), ScaleToVcl(Width), … … 995 1034 end; 996 1035 1036 procedure TDpiJpegImage.LoadFromFile(const Filename: string); 1037 var 1038 Bitmap: TJPEGImage; 1039 begin 1040 Bitmap := TJPEGImage.Create; 1041 Bitmap.LoadFromFile(FileName); 1042 Width := ScaleFromVcl(Bitmap.Width); 1043 Height := ScaleFromVcl(Bitmap.Height); 1044 if Self is TDpiBitmap then 1045 TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap) 1046 else raise Exception.Create('Unsupported class ' + Self.ClassName); 1047 Bitmap.Free; 1048 end; 1049 997 1050 { TDpiPortableNetworkGraphic } 998 1051 … … 1006 1059 if not Assigned(VclPng) then VclPng := TPortableNetworkGraphic.Create; 1007 1060 Result := VclPng; 1061 end; 1062 1063 procedure TDpiPortableNetworkGraphic.LoadFromFile(const Filename: string); 1064 var 1065 Bitmap: TPortableNetworkGraphic; 1066 begin 1067 Bitmap := TPortableNetworkGraphic.Create; 1068 Bitmap.LoadFromFile(FileName); 1069 Width := ScaleFromVcl(Bitmap.Width); 1070 Height := ScaleFromVcl(Bitmap.Height); 1071 if Self is TDpiBitmap then 1072 TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap) 1073 else raise Exception.Create('Unsupported class ' + Self.ClassName); 1074 Bitmap.Free; 1008 1075 end; 1009 1076 … … 1157 1224 end; 1158 1225 1226 procedure TDpiGraphic.ScreenChanged; 1227 begin 1228 end; 1229 1230 procedure TDpiGraphic.SetDpi(AValue: Integer); 1231 begin 1232 FDpi := AValue; 1233 ScreenChanged; 1234 end; 1235 1236 function TDpiGraphic.GetDpi: Integer; 1237 begin 1238 Result := FDpi; 1239 end; 1240 1241 constructor TDpiGraphic.Create; 1242 begin 1243 Dpi := DpiScreen.Dpi; 1244 end; 1245 1159 1246 procedure TDpiGraphic.LoadFromFile(const Filename: string); 1160 begin 1161 GetVclGraphic.LoadFromFile(FileName); 1247 var 1248 Bitmap: TBitmap; 1249 begin 1250 Bitmap := TBitmap.Create; 1251 Bitmap.LoadFromFile(FileName); 1252 Width := ScaleFromVcl(Bitmap.Width); 1253 Height := ScaleFromVcl(Bitmap.Height); 1254 if Self is TDpiBitmap then 1255 TBitmap(GetVclGraphic).Canvas.StretchDraw(Bounds(0, 0, Bitmap.Width, Bitmap.Height), Bitmap) 1256 else raise Exception.Create('Unsupported class ' + Self.ClassName); 1257 Bitmap.Free; 1162 1258 end; 1163 1259 … … 1166 1262 function TDpiBitmap.GetHeight: Integer; 1167 1263 begin 1168 Result := ScaleFromVcl(GetVclBitmap.Height);1264 Result := FHeight; 1169 1265 end; 1170 1266 … … 1190 1286 function TDpiBitmap.GetWidth: Integer; 1191 1287 begin 1192 Result := ScaleFromVcl(GetVclBitmap.Width);1288 Result := FWidth; 1193 1289 end; 1194 1290 1195 1291 procedure TDpiBitmap.SetHeight(AValue: Integer); 1196 1292 begin 1293 FHeight := AValue; 1197 1294 GetVclBitmap.Height := ScaleToVcl(AValue); 1198 1295 end; … … 1205 1302 procedure TDpiBitmap.SetWidth(AValue: Integer); 1206 1303 begin 1304 FWidth := AValue; 1207 1305 GetVclBitmap.Width := ScaleToVcl(AValue); 1306 end; 1307 1308 procedure TDpiBitmap.ScreenChanged; 1309 var 1310 Bitmap: TBitmap; 1311 NewWidth: Integer; 1312 NewHeight: Integer; 1313 begin 1314 NewWidth := ScaleToVcl(Width); 1315 NewHeight := ScaleToVcl(Height); 1316 if Assigned(VclBitmap) and ((NewWidth <> VclBitmap.Width) or (NewHeight <> VclBitmap.Height)) then begin 1317 // Rescale bitmap to new size 1318 Bitmap := TBitmap.Create; 1319 Bitmap.SetSize(NewWidth, NewHeight); 1320 Bitmap.PixelFormat := VclBitmap.PixelFormat; 1321 Bitmap.Canvas.StretchDraw(Bounds(0, 0, NewWidth, NewHeight), VclBitmap); 1322 VclBitmap.Free; 1323 VclBitmap := Bitmap; 1324 Canvas.VclCanvas := VclBitmap.Canvas; 1325 end; 1208 1326 end; 1209 1327 … … 1227 1345 end; 1228 1346 1229 procedure TDpiBitmap.SetSize(Width, Height: Integer); 1230 begin 1231 GetVclBitmap.SetSize(ScaleToVcl(Width), ScaleToVcl(Height)); 1347 procedure TDpiBitmap.SetSize(AWidth, AHeight: Integer); 1348 begin 1349 FWidth := AWidth; 1350 FHeight := AHeight; 1351 GetVclBitmap.SetSize(ScaleToVcl(AWidth), ScaleToVcl(AHeight)); 1232 1352 end; 1233 1353 … … 1372 1492 { TDpiPaintBox } 1373 1493 1374 function TDpiPaintBox.GetOnPaint: TNotifyEvent; 1375 begin 1376 Result := VclPaintBox.OnPaint; 1377 end; 1378 1379 procedure TDpiPaintBox.SetOnPaint(AValue: TNotifyEvent); 1380 begin 1381 VclPaintBox.OnPaint := AValue; 1494 procedure TDpiPaintBox.UpdateVclControlPrivate; 1495 begin 1496 VclPaintBox.OnPaint := @PaintHandler; 1497 VclPaintBox.OnMouseDown := @MouseDownHandler; 1498 VclPaintBox.OnMouseUp := @MouseUpHandler; 1499 VclPaintBox.OnMouseMove := @MouseMoveHandler; 1382 1500 end; 1383 1501 … … 1416 1534 end; 1417 1535 1418 1419 1536 { TDpiCanvas } 1420 1537 … … 1471 1588 1472 1589 procedure TDpiCanvas.SetPixel(X, Y: Integer; AValue: TColor); 1473 begin 1590 var 1591 BrushStyle: TBrushStyle; 1592 BrushColor: TColor; 1593 begin 1594 { BrushStyle := GetVclCanvas.Brush.Style; 1595 BrushColor := GetVclCanvas.Brush.Color; 1596 GetVclCanvas.Brush.Color := AValue; 1597 GetVclCanvas.Brush.Style := bsClear; 1598 GetVclCanvas.FillRect(ScaleToVcl(X), ScaleToVcl(Y), ScaleToVcl(X + 1) - 1, ScaleToVcl(Y + 1) - 1); 1599 GetVclCanvas.Brush.Style := BrushStyle; 1600 GetVclCanvas.Brush.Color := BrushColor; 1601 } 1474 1602 GetVclCanvas.Pixels[ScaleToVcl(X), ScaleToVcl(Y)] := AValue; 1475 1603 end; … … 1497 1625 end; 1498 1626 1627 procedure TDpiCanvas.Rectangle(const ARect: TRect); 1628 begin 1629 Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom); 1630 end; 1631 1499 1632 function TDpiCanvas.TextWidth(Text: string): Integer; 1500 1633 begin … … 1550 1683 Source: TRect); 1551 1684 begin 1552 GetVclCanvas.CopyRect(Dest, SrcCanvas.VclCanvas, S ource);1685 GetVclCanvas.CopyRect(Dest, SrcCanvas.VclCanvas, ScaleRectToVcl(Source)); 1553 1686 end; 1554 1687 … … 1591 1724 begin 1592 1725 if not Assigned(VclGraphicControl) then begin 1593 VclGraphicControl := TGraphicControlEx.Create(nil); 1594 (VclGraphicControl as TGraphicControlEx).OnPaint := @PaintHandler; 1726 VclGraphicControl := TGraphicControl.Create(nil); 1595 1727 end; 1596 1728 Result := VclGraphicControl; … … 1600 1732 begin 1601 1733 inherited; 1602 (GetVclGraphicControl as TGraphicControlEx).OnMouseDown := @MouseDownHandler; 1603 (GetVclGraphicControl as TGraphicControlEx).OnMouseUp := @MouseUpHandler; 1604 (GetVclGraphicControl as TGraphicControlEx).OnMouseMove := @MouseMoveHandler; 1734 UpdateVclControlPrivate; 1735 end; 1736 1737 procedure TDpiGraphicControl.UpdateVclControlPrivate; 1738 begin 1739 TGraphicControlEx(GetVclGraphicControl).OnPaint := @PaintHandler; 1740 TControlEx(GetVclControl).OnMouseDown := @MouseDownHandler; 1741 TControlEx(GetVclControl).OnMouseUp := @MouseUpHandler; 1742 TControlEx(GetVclControl).OnMouseMove := @MouseMoveHandler; 1743 // Some VCL component event are not accessible on TGraphicControl level. 1744 // Delegate this responsibility up 1745 { 1746 GetVclGraphicControl.OnPaint := @PaintHandler; 1747 GetVclControl.OnMouseDown := @MouseDownHandler; 1748 GetVclControl.OnMouseUp := @MouseUpHandler; 1749 GetVclControl.OnMouseMove := @MouseMoveHandler; 1750 } 1751 // raise Exception.Create('Missing inicialization of private fields for ' + ClassName); 1752 end; 1753 1754 function TDpiGraphicControl.GetOnPaint: TNotifyEvent; 1755 begin 1756 Result := FOnPaint; 1757 end; 1758 1759 procedure TDpiGraphicControl.SetOnPaint(AValue: TNotifyEvent); 1760 begin 1761 FOnPaint := AValue; 1605 1762 end; 1606 1763 … … 1906 2063 Forms := TDpiForms.Create; 1907 2064 Forms.FreeObjects := False; 1908 Dpi := 1 44;2065 Dpi := 150; 1909 2066 end; 1910 2067 … … 1914 2071 inherited Destroy; 1915 2072 end; 1916 1917 { TDpiWinControl }1918 1919 2073 1920 2074 { TDpiButton } … … 2513 2667 begin 2514 2668 if not Assigned(VclForm) then begin 2515 VclForm := TFormEx.Create (nil);2669 VclForm := TFormEx.CreateNew(nil); 2516 2670 (VclForm as TFormEx).OnMessage := @FormMessageHandler; 2517 2671 //VclForm := TForm.Create(nil);
Note:
See TracChangeset
for help on using the changeset viewer.