Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/AutoCompleteComboBox.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// AutoCompleteComboBox.c
//
// To implement an autocomplete in ComboBox
//


#include "WinSpy.h"
#include <tchar.h>


#define ACC_EDIT_PROC TEXT("Acc_Edit_Proc")
#define GetEditOriProc(hEdit) (WNDPROC)GetProp(hEdit, ACC_EDIT_PROC)


static void ACComboBoxEdit_AutoComplete(HWND hComboBox, TCHAR ch)
{
TCHAR txt[MAX_PATH] = { 0 };
int index = ComboBox_GetCurSel(hComboBox);

if (!_istcntrl(ch))
{
ComboBox_GetText(hComboBox, txt, ARRAYSIZE(txt));

index = ComboBox_FindStringExact(hComboBox, index, txt);
if (CB_ERR == index)
{
index = ComboBox_FindString(hComboBox, index, txt);
}

if (CB_ERR != index)
{
ComboBox_SetCurSel(hComboBox, index);
ComboBox_SetEditSel(hComboBox, _tcslen(txt), -1);
}
}
}

//
// Paste text cannot trigger autocomplete, so when the control
// loses focus, we will try to implement it manually.
//
static void ACComboBoxEdit_OnKillFocus(HWND hComboBox)
{
TCHAR txt[MAX_PATH] = { 0 };

int tc = ComboBox_GetText(hComboBox, txt, ARRAYSIZE(txt));
int si = ComboBox_GetCurSel(hComboBox);
int ei = ComboBox_FindStringExact(hComboBox, si, txt);

if (tc != 0 && CB_ERR != ei && si != ei)
{
ComboBox_SetCurSel(hComboBox, ei);
}
}

static LRESULT CALLBACK ACComboBoxEdit_Proc(HWND hEdit, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CHAR:
{
LRESULT r = CallWindowProc(GetEditOriProc(hEdit), hEdit, msg, wParam, lParam);
ACComboBoxEdit_AutoComplete(GetParent(hEdit), (TCHAR)wParam);
return r;
}

case WM_KILLFOCUS:
ACComboBoxEdit_OnKillFocus(GetParent(hEdit));
break;

case WM_DESTROY:
SetWindowLongPtr(hEdit, GWLP_WNDPROC, (LONG_PTR)GetEditOriProc(hEdit));
RemoveProp(hEdit, ACC_EDIT_PROC);
break;
}

return CallWindowProc(GetEditOriProc(hEdit), hEdit, msg, wParam, lParam);
}

static void ACComboBoxEdit_Init(HWND hComboBox)
{
HWND h = FindWindowEx(hComboBox, NULL, WC_EDIT, NULL);
LONG_PTR o = SetWindowLongPtr(h, GWLP_WNDPROC, (LONG_PTR)ACComboBoxEdit_Proc);

SetProp(h, ACC_EDIT_PROC, (HANDLE)o);

ComboBox_LimitText(hComboBox, MAX_PATH - 1);
}

void InitAutoCompleteComboBox(HWND hDlg, int nIdComboBox)
{
TCHAR txt[MAX_PATH] = { 0 };
HWND hcb = GetDlgItem(hDlg, nIdComboBox);

GetClassName(hcb, txt, ARRAYSIZE(txt));

if (lstrcmp(WC_COMBOBOX, txt) == 0)
{
ACComboBoxEdit_Init(hcb);
}
}
14 changes: 14 additions & 0 deletions src/AutoCompleteComboBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef AUTOCOMPLETECOMBOBOX_INCLUDED
#define AUTOCOMPLETECOMBOBOX_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

void InitAutoCompleteComboBox(HWND hDlg, int nIdComboBox);

#ifdef __cplusplus
}
#endif

#endif
3 changes: 2 additions & 1 deletion src/Poster.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Poster.h"
#include "resource.h"
#include "Utils.h"
#include "AutoCompleteComboBox.h"

typedef struct {
PCSTR pszMsgName;
Expand Down Expand Up @@ -543,7 +544,7 @@ INT_PTR CALLBACK PosterDlgProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lPara
{
case WM_INITDIALOG:
hwndTarget = (HWND)lParam;

InitAutoCompleteComboBox(hwnd, IDC_POSTER_MESSAGES);
SetInitialGuiInfo(hwnd, hwndTarget);
return TRUE;

Expand Down
2 changes: 2 additions & 0 deletions src/winspy.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="AutoCompleteComboBox.c" />
<ClCompile Include="BitmapButton.c" />
<ClCompile Include="CaptureWindow.c" />
<ClCompile Include="DisplayClassInfo.c" />
Expand Down Expand Up @@ -159,6 +160,7 @@
<ClCompile Include="WinSpyWindow.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AutoCompleteComboBox.h" />
<ClInclude Include="BitmapButton.h" />
<ClInclude Include="CaptureWindow.h" />
<ClInclude Include="FindTool.h" />
Expand Down
9 changes: 9 additions & 0 deletions src/winspy.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
<ClCompile Include="DisplayDpiInfo.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="AutoCompleteComboBox.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="BitmapButton.h">
Expand Down Expand Up @@ -140,6 +143,12 @@
<ClInclude Include="resource\resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Poster.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="AutoCompleteComboBox.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="resource\WinSpy.rc">
Expand Down
Loading