How to Select a “Bar” in the TChart Delphi control
Reference : http://delphi.about.com/od/adptips2006/qt/chart_selectbar.htm
Tip submitted by Meze Calin Nicolae
Here’s a method of simulating a selection in the TChart Delphi control. When a user clicks on a bar of a chart, the respective bar changes its color.
Everything is being done using one event, the onClickSeries, which provides us the index of the clicked bar.
Follow the steps:
- Drop a TChart (“Additional” tab) control on a Delphi form.
- Add one (empty) series to the Chart.
- Select a “Bar” chart style.
Declare two form level variables:
colors : array of TColor;
k : integer;
Fill in the Chart with some “dummy” data in the Form’s OnCreate event:
procedure TChartForm.FormCreate(Sender: TObject) ;
begin
SetLength(colors,7) ;
for k := 0 to 6 do colors[k] := clBlue;Chart1.Series[0].Add(10,’test2′,Colors[0]) ;
Chart1.Series[0].Add(70,’test3′,Colors[1]) ;
Chart1.Series[0].Add(90,’test4′,Colors[2]) ;
Chart1.Series[0].Add(40,’test5′,Colors[3]) ;
Chart1.Series[0].Add(66,’test6′,Colors[4]) ;
end;
Handle the OnClickSeries event as:
procedure TChartForm.Chart1ClickSeries(
Sender: TCustomChart;
Series: TChartSeries;
ValueIndex: Integer;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer) ;
begin
for k := 0 to 6 do
if k <> ValueIndex then
colors[k] := clBlue
else
colors[k] := clRed;Chart1.Series[0].Clear;
Chart1.Series[0].Add(10,’test2′,Colors[0]) ;
Chart1.Series[0].Add(70,’test3′,Colors[1]) ;
Chart1.Series[0].Add(90,’test4′,Colors[2]) ;
Chart1.Series[0].Add(40,’test5′,Colors[3]) ;
Chart1.Series[0].Add(66,’test6′,Colors[4]) ;
end;
Capturing a Screen Shot of a TWebBrowser Content (Web Page) in Delphi
Reference : http://delphi.about.com/od/vclusing/a/wb_scren_shot.htm
The TWebBrowser Delphi control provides access to the Web browser functionality from your Delphi apps – to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.
Web Page Screen Shot
You might need to programmatically grab the screen shot of the current page loaded in the web browser control.A screen shot (screen capture) is a copy of the screen’s contents that can be saved as a graphics file.
Web Browser screen shot is a graphics copy of the content on the web browser control – usually a web page (document).
The custom function WebBrowserScreenShot will capture the contents of a TWebBrower’s client area into a JPEG image and save it to a specified file.
uses ActiveX;
procedure WebBrowserScreenShot(const wb: TWebBrowser; const fileName: TFileName) ;
var
viewObject : IViewObject;
r : TRect;
bitmap : TBitmap;
begin
if wb.Document <> nil then
begin
wb.Document.QueryInterface(IViewObject, viewObject) ;
if Assigned(viewObject) then
try
bitmap := TBitmap.Create;
try
r := Rect(0, 0, wb.Width, wb.Height) ;bitmap.Height := wb.Height;
bitmap.Width := wb.Width;viewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Application.Handle, bitmap.Canvas.Handle, @r, nil, nil, 0) ;
with TJPEGImage.Create do
try
Assign(bitmap) ;
SaveToFile(fileName) ;
finally
Free;
end;
finally
bitmap.Free;
end;
finally
viewObject._Release;
end;
end;
end;
Note: JPEG images are smaller when compared to BMPs – this is why the BMP object is converted to a JPG before saving to the disk.A sample usage: navigate to a web site in the form’s OnCreate event, take the screen shot in the webbrowser’s OnNavigateComplete2 event:
procedure TForm1.FormCreate(Sender: TObject) ;
begin
WebBrowser1.Navigate(‘http://delphi.about.com’) ;
end;procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant) ;
begin
WebBrowserScreenShot(WebBrowser1,’c:\WebBrowserImage.jpg’) ;
end;
Note: the code above saves the “http://delphi.about.com” site’s screen shot to a file named WebBrowserImage.jpg on the C drive.
Take a Screen Shot of an Inactive Window
Reference : http://delphi.about.com/od/delphitips2008/qt/print_window.htm
Taking a screen shot of a window using Delphi code is rather easy.
A screen shot (screen capture) is a copy of the screen’s contents that can be saved as a graphics file or displayed in a graphics “aware” control, for example TImage.
In most cases you will want to take a screen shot of the active window or the Windows Desktop.
What if you need to do a screen capture of all the running applications – most of them will be inactive and not visible to the user?
WindowSnap – Inactive Window Screen Capture
Windows XP also introduces the new printing API, PrintWindow. This API enables the caller to snapshot a visual copy of a window into a device context.Drop a TImage (named “Image1”) on a form and use the following code:
WindowSnap(Self.Handle, Image1.Picture.Bitmap) ;
Image1.Refresh;
The actual WindowSnap function is defined as:
function WindowSnap(windowHandle: HWND; bmp: TBitmap): boolean;
var
r: TRect;
user32DLLHandle: THandle;
printWindowAPI: function(sourceHandle: HWND; destinationHandle: HDC; nFlags: UINT): BOOL; stdcall;
begin
result := False;
user32DLLHandle := GetModuleHandle(user32) ;
if user32DLLHandle <> 0 then
begin
@printWindowAPI := GetProcAddress(huser32, ‘PrintWindow’) ;
if @printWindowAPI <> nil then
begin
GetWindowRect(windowHandle, r) ;
bmp.Width := r.Right – r.Left;
bmp.Height := r.Bottom – r.Top;
bmp.Canvas.Lock;
try
result := printWindowAPI(windowHandle, bmp.Canvas.Handle, 0) ;
finally
bmp.Canvas.Unlock;
end;
end;
end;
end; (*WindowSnap*)
Note that the first parameter to the WindowSnap procedure is a HWND value (THandle) – the handle of the window you want to capture.WinDowse is an extremely convenient and easy to use tool for obtaining necessary technical information about any window (handle, child windows, etc.)
Here’s an idea: enumerate top-level windows (to grab their handles) and create your own task-switcher 🙂
Display an Error Message for an OS Error Code
Delphi’s SysErrorMessage function converts OS error codes into a string – an error message string that corresponds to the specified OS error code..
GetLastError returns the last error reported by an operating system API call.
Note: calling getLastError resets the operating system error state – do not call it two times “in a row” – the second call will clear the “first” error.
Convert the OS Error Code into a User Friendly Message
Here’s a quick example – trying to delete a non existing folder. RemoveDir deletes an existing empty directory. The return value is true if a new directory was successfully deleted, false if an error occurred. Read GetLastError to find out why an error occurred.
RemoveDir(‘c:\NoSuchFolder’) ;
ShowMessage(‘System Error Message: ‘+ SysErrorMessage(GetLastError)) ;
Get the control Under Mouse in a Delphi application
Reference : http://delphi.about.com/od/delphitips2008/qt/find-vcl-window.htm
If, in your Delphi application you need to know what control is “under” the mouse while the mouse is being moved over the application, you can use RTL’s FindVCLWindow function.
FindVCLWindow
The VCLWindow function can locate the windowed control under a certain point – for example under the mouse pointer. You can use FindVCLWindow to identify the windowed control that is under the mouse from another control that has captured the mouse. The Pos parameter specifies the location that must be over the returned windowed control. If there is no windowed control under the Pos parameter, FindVCLWindow returns nil.For an example have a TApplicationEvents component on the main form of the application and handle its OnIdle event as:
//Handles OnIdle event of the ApplicationEvents1 control the MainForm
procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean) ;
var
ctrl : TWinControl;
begin
ctrl := FindVCLWindow(Mouse.CursorPos) ;if ctrl <> nil then
begin
Caption := ctrl.Name;//do something if mouse is over TLabeledEdit
if ctrl is TLabeledEdit then
begin
Caption := TLabeledEdit(ctrl).Text;
end;
end;
end;
Note: Mouse.CursorPos returns the coordinate of the mouse pointer related to the screen.