PDA

View Full Version : سوال: مشكل در ذخيره و فراخواني متن در clipboard



A_Salimi
یک شنبه 02 خرداد 1389, 20:40 عصر
من قصد دارم تا با كد نويسي يك رشته را در كليبرد ذخيره كنم و باز آن را بازيابي كنم.با جستجو در اينترنت چند نمونه كد پيدا كردم كه با آنها مشكل دارم .لطفا راهنمايي بديد.

براي ذخيره در كليبرد:


SetClipboardText(LPCTSTR lpszText, UINT uBuff)
{

HGLOBAL hgText = NULL;
LPTSTR lpszCopy = NULL;

// First open the clipboard
if(!::OpenClipboard(NULL))
return FALSE;
// Empty the clipboard, and make us the owner

if(!EmptyClipboard())
{
::CloseClipboard();
return FALSE;
}

hgText = GlobalAlloc(GMEM_DDESHARE, (uBuff+1) * sizeof(TCHAR));

if (hgText != NULL)
{
// Lock the handle and copy the text to the buffer.
lpszCopy = (TCHAR*)GlobalLock(hgText);
memcpy(lpszCopy, lpszText, uBuff * sizeof(TCHAR));
lpszCopy[uBuff] = (TCHAR) 0;
GlobalUnlock(hgText);

// Set the clipboard text
if(!SetClipboardData(CF_TEXT,hgText))
{
::CloseClipboard();
return FALSE;
}
}
::CloseClipboard();
return TRUE;

}

براي بازيابي از كليبرد:


GetClipboardText(LPTSTR buff, UINT iBuffSize)
{
HANDLE hClip;
char* pszClipboard;

if(::OpenClipboard(NULL)) // Open the clipboard so we can read its contents
{
hClip = GetClipboardData(CF_TEXT); // Get a handle to the clipboad contents
if(hClip == NULL)
{
CloseClipboard();
return FALSE;
}
pszClipboard = (char*)GlobalLock(hClip); // Point pClipboard to the clipboard contents
if(!pszClipboard)
{
GlobalUnlock(hClip);
return FALSE;
}
//LPTSTR st=new TCHAR[255];
//st=buff;
// strncpy((char*)buff,pszClipboard,iBuffSize); // Copy the contents to a local variable
buff[iBuffSize-1] = 0;
GlobalUnlock(hClip); // Release the lock on the clipboard contents
CloseClipboard(); // Close the clipboard.
}
else
{
return FALSE;
}

return TRUE;
{

لطفا توضيح بديد كه :

آيا اين كدها صحيح هستند؟
چرا من نتوانستم در يك پرو‍ژه MFC بدرستي از آنها استفاده كنم؟

A_Salimi
سه شنبه 11 خرداد 1389, 09:46 صبح
سلام مجدد .

آقا من يك مشكل اساسي دارم .كد زير كه از اين لينك (http://www.codeproject.com/KB/clipboard/archerclipboard1.aspx) گير آوردمش بدرستي كار ميكنه اما هنگامي كه ازش استفاده ميكنم فقط كاراكتر اول رشته رو در كليبرد كپي ميكنه.كد با كمي تغييرات را در زير مشاهده نماييد:


CString strData;
strData=_T("Test text");
if (OpenClipboard())
{
// Empty the Clipboard. This also has the effect
// of allowing Windows to free the memory associated
// with any data that is in the Clipboard
EmptyClipboard();

// Ok. We have the Clipboard locked and it's empty.
// Now let's allocate the global memory for our data.

// Here I'm simply using the GlobalAlloc function to
// allocate a block of data equal to the text in the
// "to clipboard" edit control plus one character for the
// terminating null character required when sending
// ANSI text to the Clipboard.
HGLOBAL hClipboardData;
hClipboardData = GlobalAlloc(GMEM_DDESHARE,strData.GetLength()+1);


// Calling GlobalLock returns to me a pointer to the
// data associated with the handle returned from GlobalAlloc
char * pchData;
pchData = (char*)GlobalLock(hClipboardData);

// At this point, all I need to do is use the standard
// C/C++‎ strcpy function to copy the data from the local
// variable to the global memory.
// LPCSTR MyText=new TCHAR[255];
strcpy(pchData,LPCSTR(strData.AllocSysString()));

// Once done, I unlock the memory - remember you don't call
// GlobalFree because Windows will free the memory
// automatically when EmptyClipboard is next called.
GlobalUnlock(hClipboardData);

// Now, set the Clipboard data by specifying that
// ANSI text is being used and passing the handle to
// the global memory.
SetClipboardData(CF_TEXT,hClipboardData);

// Finally, when finished I simply close the Clipboard
// which has the effect of unlocking it so that other
// applications can examine or modify its contents.
CloseClipboard();
}كد اصلي كه در codeproject بدرستي جواب مي دهد را در زير آپلود مي كنم.

hoax3r
سه شنبه 11 خرداد 1389, 16:27 عصر
سلام

از این خط AllocSysString رو بردارید


strcpy(pchData,LPCSTR(strData.AllocSysString()));


به این صورت، بعد برداشتن من امتحان کردم کل رشته کپی می شد، البته داخل سورسی هم که گزاشته بودین به همین شکل استفاده شده بود


strcpy(pchData, LPCSTR(strData));

A_Salimi
سه شنبه 11 خرداد 1389, 17:03 عصر
cannot convert from 'CString' to 'LPCSTR' رو واسه كامپايل ميده.

AllocSysString را خودم گذاشتم تا اين ارور رفع بشه.از روشهاي تبديل هم كه استفاده ميكنم باز هم فقط كاراكتر اول رشته رو كپي ميكنه.آخه من در استديو 2008 كار مي كنم.



همان تكه كد را لطفا در نسخه 2008 كپي كرده و اجرا نماييد.خواهيد ديد كه اين مشكل همچنان پا بر جاست.

hoax3r
سه شنبه 11 خرداد 1389, 18:20 عصر
مشکل به خاطر اینه که احتمالا پروژه شما از نوع یونیکد هست ولی داده ها از نوع اسکی تعریف شدن
یعنی قسمتی که از AllocSysString استفاده کردین یه اشاره گر به رشته یونیکد برگردانده می شود
و چون یونیکد دو بایت هست و بعد هر حرف (بایت) یه کاراکتر نول وجود داره در نتیجه در تبدیل به LPCSTR
فقط یه حرف خوانده میشود.

من یه تغییراتی روی سورس دادم، روی ویژوال 2008 تست کردم درست کار کرد


CString strData;
strData=_T("Test text");
if (OpenClipboard())
{
// Empty the Clipboard. This also has the effect
// of allowing Windows to free the memory associated
// with any data that is in the Clipboard
EmptyClipboard();

// Ok. We have the Clipboard locked and it's empty.
// Now let's allocate the global memory for our data.

// Here I'm simply using the GlobalAlloc function to
// allocate a block of data equal to the text in the
// "to clipboard" edit control plus one character for the
// terminating null character required when sending
// ANSI text to the Clipboard.
HGLOBAL hClipboardData;
hClipboardData = GlobalAlloc(GMEM_DDESHARE,(strData.GetLength()+1) * sizeof(TCHAR));


// Calling GlobalLock returns to me a pointer to the
// data associated with the handle returned from GlobalAlloc
TCHAR *pchData;
pchData = (TCHAR *)GlobalLock(hClipboardData);

// At this point, all I need to do is use the standard
// C/C++‎‎‎ strcpy function to copy the data from the local
// variable to the global memory.
// LPCSTR MyText=new TCHAR[255];
lstrcpy(pchData,LPCTSTR(strData));

// Once done, I unlock the memory - remember you don't call
// GlobalFree because Windows will free the memory
// automatically when EmptyClipboard is next called.
GlobalUnlock(hClipboardData);

// Now, set the Clipboard data by specifying that
// ANSI text is being used and passing the handle to
// the global memory.
SetClipboardData(CF_UNICODETEXT,hClipboardData);

// Finally, when finished I simply close the Clipboard
// which has the effect of unlocking it so that other
// applications can examine or modify its contents.
CloseClipboard();
}
}