This article introduces the method of implementing picture scrolling switching in Delphi with an example. This program can realize the scrolling broadcast of images, and through this code, the image in the window can be controlled to scroll upward. By adjusting the value of the speed scroll bar, the scrolling speed can also be adjusted; click the "Stop" button, and the image will stop scrolling.
The complete function code is as follows:
unit Unit1;interfaceusesWindows, Classes, Graphics, Forms, Controls, Menus,Dialogs, StdCtrls, ExtCtrls, SysUtils, ComCtrls, Buttons;typeTForm1 = class(TForm)MainMenu1: TMainMenu;File1: TMenuItem;Open1: TMenuItem;N1: TMenuItem;Exit1 : TMenuItem;Help1: TMenuItem;AboutImageViewer1: TMenuItem;OpenDialog1: TOpenDialog;Options1: TMenuItem;ColorDepth1: TMenuItem;ScrollPanel1: TMenuItem;Panel1: TPanel;TrackBar1: TTrackBar;BitBtn2: TBitBtn;BitBtn1: TBitBtn;Label1: TLabel;Image1: TImage;N2: TMenuItem;procedure Open1Click(Sender: TObject);procedure Exit1Click(Sender: TObject);procedure AboutImageViewer1Click(Sender: TObject);procedure ColorDepth1Click(Sender: TObject);procedure ScrollPanel1Click(Sender : TObject); procedure TrackBar1Change(Sender: TObject); procedure BitBtn1Click(Sender: TObject); procedure BitBtn2Click(Sender: TObject); procedure FormCreate(Sender: TObject); privatepublic{ Public declarations }end;varForm1: TForm1;nLines: Integer; fCancel: Boolean;implementation{$R *.DFM}procedure TForm1.Open1Click(Sender: TObject);beginif OpenDialog1.Execute thenbeginImage1.Picture.LoadFromFile (OpenDialog1.FileName);//Import image file Caption := 'Control the dynamic movement of the image - ' + OpenDialog1.FileName;/ /Change the form titleend;end;procedure TForm1.Exit1Click(Sender: TObject);beginClose; //Exit the formend;procedure TForm1.AboutImageViewer1Click(Sender: TObject);//About beginMessageDlg ('Control the dynamic movement of images for'+ #13'Delphi Graphics Studio' , mtInFormation,[mbOk], 0);end;procedure TForm1.ColorDepth1Click(Sender: TObject);varstrDepth: String;begincase Image1.Picture.Bitmap.PixelFormat of //Get the image color depth pfDevice: strDepth := 'No picture';pf1bit: strDepth := '1-bit';pf4bit : strDepth := '4-bit';pf8bit: strDepth := '8-bit';pf15bit: strDepth := '15-bit';pf16bit: strDepth := '16-bit';pf24bit: strDepth := '24-bit';pf32bit: strDepth := '32-bit'; pfCustom: strDepth := 'Custom';end;MessageDlg ('Image color depth:' + strDepth,mtInFormation, [mbOK], 0);//Gives the image color depth informationend;procedure TForm1.ScrollPanel1Click(Sender: TObject);beginPanel1.Visible := not Panel1.Visible;ScrollPanel1.Checked := Panel1.Visible; //Change the visibility of the control panelend;procedure TForm1.TrackBar1Change(Sender: TObject);beginnLines := TrackBar1.Position;TrackBar1.Hint := IntToStr (TrackBar1.Position);//Gives a hint of the current speedend;procedure TForm1.BitBtn1Click(Sender: TObject);varW, H, I, J , LineBytes: Integer;Line: PByteArray; //Define pointer type variable Bmp: Tbitmap;R: TRect;beginif Image1.Picture.Bitmap.PixelFormat=pfDevice then //When the picture is not opened beginMessageDlg ('The picture is not opened! ', mtInFormation, [mbOk], 0); //Give an error message exit; //Exit endelsefCancel := False;BitBtn1.Enabled := False; //Change the properties of the two buttons BitBtn2.Enabled := True;Bmp := Image1.Picture.Bitmap; //Get the BMP image and define the image size W := Bmp.Width;H := Bmp.Height;LineBytes := Abs (Integer (Bmp.ScanLine [1]) -Integer (Bmp.ScanLine [0]));Line := AllocMem (LineBytes);for I := 0 to H - 1 dobeginif fCancel then //Check the break flag before each loop (that is, determine whether the stop button is pressed) Break; //Click the "Stop" button to stop scrolling the image Move ((Bmp.ScanLine [0]), Line, LineBytes); //Copy the first line of the image for J := 1 to H - 1 dobeginMove ((Bmp.ScanLine [J]), (Bmp.ScanLine [J- 1]), LineBytes);if (J mod nLines = 0) thenbeginR := Rect (0, Panel1.Height + J-nLines,W, Panel1.Height + J);InvalidateRect (Handle, @R, False);UpdateWindow (Handle);end;end;Move (Line, (Bmp.ScanLine [Bmp.Height - 1]), LineBytes);R := Rect (0, Panel1.Height + H - nLines,W, Panel1.Height + H);InvalidateRect (Handle, @R, False);UpdateWindow (Handle);Application.ProcessMessages; //Allow the loop to be stopped immediately to put the program in the initial stateend;BitBtn1.Enabled := True; //Activate the "Start" button BitBtn2.Enabled := False; //The stop "button" is invalidend ;procedure TForm1.BitBtn2Click(Sender: TObject);beginfCancel := True; //Change flag Fcancel value, stop image scrollingend;procedure TForm1.FormCreate(Sender: TObject);beginTrackBar1Change (self); //Call the TrackBar1Change functionend;end.