1) How do you convert an int into a string?
2) How do copy the contents of an editbox to the clipboard including the font and size?
Thanks
-Jon
Printable View
1) How do you convert an int into a string?
2) How do copy the contents of an editbox to the clipboard including the font and size?
Thanks
-Jon
I'm not that great at programming but I figure I might be able to help. Just don't believe everything you read from me.
I am assuming you are using C++?
As I thought about it, I came up with a wacky solution that 'might' work?
First you need to figure out how many numbers are in your int.
ex.
495 has 3 numbers.
then you would take your number and storeing it as something else, would devide it by 10 to the exponent of however many numbers the int has -1.
ex.
495/(10^(3-1)) = 4.95
stored as an int, the .95 is dropped and you have the number four. Then you would have 9 compare functions and when your 1 digit int hits its match, you would put into the string the first character. For the 9 compare functions you might be able to use a case-switch statment, I'm not sure.
Then you would need to design a loop that would go through it all again this time acting on the next int number,
ex.
4'9'5 -> 9.
All of this is a theory, I haven't tried it out, don't know if it would work, and bet that there is probebly a 2-line peice of code somewhere else that would solve your problem.
As for your other question, I haven't the foggiest idea.
:confused:
Use the itoa function
here is an example:
The last #er is the base of the int you are trying to convert.PHP Code:int y = 10;
char cString[10];
itoa(y,cString,10);
Or:
Code:char tm[[10];
int mynumber;
mynumber = 3;
sprintf(tmp,"%d",mynumber);
HEHEHE I think you ment:
Quote:
Originally posted by jim mcnamara
Or:
Code:char tmp[10];
int mynumber;
mynumber = 3;
sprintf(tmp,"%d",mynumber);
Thanks guys, been wanting to know that for ages:D
Talon_Karrde_YL: Nice Idea mate, took me a few reads to figure out what you were trying to do. But now I finally understand it, I can't see why it wouldn't work.
O' one more question if that’s okay: How do you/can you print to the debug windows in VC++ 6.
Thanks again
Jon
I know there is more than one way to do it. Here is the only way I can remember right now.
OutputDebugString("Output This");
If you want to convert int to string in pure C++, you can use:That's a little slower for conversion of a single string, but if you want to create an entire string it's a lot more stable (and possibly faster depending on your libraries) than using sprintf - for one thing, you can't explode the array boundaries :DCode:#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void somecode() {
ostringstream oss;
oss << 5635 << ends;
cout << oss.str().length() << endl;
}
I was going to suggest that parksie :)
Actually itoa is not part of the C standard (this is why you will only find _itoa in the VC++ help). The standard C way is sprintf.
talon: the usual approach is the reverse. Instead of figuring out how many digits a number has, any itoa function uses modulo to get the least significant digit ( % 10 ) and writes it to memory. Then it divides the whole number by 10 (essentially shifting all digits right one position) and repeats. The function finishes by reversing the string.
This has the advantage of not having to figure out the number of digits (mainly a waste of time) and not having to do any powers (which is damn slow, or it requires you to write a function specialized on integers, but it would still be slow).
As for the other question:
RichEditBoxes can do that automatically. You only need to send them messages like WM_COPY, WM_CUT, WM_PASTE etc.
If you have a normal edit control and you want to pass font info along, you need to retrieve the current selection (WM_GETSEL, WM_GETTEXT), the current style (you have probably stored it somewhere) and format it to rich text clipboard data. Then you can use the usual clipboard functions to pass the data.
Sorry about all these questions but when I make a call to AfxInitRichEdit() to use a rich edit box, i get the error:
error C2065: 'AfxInitRichEdit' : undeclared identifier
Dose anyone know where this function is defined or how to use it?
Cheers
Why don't people say in advance that they're using MFC?
AfxInitRichEdit() is defined in afxwin.h. Why don't you use the CRichEditCtrl class?
Because I’m not using MFC, I’m quite new to this so am trying to learn API first.Quote:
Originally posted by CornedBee
Why don't people say in advance that they're using MFC?
AfxInitRichEdit() is defined in afxwin.h. Why don't you use the CRichEditCtrl class?
I was under the impression that you could use all windows controls with pure API. Is this the case?
I tried including afxwin.h but it came up with this error:
#error : WINDOWS.H already included. MFC apps must not #include <windows.h>
I got rid of windows.h and got another 102 errors:eek: , I take it that header's just for MFC?
Thanks for your patience
P.S I'm thinking of loading riched20.dll and looking for AfxInitRichEdit() in there. Any other suggestions would be very welcome.
Cheers
Yep, you may not use any MFC global functions (those starting with Afx) outside an MFC app. Neither may you include any MFC header (they also start with afx).
You can use every control with pure API, though it is a little easier with MFC (sometimes a lot easier). But you're absolutly right if you want to learn API first.
You don't need the AfxInitRichEdit function. It is only used to prepare MFC internal state for rich edit controls. BTW, it loads riched32.dll. You can load riched20.dll by doing this:
Code:// global var
HINSTANCE g_hInstRichEdit = NULL;
// during init:
g_hInstRichEdit = LoadLibrary(TEXT("RICHED20.DLL"));
if(g_hInstRichEdit == NULL)
{
// critical initialization error: could not load required library!
}
O' right, that’s really handy to know, probably why AfxMessageBox() didn't work earlier.
The reason I kept trying to call AfxInitRichEdit() is because every time I place a Rich Edit Box on my form the
DialogBox() function fails. I keep getting error code: '1407 - Cannot find window class' but it can find my
windows class cuz the form works fine with editboxes.
My program (A barcode producer) is based on winprog.org's examples. I use this code to display the dialog:
And this to handle messages from it:Code:DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC)MAINProc );
Any ideas what could be wrong?Code:BOOL CALLBACK MAINProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
SetDefaultStyle(hwnd);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_PRODUCE:{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_BARNUM));
if(len == 13)
{
char* buf;
buf = (char*)GlobalAlloc(GPTR, len+1);
GetDlgItemText(hwnd, IDC_BARNUM, buf, len+1);
buf[13] = '\0';
if(!SetDlgItemText(hwnd, IDC_BARCODE, BarGen(buf)))
MessageBox(NULL, "SetDlgText() error", "Registration Failed", MB_ICONEXCLAMATION | MB_OK);
SetDlgItemText(hwnd, IDC_STAT, "Barcode produced");
GlobalFree((HANDLE)buf);
}
else
SetDlgItemText(hwnd, IDC_STAT, "Please enter a 13 digit number");
}
}
break;
default:
return FALSE;
}
return TRUE;
}
Thanks again:D
Jon
Ok, I’ve finally got it sorted. You need to load riched32.dll instead of riched20 dissipate the SDK saying it will work.
Thanks for all your help
Jon