Superficial use of GDI + programming and conversion between char, cstring, and wchar

Source: Internet
Author: User

 

1. Create a development environment for VC and VS with GDI +

First, the header file and library file of the library are downloaded, and then the library and header file are added in the VC project configuration. The specific operation is as follows:

The operations below vc6.0:

Enter gdiplus in the project-> Settings-> link-> Object/Library module. lib. this static library file should be copied to the project folder, otherwise it should be filled in the full path.

In stdafx. H, I added the following code:

# Ifndef ulong_ptr
# Define ulong_ptr unsigned long *
# Include "./gdiplus/DES/gdiplus. H"
Using namespace gdiplus;

# Include "./gdiplus/DES/gdiplus. H" this is because the decompressed path is in the project folder. Someone copied the include folder content to the include folder in the installation file of VC.

The configuration below vs2005 is slightly different from the above. The macro ulong_ptr is not specified. The rest are just different interfaces.

Next, add the member variable in the project application class (C * app class:

Ulong_ptr m_gdiplustoken;

Reload cwinapp member functions:

Virtual int exitinstance ();

Add the statement to load gdiplus in C * app: initinstance ()

Gdiplus: gdiplusstartupinput;
Gdiplus: gdiplusstartup (& m_gdiplustoken, & gdiplusstartupinput, null );

Load the following in C * app: exitinstance:

Gdiplus: gdiplusshutdown (m_gdiplustoken );
Return cwinapp: exitinstance ();

In this way, you can use the corresponding gdiplus class in the view or fram class.

2. How to plot an image (common formats)

The method I learned on the internet is roughly as follows:

First define a bitmap or image object

For example, image IMG (L "example.jpg ");

Then a static function fromimage () of image or bitmap is used ();

Obtain a graphics object pointer. Graphics is a class with graphic interfaces. It supports common operations such as painting points, lines, rectangles, ovans, strings, and fills. Obviously, the so-called DC shown here should be the memory DC, that is, the data zone memory of the image file. Therefore, the IMG object calls the SAVE Function after creating the image.

IMG. Save (...); after opening the saved image, you can see the effect of the graph.

Another question is how to copy a small image to a large image, or splice several images into a large one. It should be that the resources on the Internet are quite rich, but people should apply what they have learned. Learning to understand and use it effectively is the best way to learn online. My general operations on this problem are as follows:

Create an object for a large image and specify its width and height: bitmap bigbmp (width, height );

Get a small image object: bitmap smallbmp (L "resource.jpg ");

Graphics * is obtained from the drawing method on the image summarized above, and corresponding strings are drawn in smallbmp.

Similarly, a graphics * directed to bigbmp is obtained. It is OK to draw the smallbmp image after the image is completed to the specified position. That is:

Bitmap: fromimage (bigbmp)-> drawimage (& smallbmp, x, y );

Iii. Conversion between char *, cstring, and wchar *

If you want to pass a string as a parameter to all interface functions of the GDI + class, it seems that the Unicode string is used, that is, wchar *. I was also confused at the beginning, because Window Programming is often used as cstring, and reading file data with IO streams produces char *. Thanks to the summary from the Internet, I use the following basic methods to achieve the conversion between the three:

Char * To wchar *:

: Multibytetowidechar (cp_acp, 0, (const char *) res, int count, char * DEST, int count );

Similarly, wchar * is converted to char *:

Widechartomultibyte (cp_acp, 0 ,.........);

Cstring to wchar *:

Wchar_t * P = Str. allocsysstrinig ()

There are also a2w (STR) files, but they must include the ATL conversion header file # include;

Uses_conversion macro is used before a2w.

Others:

Char * To cstring:

In addition to direct value assignment, you can also use cstring: format.

For example, char * P = "sfdasf ";

Cstring STR = P; or str. Format ("% s", P );

Convert cstring to char *

1. Direct forced type conversion:

Cstring Ss = "sfasf ";
Char * P = (lpstr) (lpcstr) ss;

2.Cstring: getbuffer or lockbuffer

Char * P = Str. getbuffer ();

Char * PT = Str. lockbuffer ();

Wchar * To cstring

No relevant documents are found on the Internet. You can assign values directly.

However, the experiment found that although there was no compilation error, it was normal to use Chinese Characters in case of garbled characters and letters, it is only because garbled characters are displayed in MessageBox. For example, DBCS is used to display Chinese characters (it is purely a guess ). In general, in Windows Programming: # define Unicode

The cstring. tchar, and so on all use Unicode codes. One character occupies two bytes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.