P.45.6 Ответы
program MyProgram;
uses WObjects, WinProcs, WinTypes, Strings;
const
cmAAAA1 = 101;
cmAAAA2 = 102;
cmAAAA3 = 103;
cmBBBB1 = 104;
cmBBBB2 = 105;
cmBBBB3 = 106;
type
TMyWindow = object(TWindow)
PrizLine : Boolean; (* признак того, что нарисована линия *)
PrizBrush : Boolean; (* признак того, что нарисована кисть *)
PrizWord : Boolean; (* признак того, что нарисовано слово *)
constructor Init(AParent: PWindowsObject; ATitle: PChar);
procedure DrawLine(var Msg: TMessage); virtual cm_First + cmAAAA1;
procedure DrawBrush(var Msg: TMessage); virtual cm_First +
cmAAAA2;
procedure DrawWord(var Msg: TMessage); virtual cm_First + cmAAAA3;
procedure ClearLine(var Msg: TMessage); virtual cm_First +
cmBBBB1;
procedure ClearBrush(var Msg: TMessage); virtual cm_First +
cmBBBB2;
procedure ClearWord(var Msg: TMessage); virtual cm_First +
cmBBBB3;
procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
virtual;
end;
PMyWindow = ^TMyWindow;
TMyApplication = object(TApplication)
procedure InitMainWindow; virtual;
end;
procedure TMyApplication.InitMainWindow;
var
PP, Dest : array[0..99] of char;
MM, AAAA, BBBB : HMenu;
begin
MainWindow :=
New(PMyWindow, Init(nil,’’));
MM:=CreateMenu;
AAAA:=CreatePopupMenu;
BBBB:=CreatePopupMenu;
StrPCopy(PP,’Line’);
InsertMenu(AAAA,1,mf_Unchecked,cmAAAA1,PP);
StrPCopy(PP,’Brush’);
InsertMenu(AAAA,2,mf_Unchecked,cmAAAA2,PP);
StrPCopy(PP,’Word’);
InsertMenu(AAAA,3,mf_Unchecked,cmAAAA3,PP);
StrPCopy(PP,’Line’);
InsertMenu(BBBB,1,mf_Unchecked,cmBBBB1,PP);
StrPCopy(PP,’Brush’);
InsertMenu(BBBB,2,mf_Unchecked,cmBBBB2,PP);
StrPCopy(PP,’Word’);
InsertMenu(BBBB,3,mf_Unchecked,cmBBBB3,PP);
StrPCopy(PP,’Draw’);
InsertMenu(MM,1,mf_Popup,AAAA,PP);
StrPCopy(PP,’Clear’);
InsertMenu(MM,1,mf_Popup,BBBB,PP);
PMyWindow(MainWindow)^.Attr.Menu:=MM;
end;
constructor TMyWindow.Init(AParent: PWindowsObject; ATitle: PChar);
begin
TWindow.Init(AParent,ATitle);
PrizLine:=false;
PrizBrush:=false;
PrizWord:=false;
end;
procedure TMyWindow.DrawLine(var Msg: TMessage);
begin
PrizLine:=true;
InvalidateRect(HWindow, nil, True);
end;
procedure TMyWindow.DrawBrush(var Msg: TMessage);
begin
PrizBrush:=true;
InvalidateRect(HWindow, nil, True);
end;
procedure TMyWindow.DrawWord(var Msg: TMessage);
begin
PrizWord:=true;
InvalidateRect(HWindow, nil, True);
end;
procedure TMyWindow.ClearLine(var Msg: TMessage);
begin
PrizLine:=false;
InvalidateRect(HWindow, nil, True);
end;
procedure TMyWindow.ClearBrush(var Msg: TMessage);
begin
PrizBrush:=false;
InvalidateRect(HWindow, nil, True);
end;
procedure TMyWindow.ClearWord(var Msg: TMessage);
begin
PrizWord:=false;
InvalidateRect(HWindow, nil, True);
end;
procedure TMyWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
var
PenSize : integer;
PenColor : TColorRef;
ThePen : HPen;
TheBrush : HBrush;
TheFont : HFont;
MyLogFont : TLogFont;
MyLogBrush : TLogBrush;
MyRegion : HRgn;
begin
if PrizLine then begin
PenSize := 1;
PenColor:= RGB(0,255,0);
ThePen := CreatePen(ps_Solid, PenSize, PenColor);
SelectObject(PaintDC, ThePen);
MoveTo(PaintDC, 20, 20);
LineTo(PaintDC, 100,40);
end
else begin
PenSize := 1;
PenColor:= RGB(255,255,255);
ThePen := CreatePen(ps_Solid, PenSize, PenColor);
SelectObject(PaintDC, ThePen);
MoveTo(PaintDC, 20, 20);
LineTo(PaintDC, 100,40);
end;
DeleteObject(ThePen);
MyRegion:=CreateRectRgn(150,150,250,250);
MyLogBrush.lbStyle:=bs_Solid;
MyLogBrush.lbHatch:=0;
if PrizBrush then begin
MyLogBrush.lbColor:=RGB(255,0,0);
TheBrush:=CreateBrushIndirect(MyLogBrush);
SelectObject(PaintDC, TheBrush);
FillRgn(PaintDC,MyRegion,TheBrush);
end
else begin
MyLogBrush.lbColor:=RGB(255,255,255);
TheBrush:=CreateBrushIndirect(MyLogBrush);
SelectObject(PaintDC, TheBrush);
FillRgn(PaintDC,MyRegion,TheBrush);
end;
DeleteObject(TheBrush);
MyLogFont.lfHeight:=16;
MyLogFont.lfWidth:=16;
MyLogFont.lfEscapement:=0;
MyLogFont.lfOrientation:=0;
MyLogFont.lfWeight:=fw_Medium;
MyLogFont.lfItalic:=0;
MyLogFont.lfUnderline:=0;
MyLogFont.lfStrikeOut:=0;
MyLogFont.lfCharSet:=ANSI_CharSet;
MyLogFont.lfOutPrecision:=Out_Character_Precis;
MyLogFont.lfClipPrecision:=Clip_Default_Precis;
MyLogFont.lfQuality:=Proof_Quality;
MyLogFont.lfPitchAndFamily:=Variable_Pitch or ff_Swiss;
StrCopy(@MyLogFont.lfFaceName,’Courier’);
TheFont:=CreateFontInDirect(MyLogFont);
SelectObject(PaintDC,TheFont);
if PrizWord then begin
SetTextColor(PaintDC,RGB(0,0,0));
TextOut(PaintDC,60,60,’Word’,4);
end
else begin
SetTextColor(PaintDC,RGB(255,255,255));
TextOut(PaintDC,60,60,’Word’,4);
end;
DeleteObject(TheFont);
end;
var
MyApp: TMyApplication;
begin
MyApp.Init(‘’);
MyApp.Run;
MyApp.Done;
end.