Friday, March 20, 2020

How to Add Items to a TPopUp Delphi Menu

How to Add Items to a TPopUp Delphi Menu When working with Menus or PopUp menus in Delphi applications, in most scenarios, you create the menu items at design-time. Each menu item is represented by a TMenuItem Delphi class. When a user selects (clicks) an item, the OnClick event is fired for you (as a developer) to grab the event and respond to it. There may be situations when the items of the menu are not known at design time, but need to be added at run-time (dynamically instantiated). Add TMenuItem at Run-Time Suppose there is a TPopupMenu component named PopupMenu1 on a Delphi form, to add an item to the popup menu you could write a piece of code as: var   Ã‚   menuItem : TMenuItem; begin   Ã‚  menuItem : TMenuItem.Create(PopupMenu1) ;   Ã‚  menuItem.Caption : Item added at TimeToStr(now) ;   Ã‚  menuItem.OnClick : PopupItemClick;   Ã‚  //assign it a custom integer value..   Ã‚  menuItem.Tag : GetTickCount;   Ã‚  PopupMenu1.Items.Add(menuItem) ; end; Notes In the above code, one item is added to the PopupMenu1 component. Note that we assigned an integer value to the Tag property. The Tag property (every Delphi component has it) is designed to allow a developer to assign an arbitrary integer value stored as part of the component.The GetTickCount API function retrieves the number of milliseconds that have elapsed since Windows was started.For the OnClick event handler, we assigned PopupItemClick - the name of the function with the *correct* signature. procedure TMenuTestForm.PopupItemClick(Sender: TObject) ; var   Ã‚   menuItem : TMenuItem; begin   Ã‚   if NOT (Sender is TMenuItem) then   Ã‚   begin   Ã‚  Ã‚  Ã‚   ShowMessage(Hm, if this was not called by Menu Click, who called this?!) ;   Ã‚  Ã‚  Ã‚   ShowMessage(Sender.ClassName) ;   Ã‚  Ã‚  Ã‚   exit;   Ã‚   end;   Ã‚   menuItem : TMenuItem(sender) ;   Ã‚   ShowMessage(Format(Clicked on %s, TAG value: %d,[menuItem.Name, menuItem.Tag])) ; end; Important When a dynamically added item is clicked, the PopupItemClick will be executed. In order to differentiate between one or more run-time added items (all executing the code in PopupItemClick) we can use the Sender parameter: The PopupItemClick method first checks if the Sender is actually a TMenuItem object. If the method is executed as a result of a menu item OnClick event handler we simply show a dialog message with the Tag value being assigned when the menu item was added to the menu. Custom String-In TMenuItem In real-world applications, you might/would need more flexibility. Lets say that each item will represent a web page - a string value would be required to hold the URL of the web page. When the user selects this item you could open the default web browser and navigate to the URL assigned with the menu item. Heres a custom TMenuItemExtended class equipped with a custom string Value property: type    TMenuItemExtended class(TMenuItem)    private   Ã‚  Ã‚   fValue: string;    published   Ã‚  Ã‚   property Value : string read fValue write fValue;    end; Heres how to add this extended menu item to a PoupMenu1: var   Ã‚   menuItemEx : TMenuItemExtended; begin   Ã‚   menuItemEx : TMenuItemExtended.Create(PopupMenu1) ;   Ã‚   menuItemEx.Caption : Extended added at TimeToStr(now) ;   Ã‚   menuItemEx.OnClick : PopupItemClick;   Ã‚   //assign it a custom integer value..   Ã‚   menuItemEx.Tag : GetTickCount;   Ã‚   //this one can even hold a string value   Ã‚   menuItemEx.Value : http://delphi.about.com;   Ã‚   PopupMenu1.Items.Add(menuItemEx) ; end; Now, the PopupItemClick must be modified to properly process this menu item: procedure TMenuTestForm.PopupItemClick(Sender: TObject) ; var   Ã‚   menuItem : TMenuItem; begin   Ã‚   //...same as above   Ã‚   if sender is TMenuItemExtended then   Ã‚   begin   Ã‚  Ã‚  Ã‚   ShowMessage(Format(Ohoho Extended item .. heres the string value : %s,[TMenuItemExtended(Sender).Value])) ;   Ã‚   end; end; Thats all. Its up to you to extend the TMenuItemExtended as per your needs. Creating custom Delphi components is where to look for help on creating your own classes/components. Note To actually open up the default Web Browser you can use the Value property as a parameter to a ShellExecuteEx API function.

Tuesday, March 3, 2020

Plane That Crashed Into the Empire State Building

Plane That Crashed Into the Empire State Building On the foggy morning of Saturday, July 28, 1945, Lt. Colonel William Smith was piloting a U.S. Army B-25 bomber through New York City when he crashed into the Empire State Building  at 9:45 a.m, killing 14 people. Fog Lt. Colonel William Smith  was on his way to Newark Airport to pick up his commanding officer, but for some reason, he showed up over LaGuardia Airport and asked for a weather report. Because of the poor visibility, the LaGuardia tower wanted him to land, but Smith requested and received permission from the military to continue on to Newark. The last transmission from the LaGuardia tower to the plane was a foreboding warning: From where Im sitting, I cant see the top of the Empire State Building. Avoiding Skyscrapers Confronted with dense fog, Smith dropped the bomber low to regain visibility, where he found himself in the middle of Manhattan, surrounded by skyscrapers. At first, the bomber was headed directly for the New York Central Building (now called the Helmsley Building) but at the last minute, Smith was able to bank west and miss it. Unfortunately, this put him in line for another skyscraper. Smith managed to miss several skyscrapers until he was headed for the Empire State Building. At the last minute, Smith tried to get the bomber to climb and twist away, but it was too late. The Crash At 9:49 a.m., the ten-ton, B-25 bomber smashed into the north side of the Empire State Building. The majority of the plane hit the 79th floor, creating a hole in the building 18 feet wide and 20 feet high. The planes high-octane fuel exploded, hurtling flames down the side of the building and inside through hallways and stairwells all the way down to the 75th floor. World War II had caused many to shift to a six-day work week; thus there were many people at work in the Empire State Building that Saturday. The plane crashed into the offices of the War Relief Services of the National Catholic Welfare Conference. Catherine OConnor described the crash: The plane exploded within the building. There were five or six seconds- I was tottering on my feet trying to keep my balance- and three-quarters of the office was instantaneously consumed in this sheet of flame. One man was standing inside the flame. I could see him. It was a co-worker, Joe Fountain. His whole body was on fire. I kept calling to him, Come on, Joe; come on, Joe. He walked out of it. Joe Fountain died several days later. Eleven of the office workers were burned to death, some still sitting at their desks, others while trying to run from the flames. Damage From the Crash One of the engines and part of the landing gear hurtled across the 79th floor, through wall partitions and two firewalls, and out the south walls windows to fall onto a 12-story building across 33rd Street. The other engine flew into an elevator shaft and landed on an elevator car. The car began to plummet, slowed somewhat by emergency safety devices. Miraculously, when help arrived at the remains of the elevator car in the basement, the two women inside the car were still alive. Some debris from the crash fell to the streets below, sending pedestrians scurrying for cover, but most fell onto the buildings setbacks on the fifth floor. The bulk of the wreckage, however, remained stuck in the side of the building. After the flames were extinguished and the remains of the victims removed, the rest of the wreckage was removed through the building. Death Toll The plane crash killed 14 people (11 office workers and the three crewmen) plus injured 26 others. Though the integrity of the Empire State Building was not affected, the cost of the damage done by the crash was $1 million. Sources Goldman, Jonathan. The Empire State Building Book. Paperback, St Martins Pr, 1856.Tauranac, John. The Empire State Building: The Making of a Landmark. Paperback, 1 edition, Cornell University Press, March 25, 2014.