Results 1 to 15 of 15

Thread: Two quick questions

  1. #1
    o0Jon0o
    Guest

    Question Two quick questions

    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

  2. #2
    New Member
    Join Date
    May 2002
    Location
    For the moment the U.S., for eternity, Heaven!
    Posts
    5
    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.

    Does anyone know of a witty and entertaining signature I could use? I mean, who thinks these things up?????

  3. #3
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Use the itoa function

    here is an example:
    PHP Code:
    int y 10;
    char cString[10];
    itoa(y,cString,10); 
    The last #er is the base of the int you are trying to convert.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  4. #4
    jim mcnamara
    Guest
    Or:

    Code:
    char tm[[10];
    int mynumber;
    mynumber = 3;
    
    sprintf(tmp,"%d",mynumber);

  5. #5
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    HEHEHE I think you ment:

    Originally posted by jim mcnamara
    Or:

    Code:
    char tmp[10];
    int mynumber;
    mynumber = 3;
    
    sprintf(tmp,"%d",mynumber);
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  6. #6
    o0Jon0o
    Guest
    Thanks guys, been wanting to know that for ages


    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

  7. #7
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    I know there is more than one way to do it. Here is the only way I can remember right now.

    OutputDebugString("Output This");
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you want to convert int to string in pure C++, you can use:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    void somecode() {
        ostringstream oss;
    
        oss << 5635 << ends;
    
        cout << oss.str().length() << endl;
    }
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    o0Jon0o
    Guest
    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

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    o0Jon0o
    Guest
    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?
    Because I’m not using MFC, I’m quite new to this so am trying to learn API first.

    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 , 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

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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!
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    o0Jon0o
    Guest
    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:

    Code:
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC)MAINProc );
    And this to handle messages from it:

    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;
    }
    Any ideas what could be wrong?

    Thanks again

    Jon

  15. #15
    o0Jon0o
    Guest
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width