Feb 13th, 2003, 04:30 PM
#1
Thread Starter
Frenzied Member
Subclass Edit Window Class
I wrote classes to make using windows and edit controls easier.
The CEdit class subclasses an edit control to catch it's messages.
As soon as the CEdit is created and the window procedure is
subclassed, the CEdit destructor is called. The subclass
procedure is never called even though the call to
SetWindowLong(...) returns a value.
HELP!
Code:
// resource.h
#define IDC_EDIT 5000
Code:
// CWindow.h
#ifndef __CWINDOW_H__
#define __CWINDOW_H__
#include <windows.h>
class CWindow
{
HWND hWnd;
public:
CWindow();
CWindow(HINSTANCE hInstance, LPCTSTR szClassName, UINT uIcon,
UINT uIconSm, UINT uWidth, UINT uHeight);
~CWindow();
HWND GetHwnd();
INT RelayMessages();
void OnCreate(HWND hWnd);
void OnDestroy();
static LRESULT CALLBACK WindowProcStub(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
};
#endif // __CWINDOW_H__
Code:
// CEdit.h
#ifndef __CEDIT_H__
#define __CEDIT_H__
#include <windows.h>
class CEdit
{
HWND hWndEdit;
WNDPROC wOldProc;
public:
CEdit();
~CEdit();
CEdit(HWND hWndParent, HINSTANCE hInstance, DWORD dStyle, UINT uId,
UINT uLeft, UINT uTop, UINT uWidth, UINT uHeight);
void OnLButtonUp();
void OnKeyUp();
static LRESULT CALLBACK EditProcStub(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK EditProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
};
#endif // __CEDIT_H__
Code:
// CWindow.cpp
#include "CWindow.h"
CWindow::CWindow()
{
}
CWindow::CWindow(HINSTANCE hInstance, LPCTSTR szClassName, UINT uIcon,
UINT uIconSm, UINT uWidth, UINT uHeight)
{
WNDCLASSEX wincl;
wincl.hInstance = hInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcStub;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof(WNDCLASSEX);
if(uIcon == 0)
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
else
wincl.hIcon = LoadIcon(hInstance, (LPCTSTR)uIcon);
if(uIcon == 0)
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
else
wincl.hIconSm = LoadIcon(hInstance, (LPCTSTR)uIconSm);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = (LPCTSTR)NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
if(!RegisterClassEx(&wincl))
{
PostQuitMessage(0);
}
hWnd = CreateWindow(
szClassName,
"Windows App",
WS_OVERLAPPEDWINDOW,
0,
0,
uWidth,
uHeight,
HWND_DESKTOP,
NULL,
hInstance,
NULL
);
}
CWindow::~CWindow()
{
}
HWND CWindow::GetHwnd()
{
return hWnd;
}
INT CWindow::RelayMessages()
{
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
if(!IsDialogMessage(hWnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
LRESULT CALLBACK CWindow::WindowProcStub(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
return ((CWindow*)lParam)->WindowProcedure(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK CWindow::WindowProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
{
//if(LOWORD(wParam)==IDC_EDIT)
//{
// MessageBox(hWnd, "edit", "", 0);
//}
}
break;
case WM_CREATE:
{
OnCreate(hWnd);
}
break;
case WM_DESTROY:
{
OnDestroy();
}
break;
default:
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
}
return 0;
}
Code:
// CEdit.cpp
#include "CEdit.h"
CEdit::CEdit(HWND hWndParent, HINSTANCE hInstance, DWORD dStyle, UINT uId,
UINT uLeft, UINT uTop, UINT uWidth, UINT uHeight)
{
HFONT hFont;
hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | dStyle, uLeft, uTop, uWidth, uHeight,
hWndParent, (HMENU)uId, hInstance, 0);
hFont = CreateFont(10,0,0,0,0,0,0,0,0,0,0,0,0,"MS Sans Serif");
SendMessage(hWndEdit, WM_SETFONT, (WPARAM)hFont, 0);
wOldProc = (WNDPROC)SetWindowLong(hWndEdit, GWL_WNDPROC, (LONG)EditProcStub);
if(wOldProc)
MessageBox(0, "new proc set", "", 0);
else
MessageBox(0, "no new proc", "", 0);
}
CEdit::CEdit()
{
}
CEdit::~CEdit()
{
SetWindowLong(hWndEdit, GWL_WNDPROC, (LONG)wOldProc);
MessageBox(0, "class destructor", "", 0);
}
LRESULT CALLBACK CEdit::EditProcStub(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
MessageBox(0, "stub", "", 0);
return ((CEdit*)lParam)->EditProc(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK CEdit::EditProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
MessageBox(0, "proc", "", 0);
switch(uMsg)
{
case WM_LBUTTONUP:
{
OnLButtonUp();
}
break;
case WM_KEYUP:
{
OnKeyUp();
}
break;
default:
{
}
break;
}
return CallWindowProc(wOldProc, hWnd, uMsg, wParam, lParam);
}
Code:
// Main.cpp
#include "CEdit.h"
#include "CWindow.h"
#include "resource.h"
INT WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument, INT nCmdShow)
{
CWindow cWindow(hInstance, "WinApp", 0, 0, 300, 225);
return cWindow.RelayMessages();
}
void CWindow::OnCreate(HWND hWnd)
{
DOUBLE dx, dy;
RECT rct;
GetWindowRect(hWnd, &rct);
dx = GetSystemMetrics(SM_CXSCREEN);
dy = GetSystemMetrics(SM_CYSCREEN);
dx = (dx / 2) - ((rct.right - rct.left) / 2);
dy = (dy / 2) - ((rct.bottom - rct.top) / 2);
MoveWindow(hWnd, (INT)dx, (INT)dy, rct.right - rct.left, rct.bottom - rct.top, TRUE);
CEdit cEdit(hWnd, GetModuleHandle(NULL), WS_VSCROLL | WS_HSCROLL |
ES_MULTILINE | ES_WANTRETURN, IDC_EDIT, 0, 0, 100, 100);
ShowWindow(hWnd, SW_SHOW);
}
void CWindow::OnDestroy()
{
PostQuitMessage(0);
}
void CEdit::OnKeyUp()
{
MessageBox(0, "keyup", "", 0);
}
void CEdit::OnLButtonUp()
{
MessageBox(0, "lbuttonup", "", 0);
}
Attached Files
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width