zoukankan      html  css  js  c++  java
  • Using Resource File on DotNet

    [http://www.jelovic.com/articles/resources_in_visual_studio.htm]

    Using Resources in Visual Studio .NET

    by Dejan Jelovic

    Concepts

    Here's how things break down:

    In Visual Studio .NET, many resources are no longer specified using .RC files. Instead, the form editor directly manipulates your VB or C# code when you edit the forms visually..

    We still need other types of resources, though. We need graphic resources like bitmaps and icons, as well as locale-specific string resources. These are typically stored in one or more .RESX files that make up a Visual Studio project.

    In order to use resources from your code, you'll need to know how to:

    1. Create RESX files.
    2. Add bitmaps and other types of resources to the RESX files.
    3. Load these resources from your code.

    So let's get started.

    1. Create RESX Files

    Visual Studio .NET stores resources like bitmaps, icons and locale-specific strings in files with the extension .RESX. These are actually XML files in which binary resources are base64-encoded.

    When you compile a project the compiler will compile these files into binary files with the .resource extension, and then the linker will put these into the final EXE or DLL.

    Adding a new RESX file to a project is simple. Simply right-click on a project in the Solution Explorer, select Add/Add New Resource, and add a new "Assembly Resource File".

    2. Adding Resources to RESX Files

    Here's the funny thing: Visual Studio doesn't seem to have any tools for visually editing resources in a RESX file.

    Fortunately, a Lutz Roeder has created a tool named Resourcer that you can use to edit them.

    To add a bitmap to a RESX file:

    1. Run Resourcer.
    2. Open the RESX files that you have added to the Visual Studio project in Resourcer.
    3. Select Add/Add File.
    4. In the dialog that pops up, type in the file name of the bitmap file and a symbolic name by which the content of that bitmap will be known inside the RESX file.
    5. When you click OK, the content of the bitmap will be base64 encoded and placed in the RESX files under the name that you have specified.
    6. Save the RESX file by selecting File/Save.

    And you are done. BTW, you don't need to keep the bitmap file on the disk any more, as the RESX file contains its complete copy.

    3. Using Resources from Your Code

    Here's how to load the bitmap from the resources at run time:

    The first thing you should know is under which name Visual Studio has placed your RESX file into the resulting EXE.

    Each Visual Studio project has a namespace name associated with it. If you right-click on a project file in Solution Explorer, then select Properties, a dialog that pops up lets you select a "Default Namespace". Visual Studio will bundle the resources into the resulting EXE under the name made up from the default namespace and the name of the RESX file.

    Say your default namespace is MyCompany.MyProject and the RESX file is named SomeResources. Those resources will be bundled inside the resulting executable under the name MyCompany.MyProject.SomeResources.

    To access those resources from your code, you'll need to create an instance of ResourceManager:

    ResourceManager resourceManager = new ResourceManager ("MyCompany.MyProject.SomeResources", GetType ().Assembly);

    Then, to obtain bitmap, you will use the symbolic name that you have specified when you have added the bitmap to the RESX file using Resourcer:

    Bitmap image = (Bitmap)resourceManager.GetObject ("MyBitmapName");

    If you attempt to create a resource manager using a wrong name, or you specify a wrong name when calling ResourceManager.GetObject, the framework will throw theMissingManifestResourceException.

    That's it. Have fun now.

  • 相关阅读:
    Arduino Uno微控制器采用的是Atmel的ATmega328
    关于arduino与SPI
    fopen和fopen_s用法的比较
    C语言中 malloc
    补码原理——负数为什么要用补码表示
    晶振
    晶振(crystal)与谐振荡器(oscillator)
    LCD显示器缺陷自动化检测方案
    arduino 动态内存不足问题
    文档生成工具——Doxygen
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1688904.html
Copyright © 2011-2022 走看看