[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

How to call (Win32)FindResource in WPF app?

MichaelG

4/30/2007 2:50:00 PM

I'm building a splash screen for a .Net 3.0 WPF app. The SplashScreen class
needs to load a bitmap (.png) using Win32 functions but I'm having trouble
figuring out how to add the bitmap to my project so it is visible to
(Win32)FindResource and then determine what the ResourceID or ResourceName is
for the call.

Anyone know how to do this?
Thanks
Michael
4 Answers

wawang

5/1/2007 5:47:00 AM

0

Hi Michael,

To embed a win32 resource in .NET application, you can following these
steps:

1) Suppose we have a file "logo.png" in the project directory, create two
files "resource.h" and "SplashScreenWin32Resources.rc":

resource.h:

#define IDB_SPLASHSCREEN 101


SplashScreenWin32Resources.rc:

#include "resource.h"

IDB_SPLASHSCREEN PNG "Logo.png"


Note you don't need to include these three files in the project.

2) Open your project's properties dialog, in the Build Events tab, input
following command in the "Pre-Build event command line":

"$(DevEnvDir)..\Tools\Bin\rc.exe" /d "UNICODE"
"$(ProjectDir)SplashScreenWin32Resources.rc"



This will compile the .rc file using resource compiler everytime you build
your project; and the generated file will be SplashScreenWin32Resources.res.


3) Now switch to the first tab "Application" of the project properties
dialog, in the resource part, select "Resource File" and input
"SplashScreenWin32Resources.RES" as the file name.

4) Rebuild your project, it will not contains the win32 resource file.

5) Use following code to load the resource:

[DllImport("kernel32.dll")]
static extern IntPtr FindResource(IntPtr hModule, int lpName,
string lpType);

private void button1_Click(object sender, EventArgs e)
{
IntPtr hRes =
FindResource(Marshal.GetHINSTANCE(this.GetType().Module), 101, "PNG");
}


Hope this helps.


Sincerely,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/de....
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

MichaelG

5/2/2007 7:00:00 PM

0

Walter, that was brilliant! Just what I needed. I had a bit of trouble with
the resource compiler which kept giving 'File not found' errors so initially
I set up a fresh Win32 project and built the .RES file in that. I eventually
got it to work using the 'Pre-Build Event Commands' by adding afxres.h to
the .rc file and a couple of /I switches to rc.exe. It feels like this
should't be necessary. If you have any idea as to why RC.exe complains
without these I'd be interested.

Thanks again.
Michael

"Walter Wang [MSFT]" wrote:

> Hi Michael,
>
> To embed a win32 resource in .NET application, you can following these
> steps:
>
> 1) Suppose we have a file "logo.png" in the project directory, create two
> files "resource.h" and "SplashScreenWin32Resources.rc":
>
> resource.h:
>
> #define IDB_SPLASHSCREEN 101
>
>
> SplashScreenWin32Resources.rc:
>
> #include "resource.h"
>
> IDB_SPLASHSCREEN PNG "Logo.png"
>
>
> Note you don't need to include these three files in the project.
>
> 2) Open your project's properties dialog, in the Build Events tab, input
> following command in the "Pre-Build event command line":
>
> "$(DevEnvDir)..\Tools\Bin\rc.exe" /d "UNICODE"
> "$(ProjectDir)SplashScreenWin32Resources.rc"
>
>
>
> This will compile the .rc file using resource compiler everytime you build
> your project; and the generated file will be SplashScreenWin32Resources.res.
>
>
> 3) Now switch to the first tab "Application" of the project properties
> dialog, in the resource part, select "Resource File" and input
> "SplashScreenWin32Resources.RES" as the file name.
>
> 4) Rebuild your project, it will not contains the win32 resource file.
>
> 5) Use following code to load the resource:
>
> [DllImport("kernel32.dll")]
> static extern IntPtr FindResource(IntPtr hModule, int lpName,
> string lpType);
>
> private void button1_Click(object sender, EventArgs e)
> {
> IntPtr hRes =
> FindResource(Marshal.GetHINSTANCE(this.GetType().Module), 101, "PNG");
> }
>
>
> Hope this helps.
>
>
> Sincerely,
> Walter Wang (wawang@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default....
> ications. If you are using Outlook Express, please make sure you clear the
> check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
> promptly.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/de....
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

wawang

5/3/2007 6:21:00 AM

0

Hi Michael,

Previously I was using

"$(DevEnvDir)..\Tools\Bin\rc.exe" /d "UNICODE"
"$(ProjectDir)SplashScreenWin32Resources.rc"


The $(DevEnvDir) and $(ProjectDir) macros are exposed by VS2005, you can
see the values of them by clicking on "Edit Pre-build" and clicking on the
"Macros" button to see pre-defined macros.

Please make sure if the full path after those macros expanded exists or
not, in other words, please make sure the rc.exe and the .rc files' path
are correct.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

wawang

5/8/2007 1:59:00 AM

0

Hi Michael,

Have you seen my last reply? Please feel free to let me know if there's
anything I can help.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.