Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Strange edit box behaviour

I want to use some owner-drawn edit-box in my program (i want to have the edit box with rounded edges).
As it doesn't seem possible to do this in the same way you'll do it with a button, I did something like this:

[code]

case WM_CTLCOLOREDIT:
RECT rectch, rectp;
HDC hdc;

SetBkMode((HDC)wParam, TRANSPARENT);
GetWindowRect((HWND)lParam, &rectch);
GetWindowRect(hDlg, &rectp);
hdc = GetDC(hDlg);
color = CreateSolidBrush(RGB(217, 226, 186));
pen = CreatePen(PS_SOLID, 1, RGB(131, 140, 100));
SelectObject(hdc, color);
SelectObject(hdc, pen);
RoundRect(hdc, rectch.left - rectp.left - 3, rectch.top - rectp.top - 3,
rectch.right - rectp.left + 3, rectch.bottom - rectp.top + 3, 5, 5);
DeleteObject(color);
DeleteObject(pen);
ReleaseDC(hDlg, hdc);
return (LRESULT)(CreateSolidBrush(RGB(217, 226, 186)));
break;
[/code]

This works well, and I have my owner drawn edit box!
Yet, there is a problem: I have a dialog with some of this edit box, and when I switch between them with TAB, the content of the edit box that gains focus disappears, but if I write in it or press an arrow or click with the mouse it *magically* reappear! Where's the problem???
How can I solve it???

Thanks
nICO


[hr]
[italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
JOHN KEATS

Comments

  • pingpongpingpong Member Posts: 937
    [b][red]This message was edited by pingpong at 2002-9-22 5:10:40[/red][/b][hr]
    I tried your code and I have three notes:

    1. First off, to make it work, you need to add WS_CLIPCHILDREN style to your parent. If this is a dialog, go to your dialog editor, properties for your dialog, then check the "Clip Children" box. If you created the dialog or parent window by hand, add "WS_CLIPCHILDREN" to the dwStyle member of your CreateWindow(Ex). Check MSDN of what this flag does. Now, if you have WS_BORDER style for the edit boxes, now they will show (the dialog will clip the whole thing and will never draw on top of it), if you want that, leave them in, if not, uncheck the "Border" from your edit box styles or dont add WS_BORDER if you are creating them manually.

    2. Your code leaks, according to MSDN, the brush you return from WM_CTLCOLORXXX is NOT deleted by the system, your program would crash I believe after 100 or so paints in 9x. You can do something like this to get rid of this problem:
    [code]
    LRESULT WINAPI MyDlgProc(HWND hDlg, ...
    {
    static HBRUSH hMyEditBackBrush = NULL;

    case WM_INIDIALOG:
    hMyEditBackBrush = CreateSolidBrush(RGB(127, 226, 186));
    return 1;

    case WM_CTLCOLOREDIT:
    // some code
    return (LRESULT)hMyEditBackBrush;

    case WM_DESTROY:
    if(hMyEditBackBrush != NULL)
    DeleteObject(hMyEditBackBrush);
    return 1;
    }
    [/code]

    3. Finally, when I tried with my own dialog, the roundrect never painted on where its supposed to be (also, shifted down and right), I did not understand how are you calculating the params for RoundRect but I believe you want 3 pixels bigger than the edit control size.. If this is the case, this worked for me:
    [code]
    ScreenToClient(hWnd, (POINT*)&rectch.left);
    ScreenToClient(hWnd, (POINT*)&rectch.right);
    RoundRect(hdc, rectch.left - 3, rectch.top - 3, rectch.right + 3, rectch.bottom + 3, 5, 5);
    [/code]
    No need for the window rect (rectp) anymore.

    Hope this helps.

    : I want to use some owner-drawn edit-box in my program (i want to have the edit box with rounded edges).
    : As it doesn't seem possible to do this in the same way you'll do it with a button, I did something like this:
    :
    : [code]
    :
    : case WM_CTLCOLOREDIT:
    : RECT rectch, rectp;
    : HDC hdc;
    :
    : SetBkMode((HDC)wParam, TRANSPARENT);
    : GetWindowRect((HWND)lParam, &rectch);
    : GetWindowRect(hDlg, &rectp);
    : hdc = GetDC(hDlg);
    : color = CreateSolidBrush(RGB(217, 226, 186));
    : pen = CreatePen(PS_SOLID, 1, RGB(131, 140, 100));
    : SelectObject(hdc, color);
    : SelectObject(hdc, pen);
    : RoundRect(hdc, rectch.left - rectp.left - 3, rectch.top - rectp.top - 3,
    : rectch.right - rectp.left + 3, rectch.bottom - rectp.top + 3, 5, 5);
    : DeleteObject(color);
    : DeleteObject(pen);
    : ReleaseDC(hDlg, hdc);
    : return (LRESULT)(CreateSolidBrush(RGB(217, 226, 186)));
    : break;
    : [/code]
    :
    : This works well, and I have my owner drawn edit box!
    : Yet, there is a problem: I have a dialog with some of this edit box, and when I switch between them with TAB, the content of the edit box that gains focus disappears, but if I write in it or press an arrow or click with the mouse it *magically* reappear! Where's the problem???
    : How can I solve it???
    :
    : Thanks
    : nICO
    :
    :
    : [hr]
    : [italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
    : JOHN KEATS
    :
    :



  • chick80chick80 Member Posts: 349
    2. Your code leaks, according to MSDN, the brush you return from WM_CTLCOLORXXX is NOT deleted by the system, your program would crash I believe after 100 or so paints in 9x. You can do something like this to get rid of this problem:
    : [code]
    : LRESULT WINAPI MyDlgProc(HWND hDlg, ...
    : {
    : static HBRUSH hMyEditBackBrush = NULL;
    :
    : case WM_INIDIALOG:
    : hMyEditBackBrush = CreateSolidBrush(RGB(127, 226, 186));
    : return 1;
    :
    : case WM_CTLCOLOREDIT:
    : // some code
    : return (LRESULT)hMyEditBackBrush;
    :
    : case WM_DESTROY:
    : if(hMyEditBackBrush != NULL)
    : DeleteObject(hMyEditBackBrush);
    : return 1;
    : }
    : [/code]

    [blue] Thanks, I was aware of that, I was searching some way to do it... now I know how![/blue]

    : 3. Finally, when I tried with my own dialog, the roundrect never painted on where its supposed to be (also, shifted down and right), I did not understand how are you calculating the params for RoundRect but I believe you want 3 pixels bigger than the edit control size.. If this is the case, this worked for me:
    : [code]
    : ScreenToClient(hWnd, (POINT*)&rectch.left);
    : ScreenToClient(hWnd, (POINT*)&rectch.right);
    : RoundRect(hdc, rectch.left - 3, rectch.top - 3, rectch.right + 3, rectch.bottom + 3, 5, 5);
    : [/code]
    : No need for the window rect (rectp) anymore.
    :
    [blue]Yes, I wanted it 3 pixel bigger, but I didn't have position problems (I'll try also your code, anyway)... the only problem was that the control's text disappeared when switching between controls with TAB. The only solution i found to solve this problem is to select all the contents of the edit box (with EN_SETSEL) when it gains focus. Now it works!
    Thanks for the comments, expecially the second, I had no idea how to destroy that brush![/blue]

    nICO

    [hr]
    [italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
    JOHN KEATS


  • Shawn CarterShawn Carter Member Posts: 0

    ________ { http://forcoder.org } free ebooks and video tutorials about || Visual Basic .NET C++ Ruby Assembly R Delphi Perl PL/SQL C Python MATLAB Visual Basic Scratch JavaScript PHP Swift C# Java Objective-C Go Lua Transact-SQL Fortran Kotlin Scheme VBScript Ada LabVIEW Bash F# D Hack Julia Alice Erlang COBOL Dart FoxPro Apex Clojure Crystal Logo Prolog ML Scala SAS ABAP Awk Rust Lisp || __

Sign In or Register to comment.