|
Use a wstring and call the c_str() method on it.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Would if I could, but I am given a c_str and must convert it to a wide, hence the question.
|
|
|
|
|
Stick^ wrote: Would if I could, but I am given a c_str and must convert it to a wide, hence the question.
c_str() is a method of wstring . No conversion is necessary as it is already "wide."
Now if you are unable to change std::string to std::wstring , that's a different problem.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
You are given a const char* you mean?
If that is the case, use MultiByteToWideChar to convert the const char* to an array of const wchar_t* and pass that to your draw function (NOTE: that will only work for UNICODE builds -- for a more generic approach, look at the A2T macro)
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
No.
I'm given a std::string... here's the declaration:
string m_ac;
Now, if I attempt:
m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush);
I get:
Error 1 error C2664: 'Gdiplus::Status Gdiplus::Graphics::DrawString(const WCHAR *,INT,const Gdiplus::Font *,const Gdiplus::RectF &,const Gdiplus::StringFormat *,const Gdiplus::Brush *)' : cannot convert parameter 1 from 'std::string' to 'const WCHAR *'
so as you can clearly see, the m_ac is NOT already wide.
I have tried to do something like this:
WCHAR str[20+1] = L"";
WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL);
but this does not work either. Nor does just m_ac in parameter 3.
|
|
|
|
|
Have you tried "mbstowcs" or "mbstowcs_s" in stdlib.h.
std::string mbstr = "The string!";<br />
std::vector<wchar_t> wcstr(mbstr.size() + 1, L'\0');<br />
<br />
size_t written = 0;<br />
mbstate_t state = {0};<br />
const char* pmbstr = mbstr.c_str();<br />
<br />
errno_t result = mbsrtowcs_s(&written, &wcstr[0], <br />
wcstr.size(), &pmbstr, mbstr.size(), &state);<br />
<br />
std::wcout << &wcstr[0] << std::endl;
-- modified at 20:19 Thursday 28th September, 2006
|
|
|
|
|
Stick^ wrote: WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL);
Please use MultiByteToWideChar() instead. You are converting from char to wchar_t, right?
wchar_t buf[20+1] = { 0 };
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_ac.c_str(), m_ac.size(), buf, 20); should do the trick.
--
From the Makers of Futurama
|
|
|
|
|
Thanks. Would help if I followed everyone's directions!
|
|
|
|
|
You are compiling with UNICODE defined, so DrawString is mapped to DrawStringW (which takes wide characters). Since you have a string (which are ANSI characters), you need to convert that string to a wstring (or simply to an array of wchar_t's). You'll need to muse MultiByteToWideChar for that (not WideCharToMultiByte).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Actually, the OP is using the GDI+ classes, and they tend to always accept wide strings, and not have A or W versions of functions that are aliased-to depending on the build. For example, GDI+'s three versions of the Graphics::DrawString(...) function all take a const WCHAR* as the string type.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Thanks all for your help. I'm such an idiot, using the wrong function!
I'll try all these good suggestions and do some more reading and see if I can't figure out more.
|
|
|
|
|
Hi,
I want to make an application similar to GMail drive and have no idea where to start from.
Can someone please point out the right directions ?
Thanks a lot.
Mithin
|
|
|
|
|
You're a newbie and you want to start with such an ambitious project. Why? Do you not think it would be beneficial to learn the basics first?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Yes, I want to learn the basics only. I have been developing softwares using C++/Qt for last couple of years and now I want to do some harcore windows stuff.
Gmail Drive was interesting and I have an idea too.
I would be greatful to you if you can suggest me some links that will teach me the basics and will also be helpful in my project.
Thanks a lot for your time.
Thanks
Mithin
|
|
|
|
|
See this series.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hello,
I am trying to load a bitmap into one of my dialog boxes in the program I am coding. I have found much help in this area provided that the file I am going to load is already defined as a resource. However, my problem is that the *.BMP that I have to load will be selected by the user. I was wondering if anyone could give me a little instruction on how I am able to take a Bitmap from a *.BMP whose properities I am unaware of and get it to appear in the dialog box.
Thank you,
Joe
|
|
|
|
|
take a look at ::LoadImage
|
|
|
|
|
Yes I understand that Load Image is the function I am going to be using, however, it returns a HANDLE, can I cast that as an HBITMAP and just move on?
|
|
|
|
|
yep. it's a HANDLE because you can load cursors and icons with that function, too. but, if you're using the IMAGE_BITMAP flag, you get a HANDLE to a bitmap (aka HBITMAP) back.
|
|
|
|
|
Ok but when I load a BMP from a file, it has to be a DIB? Could you possibly point out what doesn't seem to work here?
BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP hBitmap,HPALETTE hPalette )
{
BITMAP bm;
//*phBitmap = NULL;
//*phPalette = NULL;
// Use LoadImage() to get the image loaded into a DIBSection
hBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION | LR_LOADFROMFILE );
if (hBitmap == NULL )
return FALSE;
// Get the color depth of the DIBSection
GetObject(hBitmap, sizeof(BITMAP), &bm );
// If the DIBSection is 256 color or less, it has a color table
if (( bm.bmBitsPixel * bm.bmPlanes ) <= 8 )
{
HDC hMemDC;
HBITMAP hOldBitmap;
RGBQUAD rgb[256];
LPLOGPALETTE pLogPal;
WORD i;
// Create a memory DC and select the DIBSection into it
hMemDC = CreateCompatibleDC( NULL );
hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap );
// Get the DIBSection's color table
GetDIBColorTable( hMemDC, 0, 256, rgb );
// Create a palette from the color tabl
pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) );
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries = 256;
for (i = 0 ; i < 256 ; i++)
{
pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
pLogPal->palPalEntry[i].peFlags = 0;
}
hPalette = CreatePalette( pLogPal );
// Clean up
free(pLogPal);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
}
else // It has no color table, so use a halftone palette
{
HDC hRefDC;
hRefDC = GetDC( NULL );
hPalette = CreateHalftonePalette( hRefDC );
ReleaseDC( NULL, hRefDC );
}
return TRUE;
}
Thanks a bunch in advance,
Joe
|
|
|
|
|
TheDelChop wrote: Ok but when I load a BMP from a file, it has to be a DIB?
nope. you can read it to a 'normal' HBITMAP.
TheDelChop wrote: Could you possibly point out what doesn't seem to work here?
the hBitmap and hPalette parameters should probably be references or pointers, if you want to set them so the calling function can use them.
|
|
|
|
|
Assuming that the resourse IDB_BITMAP1 is stored in C:\BITMAP1.BMP, any difference on what these two functions will return?
hbm = (HBITMAP)LoadImage(NULL,"C:\BITMAP1.BMP",IMAGE_BITMAP,0,0, LR_LOADFROMFILE);
hbm = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
|
|
|
|
|
|
The returned handle is ALWAYS a DDB if you dont set the flag LR_CREATEDIBSECTION and a DIB if u do set the flag. So, if your working on 24+ bits per pixel display you dont even have to worry abou the palette and other stuff.. just load it and enjoy
Peace!
|
|
|
|
|
If you want to load file you can use LoadImage or CImage class
|
|
|
|
|