Wednesday, October 20, 2010

Modify the pasting text of edit control in the clipboard

The steps are:
1. Inherit CEdit. For example CMyEdit

2. Handle WM_PASTE message
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
ON_MESSAGE(WM_PASTE, OnEditPaste)
END_MESSAGE_MAP()

3. OnEditPaste() method
- Open the clip board
- Read the clip board text
- Modify the text
- Write the text in the clip board

LRESULT CMyEdit::OnEditPaste(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
#ifdef UNICODE
UINT nFlag = CF_UNICODETEXT;
#else
UINT nFlag = CF_TEXT;
#endif

if(!IsClipboardFormatAvailable(nFlag))
return 1;

if(!OpenClipboard())
return 1;

HGLOBAL hData = GetClipboardData(nFlag);
if(hData != NULL)
{
CString strText;
LPTSTR lptstr = (LPTSTR)GlobalLock(hData);
if(lptstr != NULL)
{
strText = lptstr;
// Modify the text
}
GlobalUnlock(hData);

EmptyClipboard();

HGLOBAL buff = GlobalAlloc(GMEM_DDESHARE, (strText.GetLength() + 1) * sizeof(TCHAR));
if(buff != NULL)
{
lptstr = (LPTSTR)GlobalLock(buff);
_tcscpy(lptstr, strText.GetBuffer());
strText.ReleaseBuffer();
GlobalUnlock(buff);
SetClipboardData(nFlag, buff);
}

}
CloseClipboard();

Default();

return 1;
}

Wednesday, July 7, 2010

Unicode file encryption/decryption

Here is an overview of how we will encrypt/decrypt an unicode file


Encryption:

1. Open the file as binary mode and read the content of file to a byte array.

2. Encrypt the byte array.

3. Write the byte array to a file.

Decryption:

1. Open the encrypted file as binary mode and read content of the file to a byte array.

2. Decrypt the byte array.

3. Write the byte array to a file.


The encryption and decryption functions are:


void EncryptBytes
(
unsigned char* pByte, // Bytes passed in, encrypted bytes passed out.
int iSize, // Size of the byte array
int* pKey // Encrypting key. (in/out)

)

{
int ii = 0;
unsigned char iNew = 0;
for(ii = 0; ii < iSize; ii++)
{
// Encrypt the character.
iNew = pByte[ii] ^ ((*pKey >> 8));
*pKey = ((iNew + *pKey) & 0xFFFF)*EncryptConst1 - EncryptConst2;
pByte[ii] = iNew;
}
}

void DecryptBytes
(
unsigned char* pByte, // Bytes passed in, decrypted bytes passed out.
int iSize, // Size of the byte array
int* pKey // Encrypting key. (in/out)

)

{
int ii = 0;
unsigned char iNew = 0, iVal;

for(ii = 0; ii < iSize; ii++)
{
iVal = pByte[ii];
iNew = iVal ^ ((*pKey >> 8));

*pKey = ((iVal + *pKey) & 0xFFFF)*EncryptConst1 - EncryptConst2;
pByte[ii] = iNew;
}
}

Monday, June 21, 2010

The goldmine for mfc, atl, java

http://mfcgoldmine.uuuq.com/Convert/index.html

Monday, June 14, 2010

Syntax coloring text editor

www.scintilla.org

Thursday, June 10, 2010

Sample Batch file

::
::
::
@setlocal
@echo on
::
set DIRNAME=%~dp0
::
::
echo Creating Shortcut

:: To Show all param just echo %*

echo %DIRNAME%VisualHost_CreateShortcut.bat %*
::

PAUSE
::
::
endlocal


@echo off
echo -----------------------------------
echo Shortcut Name : %1
echo Shortcut Path : %2
echo Icon Path : %3
echo Launcher File : %4
echo ActiveConfig : %5
echo ActiveConfig Name : %6
echo ActiveContext : %7
echo ActiveContext Name: %8
echo -----------------------------------
set /p name= What is your name?

Wednesday, June 9, 2010

Compact the text to fit in a rectangle

void CompactText(CFont* pFont, CRect rcText, CString& strText)
{
CRect rcClient;
CClientDC clientDC(NULL);
CDC memDC;
CBitmap bmp;
CBitmap* pBmpOld;
CFont* pFontOld;

if(pFont == NULL)
return;

memDC.CreateCompatibleDC(&clientDC);
bmp.CreateCompatibleBitmap(&clientDC, rcText.Width(), rcText.Height());

pBmpOld = memDC.SelectObject(&bmp);
pFontOld = memDC.SelectObject(pFont);

UINT nFlag = DT_NOPREFIX DT_MODIFYSTRING DT_END_ELLIPSIS DT_SINGLELINE;
memDC.DrawText(strText, rcText, nFlag);

if(pBmpOld != NULL)
memDC.SelectObject(pBmpOld);

if(pFontOld != NULL)
memDC.SelectObject(pFontOld);
}
Others API releted to it:
PathCompactPath
CDC::GetTextExtent

Tuesday, February 2, 2010

Do something while executing a process

// Create the process
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

BOOL bCreate;

bCreate = CreateProcess(szProcessName, szCmdLine, NULL, NULL, TRUE,
CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,NULL, szProcessPath, &si, &pi);

// Wait for the end of the process
while(WaitForSingleObject(pi.hProcess, 250) == WAIT_TIMEOUT)
{
//Do something here
}

// Terminate the process
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);