Creating an Edit Box (Subclassing)
The code below is my first attempt to create an edit box, i've used three files "SubClassedEditProc.cpp", "subclassing.cpp", and "SubclassedEditProc.h"
BTW these should be .c files, yet struggling to compile C files in VS 2005, get lots of errors.
Subclassing.cpp..
Code:
#include "stdafx.h"
#include <windows.h>
#include "SubclassedEditProc.h" // we need the 'oldEditProcedure' variable
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
/*
First we create an edit control...
*/
HWND edit = CreateWindowEx (
WS_EX_ACCEPTFILES, // Either set the flag here or call DragAcceptFiles (edit, TRUE); after creating the edit
TEXT("EDIT"),
TEXT("You can drag and drop a file here..."),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL | ES_LEFT,
10, 10, // position
300, 22, // size
hwnd,
0, // no control ID (we don't need it in this example)
(HINSTANCE)GetModuleHandle (0),
0
);
oldEditProcedure = (WNDPROC)GetWindowLongPtr (edit, GWLP_WNDPROC);
SetWindowLongPtr (edit, GWLP_WNDPROC, (LONG_PTR)newEditProcedure);
break;
}
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
SubClassedEditProc.cpp
Code:
/*
This is SubclassedEditProc.c
*/
#include "stdafx.h"
#include <ShellAPI.h>
#define WIN32_LEAN_AND_MEAN
#include "SubclassedEditProc.h"
// the pointer to the edit's old window procedure
// this will be used to call the old procedure inside the new window procedure
WNDPROC oldEditProcedure = 0;
// the new edit's window procedure prototype
LRESULT CALLBACK newEditProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
// This is the message we want to handle...
case WM_DROPFILES:
{
// Now comes the code that retrieves the dropped file's path...
// I guess this is not relevant so you don't struggle understanding it.
// Just know that 'buffer' will store the dropped file's path.
HDROP hDrop = (HDROP)wParam;
TCHAR buffer[MAX_PATH];
int nFilesDropped = DragQueryFile (hDrop, 0, buffer, MAX_PATH);
if (nFilesDropped) {
SetWindowText (hwnd, buffer);
}
DragFinish (hDrop);
return 0;
}
// The other message are simply forwarded to the old window procedure
default:
// This is just me being too cautious... the 'oldEditProcedure' pointer will never be null if you're careful
if (oldEditProcedure) {
// The CallWindowProc() API is responsible for calling the 'oldEditProcedure', we are not going to just call it directly, see MSDN for details
return CallWindowProc (oldEditProcedure, hwnd, message, wParam, lParam);
}
else {
// Your application should never reach this point, because you were supposed to set the 'oldEditProcedure' pointer to point to the old window procedure.
return DefWindowProc (hwnd, message, wParam, lParam);
}
}
}
headerfile...
Code:
/*
This is SubclassedEditProc.h
*/
#ifndef __SUBCLASSED_EDIT_H_INCLUDED__
#define __SUBCLASSED_EDIT_H_INCLUDED__
#include <windows.h>
// the pointer to the edit's old window procedure
// this will be used to call the old procedure inside the new window procedure
extern WNDPROC oldEditProcedure;
// the new edit's window procedure prototype
LRESULT CALLBACK newEditProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
#endif
Problems... Link Error
Code:
error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
Not sure whether is looking for WinMain() or main() function????