Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I need to detect whether a specific window is minimized or not. For this purpose I have found two functions:
1.
function PAIsWindowMinimized(h: HWND): Boolean;
// Detects whether a window is minimized or not
var
wp: Winapi.Windows.WINDOWPLACEMENT;
begin
wp.length := SizeOf(Winapi.Windows.WINDOWPLACEMENT);
Winapi.Windows.GetWindowPlacement(h, #wp);
Result := wp.showCmd = Winapi.Windows.SW_SHOWMINIMIZED;
end;
2.
// Alternative (https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-isiconic):
Winapi.Windows.IsIconic(h);
Which of the two alternatives is preferable? Or are they equally good in all situations?
IsIconic() is the proper and documented way to check if a window is minimized:
IsIconic function
Determines whether the specified window is minimized (iconic).
Window Features
The IsZoomed and IsIconic functions determine whether a given window is maximized or minimized, respectively. The GetWindowPlacement function retrieves the minimized, maximized, and restored positions for the window, and also determines the window's show state.
Using anything else is a hack at best. The fact that IsIconic() and GetWindowPlacement() internally check the HWND for the WS_MINIMIZE window style is just an implementation detail. The overhead of using these functions rather than checking the window style manually is negligible.
Stick with IsIconic(), it is the API that Microsoft specifically provides for this exact purpose.
The 'best' alternative is calling
(GetWindowLong(hwnd, GWL_STYLE) & WS_MINIMIZE).
I checked IsIconic and GetWindowPlacement functions in dissassembler and both internally compares windows styles with WS_MINIMIZE flag to determine if window is minimized.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a problem about Delphi source code.
Please help me explain it.
Detail problem as below:
I have two Delphi applications. They are two .dll files (it call App1 and App2).
App1 has a method as below:
procedure SetImage( objControl : Object; img: Pointer);
begin
objControl.Picture.Bitmap := img;
end;
In App2, I call above method of App1 as below to display image on report.
SetImage( objPreImgs, tempJPEG );
With objPreImgs is correct object and temJPEG is TJPEGImage object.
If I compile App1 and App2 with Delphi 7, there is not any problem.
If I compile App1 with Delphi 7 and App2 with Delphi 6, there is one problem (cannot display image on report).
I have not known root cause of above problem yet.
If you know, please explain for me.
p/s: App1 cannot compile with Delphi 6.
Despite the fact that both "apps" share a common memory area, each of them:
uses its own memory manager,
have their own VMT (virtual method table)
and the object model.
Thus, when you pass a pointer to an object in another "application", it considers its own and is looking for methods in own VMT. Naturally, D6 VMT differs from D7, which leads to AV, stack overflow and other errors.
So, you can`t pass objects and classes via dll | apps.
Returning to the task: you need to pass the contents of an image in a way that does not require the use of objects, for example - using iStream or SharedMemory. Also you can pass a handle to the image, because the handle is "global" value for both dll in the unified address space of the application. But...the first procedure (SetImage) is not necessary - this action must be performed in app2.
It cant compile because it's not the correct syntax
First of all objControl : Object should be objControl : TImage
img: Pointer should be img: TBitmap
objControl.Picture.Bitmap := img; shoud be objControl.Picture.Bitmap.Assign(img);
I belive you need a beginners book
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was looking at all the examples shipped with RAD Studio xe6 when I came across the custom listbox example located at
Object Pascal > FireMonkey Desktop > CustomListBox
Trying to play with it and see what modifications I could do, I decided that it I wanted to create a for loop that would get the visible or not property for each object. The thing though is that I can't understand what the following line actually means.
107| Item.StylesData['visible.OnChange'] := TValue.From<TNotifyEvent>(DoVisibleChange); // set OnChange value
It adds an onChange event, but how exactly? What is TNotifyEvent, is that how we tell the compiler to create a new event?
Thanks.
The FireMonkey styles framework has been designed to be flexible and extensible. It is introduced at the root of the styled control hierarchy, TStyledControl. This article gives a brief introduction and explanation of the philosophy behind the design.
Because the styling framework is designed to support many different types of controls, there is a clear need for flexibility and extensibility. So you see code like this:
StylesData['visible.OnChange'] := ...
The StylesData property is an array property, indexed with a string. It is declared like this:
property StylesData[const Index: string]: TValue;
The TValue type is the modern variant type that is used throughout the RTL. So, we gain flexibility by allowing StylesData to hold any kind of object, by means of using a variant type, TValue. And we have extensibility by allowing named indices.
So, the control that you are referring to allows you to customise its behaviour when its visibility changes. It does so by checking for a style named visible.OnChange that is expected to be of type TNotifyEvent. We cannot supply the TNotifyEvent directly, we have to wrap it in a TValue. And hence the call to TValue<TNotifyEvent>.From().
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In our Delphi 2007 application we are using a lot of the following constructs
FdmBasic:=TdmBasicData(FindOwnerClass(AOwner,TdmBasicData));
The FindOwnerClass travels the Owner hierarchy of the current component upwards to find a specific class (in the example TdmBasicData). The resulting object is stored in the Field variable FdmBasic. We use this primarily to pass datamodules along.
Example:
When generating a report, the resulting data is compressed and stored in a Blob field of a table accessed through a datamodule TdmReportBaseData. In a separate module of our application, there is functionality to show the data from the report in a Paged form using ReportBuilder. The main code of this module (TdmRBReport), uses a class TRBTempdatabase to convert the compressed blob data into different tables that are usable in the Reportbuilder runtime reportdesigner.
TdmRBReport has access to TdmReportBaseData for all kinds of report-related data (type of report, report calculationsettings, etc). TRBTempDatabase is constructed in TdmRBReport but has to have access to TdmReportBasedata. So this is now done using the construction above:
constructor TRBTempDatabase.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FdmReportBaseData := TdmRBReport(FindOwnerClass(Owner, TdmRBReport)).dmReportBaseData;
end;{- .Create }
My feeling is that this means that TRBTempDatabase knows a lot of its owner, and I was wondering if this is some sort of code smell or Anti-pattern.
What are your thoughts about this? Is this a code smell? If so, what is a better way?
On the description presented here I regard this as mildly smelly. However, it seems easy to fix.
I'd be inclined to pass the dmReportBaseData object into the constructor of any component that needs it. This makes the contract clear at compile time rather than enforcing it at runtime as you currently do.
As it currently stands, the contract you enforce is stronger than it needs to be. Although TRBTempDatabase only requires a dmReportBaseData instance, it will only function if it can get that instance from a TdmRBReport report object.
Making this change would also allow TRBTempDatabase and TdmRBReport to have a divorce and still function successfully. And as #Lieven points out in the comments, this would likely make testing easier.
If all you're doing in a base class is maintaining a reference to a parent object then no, it's not code-smell, it's a perfectly legitimate use. You can explicitly design a base class to carry information about "something that might come later."
If the base class is relying on some characteristic of the derived class that isn't present in itself (i.e. the generalized class relies on a specialization of one of its children) then yeah, that might be a bit funky.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
It happens to me quite often that I call a function Foo and want to know what exceptions this function might throw. In order to find out I then look into the implementation of Foo, but that is not enough. Foo might indeed call a function Bar that raises an exception.
Sometimes I even miss Java's checked exception handling.
So it is obivous to me that it is necessary to document the exceptions each function can throw: the question is: how? Are there any best practices on how to document exceptions? How do you handle this problem?
I think this covers some part of the problem you became aware of
Cleaner, more elegant and wrong
Cleaner, more elegant and harder to recognize
Most Delphi applications are VCL applications. They do not require a checked exception, because the main message loop has a try/except block catching everything.
It can be good practice to document which exceptions can be explicitly raised by your code though.
I'd use XMLDoc for that (there are various questions on XMLDoc her on SO, and here is some documentation from Embarcadero).
Note however that underlying code can also raise exceptions. Depending on the influence you have on libraries, you can or cannot assure those are always the same. A different thing is the OS: depending on where you run, you can get different exceptions.
--jeroen
We use Javadoc style comments for documentation. We extract the info and generate the output with some simple text scripts. We have used DelphiCodeToDoc, too.
Documenting exceptions, we have mandated to use the #throws tag.
this is looking great for documenting code - Documentation Insight from DevJet.net
I use XMLDoc comments. It's basically adding a specialized type of comment to your code in the interface section, just above the property or method declarations. Here's a nonsensical (of course) example. If you add similar style comments in your code, they'll pop up in Code Insight when you invoke it while writing code, just like the VCL's documentation does.
type
{$REGION 'TMyClass description'}
/// <summary>TMyClass is a descendent of TComponent
/// which performs some function.</summary>
{$ENDREGION}
TMyClass=class(TComponent)
private
// your private stuff
FSomeProp: Boolean;
procedure SetSomeProp(Value: Boolean);
protected
// your protected stuff
public
{$REGION 'TMyClass constructor'}
/// <summary> TMyClass constructor.</summary>
/// <remarks>Creates an instance of TMyClass.</remarks>
/// <param>Owner: TObject. The owner of the instance of TMyClass</param>
/// <exception>Raises EMyObjectFailedAlloc if the constructor dies
/// </exception>
{$ENDREGION}
constructor Create(Owner: TObject); override;
published
{$REGION 'TMyClass.Someprop'}
/// <summary>Someprop property</summary>
/// <remarks>Someprop is a Boolean property. When True, the
/// thingamajig automatically frobs the widget. Changing this
/// property also affects the behavior of SomeOtherProp.</remarks>
{$ENDREGION}
property Someprop: Boolean read FSomeProp write SetSomeProp;
end;
I prefer to wrap these XMLDoc comments in regions, so they can be collapsed out of the way unless I want to edit them. I've done so above; if you don't like them, remove the lines with {$REGION } and {$ENDREGION}
I use PasDoc for documenting almost all of my Delphi projects. It includes a "raises" tag which does what you seem to be asking for.
Regards
- turino
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
can someone suggest the ideal Unit test cases that may fit in across each of the layers .
(which otherwise can be called as a standard).
for instance, in an ASP.NET MVC applictaion using a Repository pattern -
Controller - can assert for View names and format of the data returned to the views , from the controller action methods( i couldnt think of more , if u can please suggest).
Services Layer - ?? what can be written. because they in turn depend on the layers underneath.. ( can some one suggest a Unit Case with example for sevices layer)?.
One trivial question to finish off. Irrespective of the layers , the method being tested makes calls to other instance methods/static methods say,
public List<string> MethodUnderTest()
{
instance.SomeOtherMethod();
StaticMethod();
}
in each case it is neccesary to mock the methods calls by moving that to interfaces .? any thoughts on that . ( coz unit Testing by nomenclature should not depend on anything)
Can some
I recommend reading the Art of Unit Testing. It covers this stuff in detail.