🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #5199

Re: Using MessageBox

#5199
From: Remy Lebeau (TeamB)
Date: Fri, 26 Aug 2005 23:53
105 lines
4624 bytes
"David Erbas-White" <derbas@arachneering.com> wrote in message
news:430f4a7f$1@newsgroups.borland.com...

> Now, my client would like me to add the ability to 'print' the text
> contents of them.  No big deal, but rather than 'roll my own' message
> box with a 'print' button on it, I was wondering whether anyone had
> played around with finding out if keys can be captured by an application
> while a messagebox is up -- then I could simply put a line at the bottom
> that says "Press CTRL-P to print this screen" or something similar.

Here is an example that uses a couple of hooks to add a Print button to the
standard MessageBox() dialog and then process when it is clicked on:


    HHOOK hMsgBoxCBTHook = NULL;
    HHOOK hMsgBoxProcHook = NULL;

    LRESULT CALLBACK MyCBTProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if( nCode == HCBT_ACTIVATE )
        {
            HWND hDlg = (HWND) wParam;
            HWND hBtn = ::GetDlgItem(hDlg, IDOK);

            RECT rDlg = {0};
            ::GetWindowRect(hDlg, &rDlg);

            RECT rBtn = {0};
            ::GetWindowRect(hBtn, &rBtn);
            ::MapWindowPoints(NULL, hDlg, (LPPOINT)&rBtn, 2);

            LONG lStyle = (::GetWindowLong(hBtn, GWL_STYLE) &
~BS_DEFPUSHBUTTON);
            LONG lExStyle = ::GetWindowLong(hBtn, GWL_EXSTYLE);

            HWND hPrint = ::CreateWindowEx(lExStyle, TEXT("BUTTON"),
TEXT("Print"),
                lStyle, rBtn.left, rBtn.bottom + 4, rBtn.right - rBtn.left,
                rBtn.bottom - rBtn.top, hDlg, (HMENU)12345, HInstance,
NULL);

            if( hPrint )
            {
                RECT rPrint = {0};
                ::GetWindowRect(hPrint, &rPrint);

                ::SetWindowPos(hDlg, NULL, 0, 0, rDlg.right - rDlg.left,
                    (rDlg.bottom - rDlg.top) + (rBtn.bottom - rBtn.top) + 4,
                    SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER |
SWP_NOOWNERZORDER);
            }
        }

        return CallNextHookEx(hMsgBoxCBTHook, nCode, wParam, lParam);
    }

    LRESULT CALLBACK MyWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if( nCode == HC_ACTION )
        {
            CWPRETSTRUCT *cw = (CWPRETSTRUCT*) lParam;
            if( (cw->message == WM_COMMAND) &&
                (HIWORD(cw->wParam) == BN_CLICKED) &&
                (LOWORD(cw->wParam) == 12345) )
            {
                HWND hText = ::FindWindowEx(cw->hwnd, NULL, TEXT("STATIC"),
NULL);
                if( hText )
                {
                    int len = ::GetWindowTextLength(hText);
                    if( len > 0 )
                    {
                        LPTSTR pText = (LPTSTR) ::LocalAlloc(LMEM_FIXED,
(len+1)*sizeof(TCHAR));
                        if( pText )
                        {
                            ::GetWindowText(hText, pText, len+1);
                            // print pText as needed...
                            ::LocalFree(pText);
                        }
                    }
                }
            }
        }

        return CallNextHookEx(hMsgBoxProcHook, nCode, wParam, lParam);
    }

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        hMsgBoxCBTHook = ::SetWindowsHookEx(WH_CBT, (HOOKPROC)MyCBTProc,
NULL, GetCurrentThreadId());
        hMsgBoxProcHook = ::SetWindowsHookEx(WH_CALLWNDPROCRET,
(HOOKPROC)MyWndRetProc, NULL, GetCurrentThreadId());

        ::MessageBox(NULL, TEXT("This is a Test Message"), TEXT("Test"),
MB_OKCANCEL);

        ::UnhookWindowsHookEx(hMsgBoxCBTHook);
        ::UnhookWindowsHookEx(hMsgBoxProcHook);
    }


Gambit
Technical Details
Message-ID: <430fabae$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newscon02.news.prodigy.com!prodigy.net!nx01.iad01.newshosting.com!newshosting.com!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <430f4a7f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f7584baaf146a181
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <430f4a7f$1@newsgroups.borland.com>
Subject: Re: Using MessageBox
Date: Fri, 26 Aug 2005 16:53:40 -0700
Lines: 107
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <430fabae$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1125100462 68.69.17.41 (26 Aug 2005 16:54:22 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newscon02.news.prodigy.com!prodigy.net!nx01.iad01.newshosting.com!newshosting.com!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail