-
Cut, copy, undo, AAGG
I'm tryin to set up the ability to copy, paste, undo, and cut in my MDIWndProc, but it isnt working. Heres what I have so far:
Code:
LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
{
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100, hwnd, (HMENU)ID_CHILD_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL) {
MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
}
hfDefault = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
break;
case WM_CTLCOLOREDIT:
{
HDC dc = (HDC)wParam;
SetTextColor(dc, RGB(255, 0, 0));
}
break;
case WM_SIZE:
{
HWND hEdit;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hEdit = GetDlgItem(hwnd, ID_CHILD_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
return DefMDIChildProc(hwnd, msg, wParam, lParam);
case WM_MDIACTIVATE:
{
HMENU hMenu, hFileMenu;
UINT EnableFlag;
hMenu = GetMenu(g_hMainWindow);
if(hwnd == (HWND)lParam) {
EnableFlag = MF_ENABLED;
} else {
EnableFlag = MF_GRAYED;
}
EnableMenuItem(hMenu, 1, MF_BYPOSITION | EnableFlag);
EnableMenuItem(hMenu, 2, MF_BYPOSITION | EnableFlag);
hFileMenu = GetSubMenu(hMenu, 0);
EnableMenuItem(hFileMenu, ID_FILE_SAVEAS, MF_BYCOMMAND | EnableFlag);
EnableMenuItem(hFileMenu, ID_WINDOW_CLOSE, MF_BYCOMMAND | EnableFlag);
EnableMenuItem(hFileMenu, ID_WINDOW_CLOSEALL, MF_BYCOMMAND | EnableFlag);
DrawMenuBar(g_hMainWindow);
}
break;
case WM_ERASEBKGND:
return 1;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case ID_FILE_SAVEAS:
DoFileSaveAs(hwnd);
break;
case ID_EDIT_CUT:
SendDlgItemMessage(hwnd, ID_CHILD_EDIT, WM_CUT, 0, 0);
break;
case ID_EDIT_COPY:
SendDlgItemMessage(hwnd, ID_CHILD_EDIT, WM_COPY, 0, 0);
break;
case ID_EDIT_PASTE:
SendDlgItemMessage(hwnd, ID_CHILD_EDIT, WM_PASTE, 0, 0);
break;
case ID_EDIT_UNDO:
{
if(SendMessage(hwnd, EM_CANUNDO, 0, 0)) {
SendMessage(hwnd, WM_UNDO, 0, 0);
} else {
MessageBox(hwnd, "Nothing to undo.", "Unable to undo!", MB_OK);
}
}
break;
}
break;
default:
return DefMDIChildProc(hwnd, msg, wParam, lParam);
}
return 0;
}
Thanks :D !
-
About the UNDO:
hEdit is the handle of the edit box. not hwnd!
so, it's not if(SendMessage(hwnd, EM_CANUNDO, 0, 0))
SendMessage(hwnd, WM_UNDO, 0, 0);
it's suppost to be: if(SendMessage(hEdit, EM_CANUNDO, 0, 0))
SendMessage(hEdit, WM_UNDO, 0, 0);
About the CUT, COPY, PASTE:
try this: SendMessage(hEdit, WM_CUT, 0, 0);
Because with this you can latter on, make it work with more than one edit boxes, with GetFocus()
But there is no problem the way you did the cut, paste and copy.
Note: you should make hEdit a global variable, or if you want it local, you will need to: hEdit=GetFocus(); before every cut, copy, paste, undo
I hope that helps.