Code:
/*
* Auto accenter - Rutabega - 12/21/05 - v0.5
*/
#include <windows.h>
#define STAT 100
void AppendWindowText(HWND hWnd, const char * lpString);
HWND GetForegroundFocus(void);
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
CreateWindowEx(0,"STATIC","-",WS_CHILD | WS_VISIBLE,10,10,50,30,hwnd,(HMENU)STAT,NULL,NULL);
}
break;
case WM_HOTKEY:
{
HWND editwnd = NULL;
editwnd = GetForegroundFocus();
if(editwnd == NULL)
{
//MessageBox(hwnd,"No edit present","Warning",MB_OK);
SetWindowText(GetDlgItem(hwnd,STAT),"!");
}
switch(wParam)
{
case 0x0001:
{
//SendMessage(editwnd,WM_SETTEXT,(WPARAM)NULL,(LPARAM)"Testing 1 2 3!");
AppendWindowText(editwnd,"á");
SetWindowText(GetDlgItem(hwnd,STAT),"a");
}
break;
case 0x0002:
{
AppendWindowText(editwnd,"Á");
SetWindowText(GetDlgItem(hwnd,STAT),"A");
}
break;
case 0x0003:
{
AppendWindowText(editwnd,"é");
SetWindowText(GetDlgItem(hwnd,STAT),"e");
}
break;
case 0x0004:
{
AppendWindowText(editwnd,"É");
SetWindowText(GetDlgItem(hwnd,STAT),"E");
}
break;
case 0x0005:
{
AppendWindowText(editwnd,"í");
SetWindowText(GetDlgItem(hwnd,STAT),"i");
}
break;
case 0x0006:
{
AppendWindowText(editwnd,"Í");
SetWindowText(GetDlgItem(hwnd,STAT),"I");
}
break;
case 0x0007:
{
AppendWindowText(editwnd,"ó");
SetWindowText(GetDlgItem(hwnd,STAT),"o");
}
break;
case 0x0008:
{
AppendWindowText(editwnd,"Ó");
SetWindowText(GetDlgItem(hwnd,STAT),"O");
}
break;
case 0x0009:
{
AppendWindowText(editwnd,"ú");
SetWindowText(GetDlgItem(hwnd,STAT),"u");
}
break;
case 0x000A:
{
AppendWindowText(editwnd,"Ú");
SetWindowText(GetDlgItem(hwnd,STAT),"U");
}
break;
case 0x000B:
{
AppendWindowText(editwnd,"ñ");
SetWindowText(GetDlgItem(hwnd,STAT),"n");
}
break;
case 0x000C:
{
AppendWindowText(editwnd,"Ñ");
SetWindowText(GetDlgItem(hwnd,STAT),"N");
}
break;
default:
{
MessageBox(hwnd,"Unknown hotkey","Hotkey!",MB_OK);
}
break;
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return(0);
}
// main
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
HDC hdc;
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Register the window class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "AutoAccent";
wc.hIconSm = NULL;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Window Registration Failed!","Error!",MB_ICONEXCLAMATION | MB_OK);
return(0);
}
//Create the Window
hwnd = CreateWindowEx(0,"AutoAccent","Auto Accént v0.5",WS_OVERLAPPEDWINDOW,100,100,300,200,NULL,NULL,hInstance,NULL);
if(hwnd == NULL)
{
MessageBox(NULL,"Window Creation Failed!","Error!",MB_ICONEXCLAMATION | MB_OK);
return(0);
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
RegisterHotKey(hwnd,0x0001,MOD_ALT | MOD_CONTROL,VkKeyScan('a'));
RegisterHotKey(hwnd,0x0002,MOD_SHIFT | MOD_ALT | MOD_CONTROL,VkKeyScan('a'));
RegisterHotKey(hwnd,0x0003,MOD_ALT | MOD_CONTROL,VkKeyScan('e'));
RegisterHotKey(hwnd,0x0004,MOD_SHIFT | MOD_ALT | MOD_CONTROL,VkKeyScan('e'));
RegisterHotKey(hwnd,0x0005,MOD_ALT | MOD_CONTROL,VkKeyScan('i'));
RegisterHotKey(hwnd,0x0006,MOD_SHIFT | MOD_ALT | MOD_CONTROL,VkKeyScan('i'));
RegisterHotKey(hwnd,0x0007,MOD_ALT | MOD_CONTROL,VkKeyScan('o'));
RegisterHotKey(hwnd,0x0008,MOD_SHIFT | MOD_ALT | MOD_CONTROL,VkKeyScan('o'));
RegisterHotKey(hwnd,0x0009,MOD_ALT | MOD_CONTROL,VkKeyScan('u'));
RegisterHotKey(hwnd,0x000A,MOD_SHIFT | MOD_ALT | MOD_CONTROL,VkKeyScan('u'));
RegisterHotKey(hwnd,0x000B,MOD_ALT | MOD_CONTROL,VkKeyScan('n'));
RegisterHotKey(hwnd,0x000C,MOD_SHIFT | MOD_ALT | MOD_CONTROL,VkKeyScan('n'));
//Enter the Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
UnregisterHotKey(hwnd,0x0001);
UnregisterHotKey(hwnd,0x0002);
UnregisterHotKey(hwnd,0x0003);
UnregisterHotKey(hwnd,0x0004);
UnregisterHotKey(hwnd,0x0005);
UnregisterHotKey(hwnd,0x0006);
UnregisterHotKey(hwnd,0x0007);
UnregisterHotKey(hwnd,0x0008);
UnregisterHotKey(hwnd,0x0009);
UnregisterHotKey(hwnd,0x000A);
UnregisterHotKey(hwnd,0x000B);
UnregisterHotKey(hwnd,0x000C);
return(Msg.wParam);
}
void AppendWindowText(HWND hWnd, const char * lpString)
{
/* not used for this program
int iLength = GetWindowTextLength(hWnd);
SendMessage(hWnd, EM_SETSEL, iLength, iLength);
*/
//the following line is the way I want to do things (and the way it works in non-MDI programs), the other 2 were for testing purposes
SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM) lpString);
//SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM) lpString);
//SendMessage(hWnd, WM_PASTE, 0, 0);
/* not used for this program
SendMessage(hWnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);
*/
}
HWND GetForegroundFocus(void)
{
DWORD foregroundThreadID; // foreground window thread
DWORD MDIclientThreadID; // thread of any mdi client windows
DWORD ourThreadID; // our active thread
HWND focuswnd; //window with keyboard focus
HWND child1 = NULL; //used to get the MDI child
HWND child2 = NULL; //ditto
// Check to see if we are the foreground thread
foregroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(),NULL);
ourThreadID = GetCurrentThreadId();
// If not, attach our thread's 'input' to the foreground thread's
if(foregroundThreadID != ourThreadID)
{
AttachThreadInput(foregroundThreadID, ourThreadID, TRUE);
}
focuswnd = GetFocus(); //should get the handle of the window with the keyboard focus
child1 = focuswnd;//GetTopWindow(focuswnd);
child2 = GetTopWindow(focuswnd);
while(child2 != NULL)
{ //this forces a search into the child windows, this is what gets me the MDI child handle
child1 = child2;
child2 = GetTopWindow(child1);//GetNextWindow(child1,GW_HWNDNEXT);
}
focuswnd = child1;
// If we attached our thread, detach it now
if(foregroundThreadID != ourThreadID)
{
AttachThreadInput(foregroundThreadID, ourThreadID, FALSE);
}
return(focuswnd);
}