Develop and Download Open Source Software

Browse Subversion Repository

Contents of /paintfrm.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Mon Nov 7 12:03:00 2011 UTC (12 years, 4 months ago) by shiraishikazuo
File MIME type: text/x-pascal
File size: 17700 byte(s)


1 unit paintfrm;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}{$H+}
5 {$ENDIF}
6
7 (***************************************)
8 (* Copyright (C) 2009, SHIRAISHI Kazuo *)
9 (***************************************)
10
11
12 interface
13
14 uses Types,Classes, Graphics, Forms, Controls, Menus,
15 StdCtrls, Dialogs, {Printers,} SysUtils, Clipbrd, ComCtrls, ExtCtrls,
16 LResources;
17
18
19 type
20
21 { TPaintForm }
22
23 TPaintForm = class(TForm)
24 FontDialog1: TFontDialog;
25 MainMenu1: TMainMenu;
26 File1: TMenuItem;
27 Exit1: TMenuItem;
28 Option1: TMenuItem;
29 Font1: TMenuItem;
30 N2: TMenuItem;
31 Print1: TMenuItem;
32 N3: TMenuItem;
33 SaveAs1: TMenuItem;
34 Save1: TMenuItem;
35 N4: TMenuItem;
36 Close1: TMenuItem;
37 Edit1: TMenuItem;
38 Copy1: TMenuItem;
39 Run1: TMenuItem;
40 Break1: TMenuItem;
41 SaveDialog1: TSaveDialog;
42 OpenDialog1: TOpenDialog;
43 Open1: TMenuItem;
44 Paste1: TMenuItem;
45 Show1: TMenuItem;
46 TextWindow1: TMenuItem;
47 StatusBar1: TStatusBar;
48 PaintBox1: TPaintBox;
49 procedure Font1Click(Sender: TObject);
50 procedure Option1Click(Sender: TObject);
51 procedure Open1Click(Sender: TObject);
52 procedure Exit1Click(Sender: TObject);
53 procedure Close1Click(Sender: TObject);
54 procedure Print1Click(Sender: TObject);
55 procedure Copy1Click(Sender: TObject);
56 procedure Paste1Click(Sender: TObject);
57 procedure FormClose(Sender: TObject; var Action: TCloseAction);
58 procedure FormCreate(Sender: TObject);
59 procedure FormDestroy(Sender: TObject);
60 procedure Break1Click(Sender: TObject);
61 procedure SaveAs1Click(Sender: TObject);
62 procedure Save1Click(Sender: TObject);
63 procedure Size1Click(Sender: TObject);
64 procedure FormResize(Sender: TObject);
65 {
66 procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
67 Shift: TShiftState; X, Y: Integer);
68 }
69 procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
70 Y: Integer);
71 procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
72 Shift: TShiftState; X, Y: Integer);
73 procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
74 Shift: TShiftState; X, Y: Integer);
75
76 procedure PaintBox1Click(Sender: TObject);
77 procedure E1Click(Sender: TObject);
78 procedure PaintBox1Paint(Sender: TObject);
79 procedure StatusBar1Resize(Sender: TObject);
80
81 private
82 mouseX,mouseY:integer;
83 MouseClick:boolean;
84 mousestate:TShiftState;
85 procedure SizeAdjust;
86 //procedure CMMouseLeave(var Message:TMessage);message CM_MOUSELEAVE;
87 //procedure PolyGonSub(Canvas:TCanvas; const Points:array of TPoint);
88 public
89 BitMap1:TBitMap;
90 BitMapHeight:integer;
91 BitMapWidth:integer;
92 procedure Clear;
93 procedure initial;
94 procedure SetSize1;
95 procedure setsize2;
96 procedure getpoint(var a,b:integer);
97 procedure MoveMouse(a,b:integer);
98 procedure MousePol(var a,b:integer; var l,r:boolean);
99 procedure SetBitmapSize(w,h:integer);
100 procedure OpenFile(FileName: string);
101 procedure SaveFile(FileName: string);
102 procedure SaveFileFormat(FileName: string; pf:TPixelFormat);
103 procedure SaveJpegFile(FileName:string; p:integer);
104 procedure SaveGifFile(FileName:string);
105
106 end;
107 var
108 paintform:TPaintForm;
109
110 implementation
111 uses lclintf, LclType,
112 textfrm,base,base2, printbmp,
113 myutils, arithmet, sconsts, graphsys;
114 {$R *.lfm}
115
116 var
117 RightMargin:integer=8;
118 BottomMargin:integer=60;
119
120 procedure TPaintForm.Open1Click(Sender: TObject);
121 begin
122 with TOpenDialog.Create(nil) do
123 try
124 options:=[ofPathMustExist,ofFileMustExist];
125 Filter :=
126 s_Bitmap+ '|*.BMP;*.JPG;*.JPEG;*.GIF'; { Set the OpenDialog Filters.}
127 DefaultExt:='BMP'; { Set the default extension }
128 if Execute then
129 begin
130 OpenFile(FileName);
131 Caption :=FileName;
132 end;
133 finally
134 free;
135 end;
136 end;
137 (*
138 procedure TPaintForm.Open1Click(Sender: TObject);
139 begin
140 OpenDialog1.Filter :=
141 s_Bitmap+ '|*.bmp';
142 OpenDialog1.DefaultExt :='bmp';
143 if OpenDialog1.Execute then
144 begin
145 OpenFile(OpenDialog1.FileName);
146 Caption :=OpenDialog1.FileName;
147 end;
148
149 end;
150 *)
151
152 procedure TPaintForm.Font1Click(Sender: TObject);
153 begin
154 FontDialog1.Font:=Font;
155 if FontDialog1.execute then
156 Font:=FontDialog1.Font;
157 BitMap1.Canvas.Font.assign(Font);
158 PaintBox1.Canvas.Font.assign(Font);
159 end;
160
161
162 procedure TPaintForm.Exit1Click(Sender: TObject);
163 begin
164 Application.terminate;
165 end;
166
167 procedure TPaintForm.Close1Click(Sender: TObject);
168 begin
169 Close; { Close the form }
170 end;
171
172 procedure TPaintForm.Print1Click(Sender: TObject);
173 begin
174 PrintBitMap(BitMap1);
175 end;
176
177
178 procedure TPaintForm.Copy1Click(Sender: TObject);
179 begin
180 ClipBoard.Assign(BitMap1);
181 end;
182
183
184
185 procedure TPaintForm.FormClose(Sender: TObject; var Action: TCloseAction);
186 begin
187 if Application.mainform=self then
188 Action:=caFree
189 else
190 Action:=caMiniMize;
191 if MessageDlg('Continue?', mtCustom, [mbOk,MbAbort], 230)=MrAbort then
192 raise EStop.create;
193
194 end;
195
196 procedure TPaintForm.clear;
197 var
198 NewRect:TRect;
199 svBrushColor:TColor;
200 begin
201 NewRect:=Rect(0,0,Bitmap1.width,Bitmap1.Height);
202
203 with BitMap1.Canvas do
204 begin
205 svBrushColor:=Brush.Color ;
206 Brush.color:=Mypalette.pal[0] or $2000000;
207 FillRect(NewRect);
208 Brush.Color:=svBrushColor;
209 end;
210 //PaintBox1.Canvas.Brush.color:=MyPalette[0];
211 //PaintBox1.Canvas.FillRect(NewRect);
212 if not HiddenDrawMode then
213 PaintBox1.repaint;
214 end;
215 {
216 procedure TPaintForm.clear;
217 var
218 NewRect:TRect;
219 begin
220 NewRect:=Rect(0,0,Bitmap1.width,Bitmap1.Height);
221 BitMap1.Canvas.Brush.color:=MyPalette[0] or $2000000;
222 BitMap1.Canvas.FillRect(NewRect);
223 if not HiddenDrawMode then
224 PaintBox1.repaint;
225 end;
226 }
227
228 procedure TpaintForm.Initial;
229 begin
230 MyPalette.PaletteDisabled:=false;
231 //MyPalette.PaletteNumber:=ColorIndexDlg.RadioGroup1.ItemIndex;
232
233 BitMap1.Canvas.Font.assign(Font);
234 PaintBox1.Canvas.Font.assign(Font);
235
236 if not KeepGraphic then
237 begin
238 SetSize1;
239 clear;
240 end;
241 end;
242
243
244 procedure TPaintForm.SetSize1;
245 begin
246
247 try
248 if BmpSize >=BMP321 then
249 begin
250 case BmpSize of
251 BMP321: BitMapHeight:= 321;
252 BMP401: BitMapHeight:= 401;
253 BMP501: BitMapHeight:= 501;
254 BMP641: BitMapHeight:= 641;
255 BMP801: BitMapHeight:= 801;
256 BMP1001: BitMapHeight:=1001;
257 BMP1281: BitMapHeight:=1281;
258 BMP1601: BitMapHeight:=1601;
259 BMP2001: BitMapHeight:=2001;
260 end;
261 BitMapWidth:=BitMapHeight;
262 end
263 else
264 begin
265 BitMapWidth:=640;
266 case BmpSize of
267 BMPpc9801: BitMapHeight:=400;
268 BMPdosv: BitMapHeight:=480;
269 end;
270 end;
271 BitMap1.width:=BitMapWidth;
272 BitMap1.Height:=BitMapHeight;
273
274 SetSize2;
275 except
276 if BMPsize<>BMP321 then
277 begin
278 BmpSize:=BMP321;
279 SetSize1
280 end;
281 end;
282
283 end;
284
285
286
287 procedure TPaintForm.setsize2;
288 begin
289 PaintBox1.Width:=Bitmap1.width;
290 PaintBox1.Height:=Bitmap1.Height ;
291 visible:=true;
292 ClientWidth := Bitmap1.Width +2; { Adjust clientwidth to match }
293 ClientHeight := Bitmap1.Height + 1 + Statusbar1.height; { Adjust clientheight to match }
294 sizeadjust;
295 ScreenBMPGraphSys.InitCoordinate
296 end;
297
298
299
300 procedure TPaintForm.SizeAdjust;
301 var
302 ScreenClientWidth,ScreenClientHeight:integer;
303 h:integer;
304 begin
305 ScreenClientWidth:= GetSystemMetrics(SM_CXFULLSCREEN);
306 ScreenClientHeight:= GetSystemMetrics(SM_CYFULLSCREEN);
307 if width<ScreenClientWidth div 8 * 7 then
308 left:=ScreenClientwidth-width-12;
309 if Height<ScreenClientHeight-25 then
310 top:=(ScreenClientHeight-Height)-25;
311
312 end;
313
314 procedure TPaintForm.FormResize(Sender: TObject);
315 var
316 h,w:integer;
317 begin
318 if Bitmap1=nil then exit;
319
320 w:=Bitmap1.Width + 2;
321 h:=BitMap1.Height + 1 + StatusBar1.height;
322 if ClientWidth>w then
323 ClientWidth:=w;
324 if ClientHeight>h then
325 ClientHeight:=h;
326 refresh;
327 Application.Processmessages;
328 end;
329
330
331 procedure TPaintForm.SaveFileFormat(FileName: string; pf:TPixelFormat);
332 var
333 Bitmap2:TBitMap;
334 begin
335 BitMap2:= TBitMap.Create;
336 Bitmap2.Assign(BitMap1);
337 if pf=pf1bit then
338 bitmap2.Monochrome:=true;
339 Bitmap2.pixelFormat:=pf;
340 BitMap2.SaveToFile(FileName);
341 BitMap2.Free;
342 refresh;
343 end;
344
345 procedure TPaintForm.SaveJpegFile(FileName:string; p:integer);
346 var
347 jpe:TJpegImage;
348 begin
349 jpe:=TJpegImage.Create;
350 try
351 if p>0 then
352 jpe.CompressionQuality:=p;
353 jpe.Assign(BitMap1);
354 //jpe.Compress; //Lazarus���������������
355 jpe.SaveToFile(FileName);
356 finally
357 jpe.Free;
358 end;
359 end;
360
361 procedure TPaintForm.SaveGifFile(FileName:string);
362 var
363 gif:TGifImage;
364 begin
365 gif:=TGifImage.create;
366 try
367 gif.Assign(Bitmap1);
368 //gif.Optimize([ooCleanup],rmNone,dmNearest,8); //Lazarus���������������
369 gif.SaveToFile(FileName);
370 finally
371 gif.free
372 end;
373 end;
374
375 (*
376 procedure TPaintForm.OpenFile(FileName: string);
377 begin
378 Paintbox1.visible:=false;
379 BitMap1.LoadFromFile(FileName);
380 //Bitmap1.pixelFormat:=pf24bit;
381 setSize2;
382 Paintbox1.visible:=true;
383 end;
384 *)
385
386 procedure TPaintForm.OpenFile(FileName: string);
387 var
388 ext:string;
389 jpe:TJPegImage;
390 gif:TGifImage;
391 begin
392 Paintbox1.visible:=false;
393 ext:=UpperCase( ExtractFileExt(FileName));
394 if ext='.BMP' then
395 BitMap1.LoadFromFile(FileName) { Load the image from disk }
396
397 else if ext='.GIF' then
398 begin
399 gif:=TGifImage.create;
400 try
401 gif.LoadFromFile(FileName);
402 BITMAP1.Assign(gif);
403 finally
404 gif.Free;
405 end;
406 end
407
408 else
409 begin
410 jpe:=TJpegImage.create;
411 try
412 jpe.LoadFromFile(FileName);
413 //jpe.DIBNeeded;
414 BitMap1.Assign(jpe);
415 finally
416 jpe.Free;
417 end;
418 end;
419 Bitmap1.pixelFormat:=pf24bit; //���������������
420 setSize2;
421 Paintbox1.visible:=true;
422 end;
423
424
425 procedure TPaintForm.SetBitmapSize(w,h:integer);
426 begin
427 Paintbox1.Visible:=false;
428 Bitmap1.width:=w;
429 Bitmap1.height:=h;
430 SetSize2;
431 Paintbox1.visible:=true;
432 end;
433
434
435 procedure TPaintForm.Break1Click(Sender: TObject);
436 begin
437 CtrlBreakHit:=true ;
438 //FrameForm.SetBreakMessage;
439 end;
440
441
442 procedure TPaintForm.PaintBox1Click(Sender: TObject);
443 begin
444 MouseClick:=true;
445 end;
446
447 procedure TPaintForm.FormCreate(Sender: TObject);
448 begin
449 OpenDialog1.Title:=s_OpenFile;
450 SaveDialog1.Title:=s_SaveFile;
451 Break1.ShortCut:=ShortCut(Word(BreakKey), [ssCtrl]);
452
453 with Font do
454 begin
455 CharSet:=OEM_CHARSET;
456 style:=[];
457 end;
458 with TMyIniFile.create('PaintFont') do
459 begin
460 RestoreFont(Font);
461 RightMargin:=ReadInteger('RightMargin',RightMargin);
462 BottomMargin:=ReadInteger('BottomMargin',BottomMargin);
463 free
464 end;
465
466 BitMap1:= TBitMap.Create;
467 Bitmap1.pixelFormat:=pf24bit;
468 Bitmap1.Canvas.Font.Assign(Font);
469 NotXorMode:=false;
470 HiddenDrawMode:=false;
471 ScreenBMPGraphSys.SetUp;
472 SetSize1;
473
474 Visible:=false; //Windowstate:=wsMinimized;
475 //Application.ProcessMessages;
476
477 end;
478
479 procedure TPaintForm.FormDestroy(Sender: TObject);
480 begin
481 BitMap1.Free;
482 BitMap1:=nil;
483
484 with TMyIniFile.create('PaintFont') do
485 begin
486 StoreFont(Font);
487 free
488 end;
489 end;
490
491 procedure TPaintForm.SaveAs1Click(Sender: TObject);
492 var
493 ext:string;
494 begin
495 ext:='';
496 SaveDialog1.FileName:=ChangeFileExt(Caption,ext);
497 if SaveDialog1.Execute then
498 begin
499 Caption :=SaveDialog1.FileName;
500 ext:=UpperCase( ExtractFileExt(Caption));
501 if ext='.BMP' then
502 begin
503 if SaveDialog1.FilterIndex=0{1} then //Delphi���index���������������?
504 SaveFile(Caption)
505 else if SaveDialog1.FilterIndex=1{2} then
506 SaveFileFormat(Caption,pf4bit)
507 else
508 SaveFileFormat(Caption,pf1bit);
509 end
510 else if (ext='.JPG') or (ext='.JPEG') or (ext='.JPE') then
511 saveJpegFile(Caption,0)
512 else if ext='.GIF' then
513 saveGifFile(Caption)
514 end;
515 end;
516
517 (*
518 procedure TPaintForm.SaveAs1Click(Sender: TObject);
519 begin
520 SaveDialog1.FileName:='*.BMP'; //ChangeFileExt(Caption,'.bmp');
521 if SaveDialog1.Execute then
522 begin
523 Caption :=SaveDialog1.FileName;
524 if SaveDialog1.FilterIndex=1 then
525 SaveFile(Caption)
526 else if SaveDialog1.FilterIndex=2 then
527 SaveFileFormat(Caption,pf8bit)
528 else
529 SaveFileFormat(Caption,pf1bit);
530 end;
531 end;
532 *)
533
534 procedure TPaintForm.Save1Click(Sender: TObject);
535 begin
536 if Caption='' then
537 SaveAs1Click(Sender)
538 else
539 SaveFile(Caption)
540 end;
541
542 procedure TPaintForm.OPtion1Click(Sender: TObject);
543 begin
544 // SetOption
545 end;
546
547 procedure TPaintForm.Size1Click(Sender: TObject);
548 begin
549 // OptionSizeDlg.Execute;
550 // SetSize1
551 end;
552
553 procedure TPaintForm.Paste1Click(Sender: TObject);
554 begin
555 //if ClipBoard.Provides('image/delphi.bitmap') then
556 begin
557 Paintbox1.Visible:=false;
558 BitMap1.Assign(ClipBoard);
559 //Bitmap1.pixelFormat:=pf24bit;
560 SetSize2;
561 Paintbox1.visible:=true;
562 end;
563 end;
564
565
566
567 procedure TPaintForm.E1Click(Sender: TObject);
568 begin
569 TextForm.Visible:=true;
570 TextForm.BringToFront
571 end;
572
573 procedure TPaintForm.PaintBox1Paint(Sender: TObject);
574 begin
575 if not HiddenDrawMode then
576 PaintBox1.Canvas.Draw(0,0,BitMap1);
577 end;
578
579 procedure TPaintForm.StatusBar1Resize(Sender: TObject);
580 begin
581
582 end;
583
584
585 {
586 procedure TPaintForm.CMMouseLeave(var Message:TMessage);
587 begin
588 inherited;
589 With statusBar1 do
590 begin
591 Panels[0].text := '';
592 Panels[0].text := '';
593 end;
594 mousestate:=[]; //2004.8.22
595 Set8087CW(controlword);
596 end;
597 }
598
599
600 {
601 procedure TPaintForm.GetPoint(var a,b:integer);
602 var
603 svCtrlBreakHit:boolean;
604 begin
605 svCtrlBreakHit:=CtrlBreakHit;
606 CtrlBreakHit:=false;
607 MouseClick:=false;
608 repeat
609 sleep(10);IdleImmediately;
610 until MouseClick or CtrlBreakHit;
611 a:=mouseX;
612 b:=mouseY;
613 CtrlBreakHit:=CtrlBreakHit or svCtrlBreakHit;
614 end;
615 }
616
617 procedure TPaintForm.GetPoint(var a,b:integer);
618 var
619 svCtrlBreakHit:boolean;
620 begin
621 svCtrlBreakHit:=CtrlBreakHit;
622 CtrlBreakHit:=false;
623 MouseClick:=false;
624 repeat
625 sleep(10);IdleImmediately;
626 if CtrlBreakHit then
627 if MessageDlg(s_ConfirmToBreak,mtConfirmation,[mbOk,mbAbort],0)=mrAbort then
628 raise EStop.create
629 else
630 begin
631 CtrlBreakHit:=false;
632 svCtrlBreakHit:=true;
633 end;
634 until MouseClick ;
635 a:=mouseX;
636 b:=mouseY;
637 CtrlBreakHit:=CtrlBreakHit or svCtrlBreakHit;
638 end;
639
640
641 procedure TPaintForm.MoveMouse(a,b:integer);
642 var
643 P:TPoint;
644 begin
645 P.X:= a;
646 P.Y:= b;
647 Mouse.CursorPos:=PaintBox1.ClientToScreen(P);
648 end;
649
650
651 procedure TPaintForm.MousePol(var a,b:integer; var l,r:boolean);
652 begin
653 IdleImmediately;
654 a:=mouseX;
655 b:=mouseY;
656 l:=ssleft in mousestate;
657 r:=ssright in mousestate;
658 end;
659
660 procedure TPaintForm.SaveFile(FileName: string);
661 begin
662 BitMap1.SaveToFile(FileName);
663 refresh;
664 end;
665
666 (*
667 procedure TPaintForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
668 Shift: TShiftState; X, Y: Integer);
669 var
670 a,b:number;
671 begin
672 Set8087CW(controlword);
673 MouseX:=x;
674 MouseY:=y;
675
676 if (sender=Paintbox1) and not invalidCoordinate then
677 begin
678 convert(ScreenBMPGraphSys.Virtualx(x),a);
679 convert(ScreenBMPGraphSys.Virtualy(y),b);
680 round9(a);
681 round9(b);
682 StatusBar1.Panels[0].text:=DStr(a);
683 StatusBar1.Panels[1].text:=DStr(b);
684 end
685 else
686 begin
687 StatusBar1.Panels[0].text:='';
688 StatusBar1.Panels[1].text:='';
689 end
690 end;
691 *)
692
693 procedure TPaintForm.PaintBox1MouseMove(Sender: TObject;
694 Shift: TShiftState; X, Y: Integer);
695 var
696 a,b:number;
697 begin
698 MouseX:=x;
699 MouseY:=y;
700
701 if (sender=Paintbox1) and not invalidCoordinate then
702 begin
703 convert(ScreenBMPGraphSys.Virtualx(x),a);
704 convert(ScreenBMPGraphSys.Virtualy(y),b);
705 round9(a);
706 round9(b);
707 StatusBar1.Panels[0].text:=DStr(a);
708 StatusBar1.Panels[1].text:=DStr(b);
709 end
710 else
711 begin
712 StatusBar1.Panels[0].text:='';
713 StatusBar1.Panels[1].text:='';
714 end;
715 end;
716
717
718 procedure TPaintForm.PaintBox1MouseUp(Sender: TObject;
719 Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
720 begin
721 mousestate:=shift;
722 mouseX:=x;
723 mouseY:=y;
724 {$IFDEF LclGTK2} //Bug?
725 MouseState:=[];
726 {$ENDIF}
727 {$IFDEF LclCarbon} //Bug?
728 MouseState:=[];
729 {$ENDIF}
730 end;
731
732 procedure TPaintForm.PaintBox1MouseDown(Sender: TObject;
733 Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
734 begin
735 mousestate:=shift;
736 mouseX:=x;
737 mouseY:=y;
738 end;
739
740 initialization
741
742
743 finalization
744
745 end.

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26