Mega Code Archive

 
Categories / Delphi / Functions
 

Extending the MessageBox function

Title: Extending the MessageBox function Question: This code allows you to add a custom icon to the MessageBox function. You don't have to be restricted to the four common ico images provided by windows anymore.. Answer: { IconResourceName is the name of the icon to use as a custom image. By default a delphi project has only 1 icon resource - called 'MAINICON'. To add images, use the Image Editor to insert icons into the project's .RES file. NOTE: You will need to close then re-open the project for changes. SystemSound is used to play a MessageBox sound when you are using a custom icon. SystemSound can be MB_ICONASTERISK, MB_ICONEXCLAIMATION, MB_ICONHAND or MB_ICONQUESTION. Setting SystemSound to 0 (zero) will cause no sound. UseAppIcon must be set to True. IconResourceName and SystemSound will be ignored if this parameter is False. If UseAppIcon is True, make sure the parameter uType does NOT include a icon specifier. For example-: this is not correct MB_OK + MB_ICONHAND. But this is fine:- MB_OK. } function TForm1.MessageBoxExtended(hWnd: integer; lpText: PChar; lpCaption: PChar; uType: integer; IconResourceName: PChar; SystemSound: integer; UseAppIcon: Boolean): integer; var MBParams: TMsgBoxParams; begin with MBParams do begin cbSize := SizeOf(MBParams); hwndOwner := hWnd; hInstance := SysInit.HInstance; lpszText := lpText; lpszCaption := lpCaption; dwStyle := uType + MB_TOPMOST; if UseAppIcon then dwStyle := dwStyle or MB_USERICON; lpszIcon := IconResourceName; dwContextHelpId := 0; lpfnMsgBoxCallback := nil; dwLanguageId := LANG_NEUTRAL; end; if SystemSound 0 then MessageBeep(SystemSound); Result := integer(MessageBoxIndirect(MBParams)); end; procedure TForm1.Button1Click(Sender: TObject); begin if Self.MessageBoxExtended(Self.handle, 'This is a example of a custom icon inside of your MessageBox' + #10 + 'Try changing the App''s icon, then try adding more..' + #10 + #10 + 'Do you like it?', 'Custom Icons', MB_YESNO, 'MAINICON', MB_ICONQUESTION, True) = ID_YES then Self.MessageBoxExtended(Self.handle, 'I''m very pleased. Enjoy it.', PChar(Application.Title), MB_OK + MB_ICONINFORMATION, '', 0, False) else Self.MessageBoxExtended(Self.handle, 'I''m sorry to hear that.', PChar(Application.Title), MB_OK + MB_ICONINFORMATION, '', 0, False) end;