You are not logged in.
Pages: 1
void CClipboardDlg::OnButtonCopy()
{
if ( OpenClipboard() && IsClipboardFormatAvailable(CF_TEXT) )
{
int str_len = EmptyClipboard() ? m_editbox.GetWindowTextLength() : 0;
HGLOBAL h_global_copy = str_len != 0 ? GlobalAlloc(GMEM_MOVEABLE, str_len + 1) : NULL;
if (h_global_copy != NULL)
{
LPTSTR lp_string = (LPTSTR) GlobalLock(h_global_copy);
m_editbox.GetWindowText(lp_string, str_len + 1);
GlobalUnlock(h_global_copy);
SetClipboardData(CF_TEXT, h_global_copy);
}
CloseClipboard();
}
}
I'd like to shorten that to about five lines. Any ideas? (MFC Winblows programming)
Offline
down to
// 7 lines
void CClipboardDlg::OnButtonCopy()
{
if ( OpenClipboard() && IsClipboardFormatAvailable(CF_TEXT) )
{
HGLOBAL h_global_copy = EmptyClipboard() ? GlobalAlloc(GMEM_MOVEABLE, m_editbox.GetWindowTextLength() + 1) : NULL;
if (h_global_copy != NULL)
{
m_editbox.GetWindowText((LPTSTR) GlobalLock(h_global_copy), m_editbox.GetWindowTextLength() + 1);
GlobalUnlock(h_global_copy);
SetClipboardData(CF_TEXT, h_global_copy);
}
CloseClipboard();
}
}
Offline
I can shorten that to one line, but perhaps not the way you intent it to be shortened
Offline
I can shorten that to one line, but perhaps not the way you intent it to be shortened
who says multiple semicolons on the same line isn't good programming practice???
Offline
Why do you need to shorten this? It's all function calls that kinda need to be there... shortening code for the sake of doing it does nothing - it will not make your program faster.
Also, OpenClipboard accepts a HWND owner parameter, and I'd be careful with passing the HGLOBAL to GetWindowText - it seems hackish. None of that is C++ style at all... it's MS C-smashed-into-classes style.
Instead of worrying about the length of your code, I would try doing it in real C++ style.
Offline
My instructor said he did it in 15 lines. I bet with him I'd do it in less. My original code was about 21 lines. And yes it's C smashed into classes, C++ isn't my greatest strength.
edit: btw, I've already turned it in. Managed to get it down to 14 lines which will just have to do.
Any chance you could show me the C++ way of doing it? (even tho it's MFC)
Offline
These are not equivalent:
int str_len = EmptyClipboard() ? m_editbox.GetWindowTextLength() : 0;
HGLOBAL h_global_copy = str_len != 0 ? GlobalAlloc(GMEM_MOVEABLE, str_len + 1) : NULL;
HGLOBAL h_global_copy = EmptyClipboard() ? GlobalAlloc(GMEM_MOVEABLE, m_editbox.GetWindowTextLength() + 1) : NULL;
What if m_editbox.GetWindowTextLength() returns zero?
Offline
shouldn't matter because the value is at minimum 1 (Note the +1).
And yes I know it's bad programming practice, but this is only homework =/
Offline
shouldn't matter because the value is at minimum 1 (Note the +1).
I'm not saying it's broken code. I'm just saying that if you're trying to do the exact same thing as your instructor, then you're not, because the two parts don't do identical things.
In the first case, if GetWindowTextLength() returns 0, then GlobalAlloc is never called and the if block is never executed.
In the second case, GlobalAlloc is called and the if block is executed even if GetWindowTextLength() returns zero.
It's a subtle difference, but a difference nonetheless.
Offline
Pages: 1