Sign in to follow this  
gretty

Combine 2 char arrays

Recommended Posts

Hi I am trying to figure out how to combine 2 char arrays. I know I could easily combine 2 strings but I'm pretty sure I cant use strings for what I am trying to do. I am trying to convert 2 integers(x,y) to characters(char arrays) so I can display them in a static control(SetWindowText()). I can convert each x,y value to a char array but I dont know how to combine the 2 char arrays to send them to the static control to display psuedo code/function:

void displayPosition(HWND hwnd, UINT msg, POINT focusPnt)
{
  // Post: Displays the x,y coordinate where the mouse is in static control

  char x[MAX_PATH];
  char y[MAX_PATH];
  char pnt[MAX_PATH];
  itoa(focusPnt.x,x,10);
  itoa(focusPnt.y,y,10);

  // how can I combine x & y so I can display the result in the static control
  
  SendDlgItemMessage(hwnd,msg,WM_SETTEXT,0,(LPARAM)pnt);  // set control text
}

Share this post


Link to post
Share on other sites
Quote:
Original post by gretty
Hi

I am trying to figure out how to combine 2 char arrays. I know I could easily combine 2 strings but I'm pretty sure I cant use strings for what I am trying to do.

I am trying to convert 2 integers(x,y) to characters(char arrays) so I can display them in a static control(SetWindowText()). I can convert each x,y value to a char array but I dont know how to combine the 2 char arrays to send them to the static control to display

psuedo code/function:
*** Source Snippet Removed ***


Of course you can use strings for this [smile]

// don't forget
#include <sstream>

void displayPosition(HWND hwnd, UINT msg, POINT focusPnt)
{
// Post: Displays the x,y coordinate where the mouse is in static control

std::stringstream ss;
ss << focusPnt.x << ", " << focusPnt.y;
// ss now contains something like "4, 2" (no quotes of course)

// ss.str() gives us an std::string
// calling c_str() on that string gives us a null-terminated C-style string
SendDlgItemMessage(hwnd,msg,WM_SETTEXT,0,(LPARAM)ss.str().c_str()); // set control text
}




You just have to make sure SendDlgItemMessage() expects a null-terminated C-style string.

Share this post


Link to post
Share on other sites
(Assuming C)

sprintf

void displayPosition(HWND hwnd, UINT msg, POINT focusPnt)
{
// Post: Displays the x,y coordinate where the mouse is in static control
char buf[MAX_PATH];
sprintf( buf, "(%i, %i)", focusPnt.x, focusPnt.y );

SendDlgItemMessage(hwnd,msg,WM_SETTEXT,0,(LPARAM)buf); // set control text
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this