dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Thread: [RESOLVED] Edit Control and text with transparent background scroll issue

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    Italy
    Posts
    9

    [RESOLVED] Edit Control and text with transparent background scroll issue

    Hallo Everybody,
    need some help as I can't figoure out how to solve this problem:
    I've a edit control setted as multiline, autovscroll etc, and by WM_CTLCOLOREDIT I'm able to set a black background and a text color with transparent background. The issue comes in when i fill te edit with some text out of te edit window, and exactly when i scroll with the scroll bar cursor the text draws on top of itself and creates garbage like the attache picture.
    I've solved partially the issue subclassing the edit and perform a ShowWindow(hwnd, SW_HIDE) with sequentially ShowWindow(hwnd, SW_SHOW) when the left mouse button (WM_LBUTTONDOWN) is pushed on the scroll cursor (that trough a Timer Procedure)
    But this does not solve completely the issue, the "Ghost" text is still there for some milliseconds.
    Sorry for my bad english.
    Here's my full code:

    Code:
    #include <windows.h>
    
    #define IDC_EDIT 101
    
    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    LRESULT APIENTRY EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    VOID CALLBACK ProceduraTimer(HWND hwnd, UINT msg, UINT idEvent, DWORD dwTime);
    
    //Puntatore a WindowProcedure originale
    //(variabile globale);
    WNDPROC wpOrigWindowProc = 0;
    
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        LPSTR lpCmdLine, int nCmdShow)
    {
      HWND hwnd;
      MSG  msg ;    
      WNDCLASS wc = {0};
      wc.lpszClassName = TEXT("GroupBox");
      wc.hInstance     = hInstance ;
      wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
      wc.lpfnWndProc   = WndProc ;
      wc.hCursor       = LoadCursor(0,IDC_ARROW);
    
      HINSTANCE g_hinst = hInstance;
      
      RegisterClass(&wc);
      hwnd = CreateWindow(wc.lpszClassName, TEXT("GroupBox"),
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                          600, 200, 400, 450, 0, 0, hInstance, 0);  
                          
      while(GetMessage(&msg, NULL, 0, 0)) {
           TranslateMessage(&msg);
           DispatchMessage(&msg);
      }
      return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
      HWND hwndEdit;
      DWORD CtrlEsitoID;
      HFONT hFont;
      HBRUSH hBrushCtrlBG;
      HDC hdcEdit;
      HWND hwndFocus = 0;
      HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
    
      switch(msg)  
      {
          
          case WM_CREATE:
               
                  hwndEdit = CreateWindow(TEXT("Edit"), NULL,
                                WS_TABSTOP | WS_CHILD | WS_VISIBLE | ES_WANTRETURN | WS_BORDER | 
                                WS_CLIPCHILDREN | ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL,
                                10, 10, 360, 340, hwnd, (HMENU)IDC_EDIT, 0, 0);
                                
                                
                  //Installazione del Subclassing del controllo edit (registriamo
                  //il valore della Window Procedure precedente);
                  wpOrigWindowProc = (WNDPROC)SetWindowLong(hwndEdit, GWL_WNDPROC, 
                                                           (LONG)EditSubclassProc);
                  
                  hFont = CreateFont(10, 10, 0, 0, FW_EXTRALIGHT, FALSE, FALSE, FALSE, 
                                      DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
                                      CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                                      DEFAULT_PITCH, "MS Sans Serif");
                                      
                  SendMessage(hwndEdit, WM_SETFONT, (WPARAM)hFont, 0); 
                  break;
    
    
          case WM_COMMAND:
                  if((HIWORD(wParam) == BN_PUSHED)) {            
                                    SetTimer(hwnd, LOWORD(wParam), 200, ProceduraTimer);
    							}
                  if(HIWORD(wParam) == BN_UNPUSHED) {
                                    KillTimer(hwnd,HIWORD(wParam));
                                    ShowWindow((HWND)lParam, SW_HIDE);
                                    UpdateWindow((HWND)lParam);
                                    ShowWindow((HWND)lParam,SW_SHOW);
    							}
                  break;
    
    
          case WM_DESTROY:
                  //Rimuovo il subclass dal controllo edit
                  SetWindowLong(hwndEdit, GWL_WNDPROC, (LONG)wpOrigWindowProc);
                  PostQuitMessage(0);
                  break; 
                
                  
          case WM_CTLCOLOREDIT:
                  CtrlEsitoID = GetDlgCtrlID((HWND)lParam);
                  if (CtrlEsitoID == IDC_EDIT)  {
                      hBrushCtrlBG = CreateSolidBrush(RGB(0, 0, 0));
                      hdcEdit = (HDC)wParam;
                      SetTextColor(hdcEdit, RGB(71, 239, 235));
                      //SetBkColor(hdcEdit, RGB(0, 0, 0));
                      SetBkMode(hdcEdit, TRANSPARENT);
                      return (LONG)hBrushCtrlBG;  
                  }
                  break;
                  
           case WM_LBUTTONDOWN: 
                  //if(wParam == MK_LBUTTON)
                  break; 
                    
      }
      return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    //Procedura di Subclass
    LRESULT APIENTRY EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        UINT code = 0;
        switch(uMsg)  {
               case WM_LBUTTONDOWN: code = BN_PUSHED; break;
               case WM_LBUTTONUP:   code = BN_UNPUSHED; break;
        }
        if(code)
               SendMessage(GetParent(hwnd), WM_COMMAND, 
                          MAKELONG(GetDlgCtrlID(hwnd), code), 
                          (LPARAM)hwnd);
        if(wpOrigWindowProc)
               return CallWindowProc(wpOrigWindowProc, hwnd, uMsg, wParam, lParam);
        return 0;
    }
    
    VOID CALLBACK ProceduraTimer(HWND hwnd, UINT msg, UINT idEvent, DWORD dwTime)
    {
       HWND hwndCtrl = GetDlgItem(hwnd, idEvent);
       ShowWindow(hwndCtrl, SW_HIDE);
       UpdateWindow(hwndCtrl);
       ShowWindow(hwndCtrl,SW_SHOW); 
    }
    Any help is very very appreciated, i'm trying to solve this frmo 3 days.
    Thank's in advance.
    Attached Images Attached Images  
    Last edited by ovidiucucu; February 15th, 2012 at 08:33 AM.

  2. #2
    Join Date
    Feb 2012
    Location
    Italy
    Posts
    9

    Re: Edit Control and text with transparent background scroll issue

    Sorry for the wrong code insertion, I can't edit it.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: Edit Control and text with transparent background scroll issue

    hBrushCtrlBG = CreateSolidBrush(RGB(0, 0, 0));
    You constantly create the brush, but never destroy it. This is gonna exhaust GDI objects. You may need stock object instead:
    Code:
    hBrushCtrlBG = (HBRUSH)GetStockObject(BLACK_BRUSH);
    Otherwise you need to create the brash beforehand, and return it always when you need the solid brush.
    Best regards,
    Igor

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: Edit Control and text with transparent background scroll issue

    Well, basically transparent background is the mistake that causes all the fuzz.

    Code:
          case WM_CTLCOLOREDIT:
                  CtrlEsitoID = GetDlgCtrlID((HWND)lParam);
                  if (CtrlEsitoID == IDC_EDIT)  {
                      hdcEdit = (HDC)wParam;
                      SetTextColor(hdcEdit, RGB(71, 239, 235));
                      SetBkColor(hdcEdit, RGB(0, 0, 0));
                      //SetBkMode(hdcEdit, TRANSPARENT);
                      return (LONG)GetStockObject(BLACK_BRUSH);  
                  }
    Last edited by Igor Vartanov; February 15th, 2012 at 01:35 PM.
    Best regards,
    Igor

  5. #5
    Join Date
    Feb 2012
    Location
    Italy
    Posts
    9

    Re: Edit Control and text with transparent background scroll issue

    Hallo, Mr. Igor, Thank you so much for your replay.
    I Already Knew how to solve by the setBkcolor instead and i'm Sorry for
    My inexact explanation: I need to learn how to solve with a transparent background if for exemple i want to set a background Image in a control and scroll the text over it.
    Can you please explain me better the solution you tell before, i mean that one using the stock object instead the brush one. I'm going crazy (I'm learning Win API from less than two mounth) Thank you so much

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: Edit Control and text with transparent background scroll issue

    Regarding the solution for transparent background, I have no one. Per my experience nobody in sane mind fights with old style Windows controls like button, edit box, combo box or list box. It's always easier to implement yourself all what you need rather than tweak the standard control to something beyond its pre-designed behavior.

    The first my message was not a solution at all. It was just a remark regarding the problem you might still be unaware of. When you create your custom GDI object, you need to destroy the one symmetrically. The issue is very similar to memory allocation, and in fact it actually is, as GDI object is allocated by kernel for your process. Naturally, there is a limit for a number of GDI objects created by a process, and once you've lost control on creating ones, sooner or later your process exhausts allowed resources, after then you app behavior becomes unpredictable.

    Unlike to process' custom GDI objects, there in kernel a number of stock objects exist, that are kinda "statically" allocated, and therefore, never cause exhausting. You just request the ones from kernel and never bother about deallocating those. The BLACK_BRUSH is one of them.
    Last edited by Igor Vartanov; February 16th, 2012 at 08:10 AM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)