🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Subclassing an edit control

#12448
From: Martin Golm
Date: Thu, 04 Dec 2003 13:04
82 lines
4867 bytes
Gary Setter wrote:
> I still don't understand what you mean by "my code cannot work as intended".
> Please, please please. State how you expected your code to work and your
> code 'failed'.

Well, then (just showing the relevat code):
I created my own window class and assigned a global window procedure to
it.
  WNDCLASS wndclass;
  wndclass.lpfnWndProc = ::WndProc;
I create tWindow's by using this window class.
  void tWindow :: Create() {
    hWnd = CreateWindow(szWndClsName, title.c_str(), attr.style,
                        attr.left, attr.top, attr.width, attr.height,
                        parent ? parent->GetHandle() : 0, 0,
                        tApp::hInst, reinterpret_cast<void*>(this));
  }
The global window procedure determines the window a message is sent to
and calls that window's window procedure.
  LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam,
                           LPARAM lParam) {
    tWindow* pWindow = reinterpret_cast<tWindow*>(GetWindowLong(hWnd,
0));
    if (!pWindow) {
      if (iMessage == WM_CREATE) {
        LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
        pWindow = reinterpret_cast<tWindow*>(lpcs->lpCreateParams);
        SetWindowLong(hWnd, 0, reinterpret_cast<long>(pWindow));
        return pWindow->WndProc(iMessage, wParam, lParam);
      }
      return DefWindowProc(hWnd, iMessage, wParam, lParam);
    }
    return pWindow->WndProc(iMessage, wParam, lParam);
  }
Now I create an edit control as a descendant of tWindow. Though I can
derive tEdit from tWindow, I need to use a different window class.
  void tEdit :: Create() {
    attr.style |= ES_AUTOHSCROLL | ES_LEFT | WS_TABSTOP;
    hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", attr.style,
                        attr.left, attr.top, attr.width, attr.height,
                        parent ? parent->GetHandle() : 0, 0,
                        tApp::hInst, reinterpret_cast<void*>(this));
  }
Now I thought that messages sent to the edit control would be caught by
the global WndProc and will be forwarded to tEdit's WndProc. And here's
the point where the code cannot work as intended. Of course it can't -
tEdit (being of "EDIT" window class) cannot know about any WndProc I
code for it's not of 'szWndClsName' window class.

The code I posted originally did always work, but it isn't the code I
wrote first. The posted code went one step further and introduced a
window procedure for the edit control to work as I need it to.
So, sorry if we talked at cross-purposes (as it seems to me).


> You can't replace GWL_WNDPROC with GWL_USERDATA. What I was doing was
> replacing Get/SetWindowLong(hWnd,0) with Get/SetWindowLong(hWnd,
> GWL_USERDATA) in the  tWindow  and static WndProc code.
> GetWindowLong(hWnd,0) is only valid for classes that have extra space
> allocated for each window, which you did when you registered your tWindow
> class. But you can't do that with a pre-defined class like EDIT. The only
> way to consistenly manage the tie between the c++ class and the window
> handle (other then a std::map or something) is to use GWL_USERDATA.

Ah, no, sorry, my fault. I meant I was moving from
Get/SetWindowLong(hWnd, 0) to Get/SetWindowLong(hWnd, GWL_USERDATA).

> > Do you see any problems with this? As far as I can tell the app is
> > running.
> 
> The only problem I can see is the GetDC without a matching RleaseDC.

Oh, this is done in the destructor, which I omitted for shortening the
length of the post.


Again, thanks for help. If you're interested in having another look at
the complete code (the one that's working now), I could send it to you
in a private mail.


Martin
Technical Details
Message-ID: <3FCF30C1.7D2DEB13@gmx.de>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.tpinternet.pl!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3FC37DC6.8A69A621@gmx.de> <3fc39946@newsgroups.borland.com> <3FC4C880.15E477D2@gmx.de> <3fc61f76@newsgroups.borland.com> <3fc62d43@newsgroups.borland.com> <3FC72C07.F8402E63@gmx.de> <3fc75c8b@newsgroups.borland.com> <3FCCA981.209B2B0@gmx.de> <3fcdf7b1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,7f8523ddb948f0d0
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-12-04 05:09:27 PST
Message-ID: <3FCF30C1.7D2DEB13@gmx.de>
Date: Thu, 04 Dec 2003 14:04:01 +0100
From: Martin Golm <golm@gmx.de>
X-Mailer: Mozilla 4.72 [en] (Win95; I)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Subclassing an edit control
References: <3FC37DC6.8A69A621@gmx.de> <3fc39946@newsgroups.borland.com> <3FC4C880.15E477D2@gmx.de> <3fc61f76@newsgroups.borland.com> <3fc62d43@newsgroups.borland.com> <3FC72C07.F8402E63@gmx.de> <3fc75c8b@newsgroups.borland.com> <3FCCA981.209B2B0@gmx.de> <3fcdf7b1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 193.159.113.24
X-Trace: newsgroups.borland.com 1070543023 193.159.113.24 (4 Dec 2003 05:03:43 -0700)
Lines: 83
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.tpinternet.pl!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail