May 04, 2024, 05:20:30 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


BeginPaint

Started by Mike Stefanik, February 28, 2006, 07:06:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mike Stefanik

I'm having a problem using BeginPaint/EndPaint in an OnPaint handler. I was porting some C++ code, and found that it wasn't drawing correctly. However, when I switched to using GetDC/ReleaseDC, it worked. Here's an example:


#autodefine "off"
#define BEGINPAINT

struct PAINTSTRUCT
{
    unsigned int hdc;
    int fErase;
    RECT rcPaint;
    int fRestore;
    int fIncUpdate;
    unsigned byte rgbReserved[32];
}

declare import,BeginPaint(unsigned int hWnd, PAINTSTRUCT *ps), unsigned int;
declare import,GetDC(unsigned int hWnd), unsigned int;
declare import,GetUpdateRect(unsigned int hWnd, RECT *lprc, int bErase), int;
declare import,EndPaint(unsigned int hWnd, PAINTSTRUCT *ps), int;
declare import,ReleaseDC(unsigned int hWnd, unsigned int hdc), int;
declare import,TextOut alias "TextOutA" (unsigned int hdc, int x, int y, pointer lpString, int cbString), int;

class CMainWnd:CWindow
{
    declare OnClose(), int;
    declare OnPaint(), int;
}

global sub main()
{
    CMainWnd mainWnd;

    mainWnd.Create(100, 100, 800, 600,
                   AWS_OVERLAPPEDWINDOW | AWS_HSCROLL | AWS_VSCROLL | AWS_VISIBLE,
                   0,
                   "Test Window",
                   null);

    do
    {
        Wait();
    }
    until mainWnd.m_hwnd = 0;
    return 0;
}

CMainWnd::OnClose(), int
{
    Destroy();
    return 0;
}

#ifdef BEGINPAINT
CMainWnd::OnPaint(), int
{
    PAINTSTRUCT ps;
    unsigned int hdc;

    hdc = BeginPaint(m_hWnd, &ps);
    TextOut(hdc, 10, 10, "Hello, Windows!", 15);
    EndPaint(m_hWnd, &ps);

    return true;
}
#else
CMainWnd::OnPaint(), int
{
    unsigned int hdc;

    hdc = GetDC(m_hWnd);
    TextOut(hdc, 10, 10, "Hello, Windows!", 15);
    ReleaseDC(m_hWnd, hdc);

    return true;
}
#endif


I get the feeling I'm missing something obvious, but I'm not getting what the difference is there. If I use BeginPaint/EndPaint, it's almost as if the window is being erased after the OnPaint handler is called.

Mike Stefanik
www.catalyst.com
Catalyst Development Corporation

Ionic Wind Support Team

You don't need BeginPaint as it is called for you before OnPaint is called. 

Look in the source archive for the HandlePaint() function (instance.src).  You only need to use GetHDC();  If you want to use text drawing functions directly from the API then you would need to turn off the TA_UPDATECP style of the device context.

HandlePaint, besides taking care of the autodraw window style, masks out any controls you have in your window.

Paul.

Ionic Wind Support Team

Mike Stefanik

Mike Stefanik
www.catalyst.com
Catalyst Development Corporation

Ionic Wind Support Team

Just hope I was clear enough ;)
Ionic Wind Support Team

Mike Stefanik

I dug around in there and saw how things were being setup, so tweaked my OnPaint handler accordingly. Probably would be a good idea for the docs to mention the caveats if you're trying to use the API directly (or in my case, calling functions in a DLL that were doing the same).
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation

Ionic Wind Support Team

Yep.

Aurora uses a saved methodology for the device context of a window.  When you use SetFont, or change the background/foreground colors, or change a line style, etc the current state of the device context is stored.  When you call GetHDC everything is restored for you so it's not necessary to reset a font, pens, brushes, etc.

It makes it a lot more convenient for average GDI tasks.  The only caveat is that text alignment is set to TA_UPDATECP which causes some text drawing functions to ignore the x and y positions.  But that is easily solved with one call to SetTextAlign ;)

Paul.
Ionic Wind Support Team