Author: Jonas Bilinkevicius
The message box has limited icons as set by Microsoft. I would like to use one of
the icon I have and insert it into the message box. Is there a way to do that? Do I
have to create a component to handle it?
Answer:
1
2 function CustMsgBox(const AMsg, ACaption, BCap1, BCap2, BCap3: string;
3 IconInd: integer; FocusInd: byte; Mainform: TForm): integer;
4 const
5 Userexe: array[0..9] of char = 'user.exe';
6 const
7 {$IFDEF Win32}
8 BHeight = 23;
9 {$ELSE}
10 BHeight = 25;
11 {$ENDIF}
12 BWidth = 77;
13 var
14 W: TForm;
15 lCaption: TLabel;
16 But1, But2, But3: TButton;
17 i1: integer;
18 Image1: TImage;
19 IHandle: THandle;
20 P1: array[byte] of char;
21 Textsize: TSize;
22 MDC: hDC;
23 CurMetrics: TTextMetric;
24 Curfont: HFont;
25 Msgrect: TRect;
26 begin
27 W := TForm.CreateNew(Application);
28 But2 := nil;
29 But3 := nil;
30 try {set up form}
31 W.BorderStyle := bsDialog;
32 W.Ctl3D := True;
33 W.Width := 360;
34 W.Height := 160;
35 W.Caption := ACaption;
36 W.Font.Name := 'Arial' {Mainform.Font.Name};
37 W.Font.CharSet := BALTIC_CHARSET;
38 W.Font.Size := Mainform.Font.Size;
39 W.Font.Style := Mainform.Font.Style;
40 {Get text extent}
41 for i1 := 0 to 25 do
42 P1[i1] := Chr(i1 + Ord('A'));
43 for i1 := 0 to 25 do
44 P1[i1 + 26] := Chr(i1 + Ord('a'));
45 GetTextExtentPoint(W.Canvas.Handle, P1, 52, Textsize);
46 {Get line height}
47 MDC := GetDC(0);
48 CurFont := SelectObject(MDC, W.Font.Handle);
49 GetTextMetrics(MDC, CurMetrics);
50 SelectObject(MDC, CurFont);
51 ReleaseDC(0, MDC);
52 {Set icon}
53 Image1 := TImage.Create(W);
54 StrPCopy(P1, ParamStr(0));
55 if Image1 <> nil then
56 begin
57 Image1.Width := Image1.Picture.Icon.Width;
58 Image1.Height := Image1.Picture.Icon.Height;
59 Image1.Left := 20;
60 Image1.Top := Textsize.CY + (Textsize.CY div 2);
61 Image1.Width := 32;
62 Image1.Height := 32;
63 Image1.Parent := W;
64 Image1.Name := 'Image';
65 {get icon index}
66 case IconInd of
67 16: IHandle := ExtractIcon(hInstance, userexe, 3);
68 32: IHandle := ExtractIcon(hInstance, userexe, 2);
69 48: IHandle := ExtractIcon(hInstance, userexe, 1);
70 64: IHandle := ExtractIcon(hInstance, userexe, 4);
71 128: IHandle := ExtractIcon(hInstance, userexe, 0);
72 256: IHandle := ExtractIcon(hInstance, userexe, 5);
73 512: IHandle := ExtractIcon(hInstance, userexe, 6);
74 else
75 IHandle := ExtractIcon(hInstance, P1, IconInd);
76 end;
77 if IHandle <> 0 then
78 Image1.Picture.Icon.Handle := IHandle
79 else
80 Image1.Picture.Icon := Application.Icon;
81 end;
82 SetRect(MsgRect, 0, 0, Screen.Width div 2, 0);
83 DrawText(W.Canvas.Handle, PChar(AMsg), -1, MsgRect, DT_CALCRECT or
84 DT_WORDBREAK);
85 {set up label}
86 lCaption := TLabel.Create(W);
87 lCaption.Parent := W;
88 lCaption.Left := 72;
89 lCaption.Top := Image1.Top;
90 lCaption.Width := Msgrect.Right;
91 LCaption.Height := Msgrect.Bottom;
92 lCaption.Autosize := False;
93 lCaption.WordWrap := True;
94 {Adjust form width...must do here to accommodate buttons}
95 W.Width := lCaption.Left + lCaption.Width + 30;
96 lCaption.Caption := AMsg;
97 {buttons}
98 But1 := TButton.Create(W);
99 But1.Parent := W;
100 But1.Caption := BCap1;
101 But1.ModalResult := 1;
102 if BCap2 <> '' then
103 begin
104 But2 := TButton.Create(W);
105 But2.Parent := W;
106 But2.Caption := BCap2;
107 But2.ModalResult := 2;
108 if BCap3 <> '' then
109 begin
110 But3 := TButton.Create(W);
111 But3.Parent := W;
112 But3.Caption := BCap3;
113 But3.ModalResult := 3;
114 end;
115 end;
116 {Set button positions}
117 {set height depending on whether icon or message is tallest}
118 if lCaption.Height > Image1.Height then
119 But1.Top := (lCaption.Top + lCaption.Height + 20)
120 else
121 But1.Top := (Image1.Top + Image1.Height + 20);
122 But1.Width := BWidth;
123 But1.Height := BHeight;
124 if But2 <> nil then
125 begin
126 But2.Height := BHeight;
127 But2.Width := BWidth;
128 But2.Top := But1.Top;
129 if But3 <> nil then
130 begin
131 But3.Top := But1.Top;
132 But3.Width := BWidth;
133 But3.Height := BHeight;
134 But3.Left := (W.Width div 2) + ((BWidth div 2) + 8);
135 But2.Left := (W.Width div 2) - (BWidth div 2);
136 But1.Left := (W.Width div 2) - ((BWidth div 2) + BWidth + 8);
137 But3.Cancel := True;
138 end
139 else
140 begin
141 But2.Left := (W.Width div 2) + 4;
142 But1.Left := (W.Width div 2) - (BWidth + 4);
143 end;
144 end
145 else
146 begin
147 But1.Left := (W.Width div 2) - (BWidth div 2);
148 end;
149 {set focus}
150 case FocusInd of
151 3:
152 if BCap3 <> '' then
153 But3.default := True;
154 2:
155 if BCap2 <> '' then
156 But2.default := True;
157 else
158 But1.default := True;
159 end;
160 {Set clientheight to proper height}
161 W.ClientHeight := But1.Top + But1.Height + Textsize.CY;
162 { Left := (W.ClientWidth div 2) - (((OKButton.Width * 2) + 10) div 2) }
163 {Show messagebox}
164 {Set position}
165 { Position := poScreenCenter; }
166 W.Left := Mainform.Left + ((Mainform.Width - W.Width) div 2);
167 W.Top := Mainform.Top + ((Mainform.Height - W.Height) div 2);
168 W.ShowModal;
169 Result := W.ModalResult;
170 finally
171 W.Free;
172 end;
173 end;
|