Code:
#include "stdafx.h"
#include <windows.h>
#include <ShellAPI.h>
#include <windowsx.h>
#include <commctrl.h>
#pragma comment( lib, "comctl32.lib" )
#define WIN32_LEAN_AND_MEAN
#define IDC_MY_SUBCLASSED_EDIT 2008
const char *ClsName = "SubClassing";
const char *WndName = "Edit Box";
WNDPROC oldEditProcedure = 0;
LRESULT CALLBACK newEditProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
//Main window
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX WndClsEx;
HWND hWnd;
MSG Msg;
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WindowProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(400));
WndClsEx.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(200));
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClsEx))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Create the window object
hWnd = CreateWindowEx(0,
ClsName,
WndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
// Find out if the window was created
if( !hWnd ) // If the window was not created,
return FALSE; // stop the application
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HWND edit = CreateWindowEx (
WS_EX_ACCEPTFILES,
TEXT("You can drag and drop a file here..."),
TEXT("Edit/Browse"),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL | ES_LEFT,
10,
10, // position
300,
22, // size
hwnd,
(HMENU)IDC_MY_SUBCLASSED_EDIT,
(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;
}
LRESULT CALLBACK newEditProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DROPFILES:
{
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;
}
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return CallWindowProc (oldEditProcedure, hwnd, message, wParam, lParam);
}