You are not logged in.

#1 2006-11-17 14:52:00

tardo
Member
Registered: 2006-07-15
Posts: 526

Trying to shorten some C++ code

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

#2 2006-11-17 17:00:16

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: Trying to shorten some C++ code

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

#3 2006-11-17 22:04:55

tranquility
Member
From: Portugal
Registered: 2004-08-06
Posts: 136

Re: Trying to shorten some C++ code

I can shorten that to one line, but perhaps not the way you intent it to be shortened tongue

Offline

#4 2006-11-18 02:20:50

hypermegachi
Member
Registered: 2004-07-25
Posts: 311

Re: Trying to shorten some C++ code

tranquility wrote:

I can shorten that to one line, but perhaps not the way you intent it to be shortened tongue

who says multiple semicolons on the same line isn't good programming practice???

Offline

#5 2006-11-18 03:37:38

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Trying to shorten some C++ code

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

#6 2006-11-18 03:55:08

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: Trying to shorten some C++ code

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

#7 2006-11-19 03:38:23

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Trying to shorten some C++ code

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

#8 2006-11-19 04:53:56

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: Trying to shorten some C++ code

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

#9 2006-11-19 05:23:23

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Trying to shorten some C++ code

tardo wrote:

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.  wink

Offline

Board footer

Powered by FluxBB