How to load PNG file into my custom component? Cannot get correct instance











up vote
0
down vote

favorite












I want to create a custom control (from TPanel) that holds some TImages.
I want to display PNG (with transparency) into those mages. Therefore, I am TRYING to attach the PNG via IDE's "Resource and Images" to the package.



The problem is that when I put the component into a test application it will fail on MyPng->LoadFromResourceName line with "resource not found". Interestingly, if I add the PNG as a resource to the test application, it will work.



This means that the component is looking into the wrong module for the PNG resource.

I print the instance with ShowMessage it shows indeed "ComponentTester.exe".



__fastcall TVolumeCtrl::TVolumeCtrl(TComponent* Owner)
: TPanel(Owner)
{
HINST h = FindClassHInstance(__classid(TVolumeCtrl));
ShowMessage(GetModuleName(h));

TPngImage *Png3 = new TPngImage();
MyPng->LoadFromResourceName(h, "Btn1");


How to get the correct instance?





Note: The PNG files ARE compiled into the RES file generated. I looked inside with a Hex viewer.










share|improve this question
























  • @DavidHeffernan - FindClassHInstance returns "ComponentTester.exe". ComponentTester does not contains the resource.
    – Rigel
    Nov 8 at 10:18












  • note: I don't drop the TVolumeCtrl component from the palette into ComponentTester's form. I create TVolumeCtrl at runtime: new TVolumeCtrl(this). But I don't think this is relevant to the problem.
    – Rigel
    Nov 8 at 10:20

















up vote
0
down vote

favorite












I want to create a custom control (from TPanel) that holds some TImages.
I want to display PNG (with transparency) into those mages. Therefore, I am TRYING to attach the PNG via IDE's "Resource and Images" to the package.



The problem is that when I put the component into a test application it will fail on MyPng->LoadFromResourceName line with "resource not found". Interestingly, if I add the PNG as a resource to the test application, it will work.



This means that the component is looking into the wrong module for the PNG resource.

I print the instance with ShowMessage it shows indeed "ComponentTester.exe".



__fastcall TVolumeCtrl::TVolumeCtrl(TComponent* Owner)
: TPanel(Owner)
{
HINST h = FindClassHInstance(__classid(TVolumeCtrl));
ShowMessage(GetModuleName(h));

TPngImage *Png3 = new TPngImage();
MyPng->LoadFromResourceName(h, "Btn1");


How to get the correct instance?





Note: The PNG files ARE compiled into the RES file generated. I looked inside with a Hex viewer.










share|improve this question
























  • @DavidHeffernan - FindClassHInstance returns "ComponentTester.exe". ComponentTester does not contains the resource.
    – Rigel
    Nov 8 at 10:18












  • note: I don't drop the TVolumeCtrl component from the palette into ComponentTester's form. I create TVolumeCtrl at runtime: new TVolumeCtrl(this). But I don't think this is relevant to the problem.
    – Rigel
    Nov 8 at 10:20















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to create a custom control (from TPanel) that holds some TImages.
I want to display PNG (with transparency) into those mages. Therefore, I am TRYING to attach the PNG via IDE's "Resource and Images" to the package.



The problem is that when I put the component into a test application it will fail on MyPng->LoadFromResourceName line with "resource not found". Interestingly, if I add the PNG as a resource to the test application, it will work.



This means that the component is looking into the wrong module for the PNG resource.

I print the instance with ShowMessage it shows indeed "ComponentTester.exe".



__fastcall TVolumeCtrl::TVolumeCtrl(TComponent* Owner)
: TPanel(Owner)
{
HINST h = FindClassHInstance(__classid(TVolumeCtrl));
ShowMessage(GetModuleName(h));

TPngImage *Png3 = new TPngImage();
MyPng->LoadFromResourceName(h, "Btn1");


How to get the correct instance?





Note: The PNG files ARE compiled into the RES file generated. I looked inside with a Hex viewer.










share|improve this question















I want to create a custom control (from TPanel) that holds some TImages.
I want to display PNG (with transparency) into those mages. Therefore, I am TRYING to attach the PNG via IDE's "Resource and Images" to the package.



The problem is that when I put the component into a test application it will fail on MyPng->LoadFromResourceName line with "resource not found". Interestingly, if I add the PNG as a resource to the test application, it will work.



This means that the component is looking into the wrong module for the PNG resource.

I print the instance with ShowMessage it shows indeed "ComponentTester.exe".



__fastcall TVolumeCtrl::TVolumeCtrl(TComponent* Owner)
: TPanel(Owner)
{
HINST h = FindClassHInstance(__classid(TVolumeCtrl));
ShowMessage(GetModuleName(h));

TPngImage *Png3 = new TPngImage();
MyPng->LoadFromResourceName(h, "Btn1");


How to get the correct instance?





Note: The PNG files ARE compiled into the RES file generated. I looked inside with a Hex viewer.







c++ c++builder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 12:43

























asked Nov 8 at 9:49









Rigel

9,71714110217




9,71714110217












  • @DavidHeffernan - FindClassHInstance returns "ComponentTester.exe". ComponentTester does not contains the resource.
    – Rigel
    Nov 8 at 10:18












  • note: I don't drop the TVolumeCtrl component from the palette into ComponentTester's form. I create TVolumeCtrl at runtime: new TVolumeCtrl(this). But I don't think this is relevant to the problem.
    – Rigel
    Nov 8 at 10:20




















  • @DavidHeffernan - FindClassHInstance returns "ComponentTester.exe". ComponentTester does not contains the resource.
    – Rigel
    Nov 8 at 10:18












  • note: I don't drop the TVolumeCtrl component from the palette into ComponentTester's form. I create TVolumeCtrl at runtime: new TVolumeCtrl(this). But I don't think this is relevant to the problem.
    – Rigel
    Nov 8 at 10:20


















@DavidHeffernan - FindClassHInstance returns "ComponentTester.exe". ComponentTester does not contains the resource.
– Rigel
Nov 8 at 10:18






@DavidHeffernan - FindClassHInstance returns "ComponentTester.exe". ComponentTester does not contains the resource.
– Rigel
Nov 8 at 10:18














note: I don't drop the TVolumeCtrl component from the palette into ComponentTester's form. I create TVolumeCtrl at runtime: new TVolumeCtrl(this). But I don't think this is relevant to the problem.
– Rigel
Nov 8 at 10:20






note: I don't drop the TVolumeCtrl component from the palette into ComponentTester's form. I create TVolumeCtrl at runtime: new TVolumeCtrl(this). But I don't think this is relevant to the problem.
– Rigel
Nov 8 at 10:20














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The only explanation that makes sense is that you are not using runtime packages. So you aren't loading the module that contains the resource.



The right way to link the resource for the component is to use a $R directive in the source file that declares the type, TVolumeCtrl in this case. That way the resource will be linked to whichever module contains the implementation of TVolumeCtrl. That's going to be a package when you are compiling the runtime package (which is used at designtime by your designtime package), and it will be the executable when you compile an executable that does not use runtime packages.






share|improve this answer





















  • In my tester app, the "link with runtime packages" is checked. And in "packages" in "design packages" the BPL that contains my component is checked also.
    – Rigel
    Nov 8 at 10:28










  • I let the IDE do all those resource compiling and linking. But I will try to link it also manually with $R as you say. Maybe there is a bug in the IDE's "Resources and images"?
    – Rigel
    Nov 8 at 10:30










  • I looked with Process Viewer into the tester app while it was running. Indeed it does not load my BPL! So, the project is set to use the BPL as runtime package but it doesn't.
    – Rigel
    Nov 8 at 10:34












  • There is no bug with the IDE's resources and images feature. It's just the wrong way to do what you are trying to do. Think about it. You've linked the resource to the package, but then you aren't using the package. If you were using the package then FindClassHInstance would return the module representing the package. Clearly you are linking the package into your executable. Even if you intended to link using runtime packages, your code should support both modes of operation: namely runtime packages and static linking.
    – David Heffernan
    Nov 8 at 10:52










  • You are right. The exe was not using the runtime package. It seems that it is not enough to add the BPI package into the "Runtime packages" list. I also needed to add the BPI file to the project (in Proj manager).
    – Rigel
    Nov 8 at 11:00













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205173%2fhow-to-load-png-file-into-my-custom-component-cannot-get-correct-instance%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The only explanation that makes sense is that you are not using runtime packages. So you aren't loading the module that contains the resource.



The right way to link the resource for the component is to use a $R directive in the source file that declares the type, TVolumeCtrl in this case. That way the resource will be linked to whichever module contains the implementation of TVolumeCtrl. That's going to be a package when you are compiling the runtime package (which is used at designtime by your designtime package), and it will be the executable when you compile an executable that does not use runtime packages.






share|improve this answer





















  • In my tester app, the "link with runtime packages" is checked. And in "packages" in "design packages" the BPL that contains my component is checked also.
    – Rigel
    Nov 8 at 10:28










  • I let the IDE do all those resource compiling and linking. But I will try to link it also manually with $R as you say. Maybe there is a bug in the IDE's "Resources and images"?
    – Rigel
    Nov 8 at 10:30










  • I looked with Process Viewer into the tester app while it was running. Indeed it does not load my BPL! So, the project is set to use the BPL as runtime package but it doesn't.
    – Rigel
    Nov 8 at 10:34












  • There is no bug with the IDE's resources and images feature. It's just the wrong way to do what you are trying to do. Think about it. You've linked the resource to the package, but then you aren't using the package. If you were using the package then FindClassHInstance would return the module representing the package. Clearly you are linking the package into your executable. Even if you intended to link using runtime packages, your code should support both modes of operation: namely runtime packages and static linking.
    – David Heffernan
    Nov 8 at 10:52










  • You are right. The exe was not using the runtime package. It seems that it is not enough to add the BPI package into the "Runtime packages" list. I also needed to add the BPI file to the project (in Proj manager).
    – Rigel
    Nov 8 at 11:00

















up vote
1
down vote



accepted










The only explanation that makes sense is that you are not using runtime packages. So you aren't loading the module that contains the resource.



The right way to link the resource for the component is to use a $R directive in the source file that declares the type, TVolumeCtrl in this case. That way the resource will be linked to whichever module contains the implementation of TVolumeCtrl. That's going to be a package when you are compiling the runtime package (which is used at designtime by your designtime package), and it will be the executable when you compile an executable that does not use runtime packages.






share|improve this answer





















  • In my tester app, the "link with runtime packages" is checked. And in "packages" in "design packages" the BPL that contains my component is checked also.
    – Rigel
    Nov 8 at 10:28










  • I let the IDE do all those resource compiling and linking. But I will try to link it also manually with $R as you say. Maybe there is a bug in the IDE's "Resources and images"?
    – Rigel
    Nov 8 at 10:30










  • I looked with Process Viewer into the tester app while it was running. Indeed it does not load my BPL! So, the project is set to use the BPL as runtime package but it doesn't.
    – Rigel
    Nov 8 at 10:34












  • There is no bug with the IDE's resources and images feature. It's just the wrong way to do what you are trying to do. Think about it. You've linked the resource to the package, but then you aren't using the package. If you were using the package then FindClassHInstance would return the module representing the package. Clearly you are linking the package into your executable. Even if you intended to link using runtime packages, your code should support both modes of operation: namely runtime packages and static linking.
    – David Heffernan
    Nov 8 at 10:52










  • You are right. The exe was not using the runtime package. It seems that it is not enough to add the BPI package into the "Runtime packages" list. I also needed to add the BPI file to the project (in Proj manager).
    – Rigel
    Nov 8 at 11:00















up vote
1
down vote



accepted







up vote
1
down vote



accepted






The only explanation that makes sense is that you are not using runtime packages. So you aren't loading the module that contains the resource.



The right way to link the resource for the component is to use a $R directive in the source file that declares the type, TVolumeCtrl in this case. That way the resource will be linked to whichever module contains the implementation of TVolumeCtrl. That's going to be a package when you are compiling the runtime package (which is used at designtime by your designtime package), and it will be the executable when you compile an executable that does not use runtime packages.






share|improve this answer












The only explanation that makes sense is that you are not using runtime packages. So you aren't loading the module that contains the resource.



The right way to link the resource for the component is to use a $R directive in the source file that declares the type, TVolumeCtrl in this case. That way the resource will be linked to whichever module contains the implementation of TVolumeCtrl. That's going to be a package when you are compiling the runtime package (which is used at designtime by your designtime package), and it will be the executable when you compile an executable that does not use runtime packages.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 10:28









David Heffernan

511k338031195




511k338031195












  • In my tester app, the "link with runtime packages" is checked. And in "packages" in "design packages" the BPL that contains my component is checked also.
    – Rigel
    Nov 8 at 10:28










  • I let the IDE do all those resource compiling and linking. But I will try to link it also manually with $R as you say. Maybe there is a bug in the IDE's "Resources and images"?
    – Rigel
    Nov 8 at 10:30










  • I looked with Process Viewer into the tester app while it was running. Indeed it does not load my BPL! So, the project is set to use the BPL as runtime package but it doesn't.
    – Rigel
    Nov 8 at 10:34












  • There is no bug with the IDE's resources and images feature. It's just the wrong way to do what you are trying to do. Think about it. You've linked the resource to the package, but then you aren't using the package. If you were using the package then FindClassHInstance would return the module representing the package. Clearly you are linking the package into your executable. Even if you intended to link using runtime packages, your code should support both modes of operation: namely runtime packages and static linking.
    – David Heffernan
    Nov 8 at 10:52










  • You are right. The exe was not using the runtime package. It seems that it is not enough to add the BPI package into the "Runtime packages" list. I also needed to add the BPI file to the project (in Proj manager).
    – Rigel
    Nov 8 at 11:00




















  • In my tester app, the "link with runtime packages" is checked. And in "packages" in "design packages" the BPL that contains my component is checked also.
    – Rigel
    Nov 8 at 10:28










  • I let the IDE do all those resource compiling and linking. But I will try to link it also manually with $R as you say. Maybe there is a bug in the IDE's "Resources and images"?
    – Rigel
    Nov 8 at 10:30










  • I looked with Process Viewer into the tester app while it was running. Indeed it does not load my BPL! So, the project is set to use the BPL as runtime package but it doesn't.
    – Rigel
    Nov 8 at 10:34












  • There is no bug with the IDE's resources and images feature. It's just the wrong way to do what you are trying to do. Think about it. You've linked the resource to the package, but then you aren't using the package. If you were using the package then FindClassHInstance would return the module representing the package. Clearly you are linking the package into your executable. Even if you intended to link using runtime packages, your code should support both modes of operation: namely runtime packages and static linking.
    – David Heffernan
    Nov 8 at 10:52










  • You are right. The exe was not using the runtime package. It seems that it is not enough to add the BPI package into the "Runtime packages" list. I also needed to add the BPI file to the project (in Proj manager).
    – Rigel
    Nov 8 at 11:00


















In my tester app, the "link with runtime packages" is checked. And in "packages" in "design packages" the BPL that contains my component is checked also.
– Rigel
Nov 8 at 10:28




In my tester app, the "link with runtime packages" is checked. And in "packages" in "design packages" the BPL that contains my component is checked also.
– Rigel
Nov 8 at 10:28












I let the IDE do all those resource compiling and linking. But I will try to link it also manually with $R as you say. Maybe there is a bug in the IDE's "Resources and images"?
– Rigel
Nov 8 at 10:30




I let the IDE do all those resource compiling and linking. But I will try to link it also manually with $R as you say. Maybe there is a bug in the IDE's "Resources and images"?
– Rigel
Nov 8 at 10:30












I looked with Process Viewer into the tester app while it was running. Indeed it does not load my BPL! So, the project is set to use the BPL as runtime package but it doesn't.
– Rigel
Nov 8 at 10:34






I looked with Process Viewer into the tester app while it was running. Indeed it does not load my BPL! So, the project is set to use the BPL as runtime package but it doesn't.
– Rigel
Nov 8 at 10:34














There is no bug with the IDE's resources and images feature. It's just the wrong way to do what you are trying to do. Think about it. You've linked the resource to the package, but then you aren't using the package. If you were using the package then FindClassHInstance would return the module representing the package. Clearly you are linking the package into your executable. Even if you intended to link using runtime packages, your code should support both modes of operation: namely runtime packages and static linking.
– David Heffernan
Nov 8 at 10:52




There is no bug with the IDE's resources and images feature. It's just the wrong way to do what you are trying to do. Think about it. You've linked the resource to the package, but then you aren't using the package. If you were using the package then FindClassHInstance would return the module representing the package. Clearly you are linking the package into your executable. Even if you intended to link using runtime packages, your code should support both modes of operation: namely runtime packages and static linking.
– David Heffernan
Nov 8 at 10:52












You are right. The exe was not using the runtime package. It seems that it is not enough to add the BPI package into the "Runtime packages" list. I also needed to add the BPI file to the project (in Proj manager).
– Rigel
Nov 8 at 11:00






You are right. The exe was not using the runtime package. It seems that it is not enough to add the BPI package into the "Runtime packages" list. I also needed to add the BPI file to the project (in Proj manager).
– Rigel
Nov 8 at 11:00




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205173%2fhow-to-load-png-file-into-my-custom-component-cannot-get-correct-instance%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff