?
Solved

Autocomplete combobox(ex)  using Win32 API in C

Posted on 2002-04-19
11
Medium Priority
?
2,215 Views
Last Modified: 2013-12-03
Using plain Win32 API in C, how do I capture the keyboard input within a control(edit,combobox,etc.) on a dialog-based app?

As example, how should I do autocomplete comboboxes in a dialog-based Win32 API app?

Additional 150 points will be added for best suitable answer for autocomplete combobox (and comboboxex) using simple Win32 API in C (code, explanation, etc.). Or in non-MFC C++.  

Thanks in advanced!
Roland
0
Comment
Question by:RolandAZ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6953376
If u r looking for a VB code, just llok in to this
http://www.vbthunder.com/source/combobox/autocomplete.htm

GOOD LUCK
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6955106
One possible way is to subclass every control, created in your app. In this case you will receive all the messages addressed to that controls. But it's not trivial task.
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6955109
Sorry. My note is valid only if you can't modify and recompile that app. Otherwise it is just excersise for windows messages processing.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 1

Author Comment

by:RolandAZ
ID: 6955745
Thanks for comments, Roshmon and Pavlik.

Roshmon,
The VB code will surely be used in a VB app. What I need though is code in C Win32 API. Or in non-MFC C++ code/sample.

Pavlik,
Can you give example code in C or non-MFC C++? 150 points will be added to the question if I can use that C, or/and non-MFC C++ code.

Thanks again, guys!

P.S. Question updated to include the programming language ,C/C++, required.



 
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6960631
I'm kind of busy now. I'll try to write some example on Thursday.
0
 
LVL 1

Author Comment

by:RolandAZ
ID: 6974871
Thanks Pavlik...

0
 
LVL 1

Expert Comment

by:Pavlik
ID: 7000147
Ok. Finaly i did it.
Sorry for the late answer.

The idea is to use SetWindowsHookEx() function with WH_SYSMSGFILTER. It will give you ability to catch every message, sent to controls like edit or combobox.
My sample consists of 3 files:
EditHook.h - functions that will be exported from DLL.
EditHook.cpp - Implementation of DLL.
Slon1.cpp - simple console app which loads DLL and calls functions from it.


EditHook.h
===========================================
#ifdef EDITHOOK_EXPORTS
#define EDITHOOK_API __declspec(dllexport)
#else
#define EDITHOOK_API __declspec(dllimport)
#endif

EDITHOOK_API void SetEditHook();
EDITHOOK_API void CancelEditHook();
-------------------------------------------
EditHook.cpp
===========================================
#include <windows.h>
#include "EditHook.h"

HMODULE hm;

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                          )
{
  switch (ul_reason_for_call)
  {
    case DLL_PROCESS_ATTACH:
      hm = (HMODULE)hModule;
      break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
      break;
  }
  return TRUE;
}

HHOOK hHook;

LRESULT CALLBACK MySysMsgProc(
  int nCode,      // message flag
  WPARAM wParam,  // not used
  LPARAM lParam   // message data
)
{
  MSG* pMsg = (MSG*)lParam;
  if (nCode == MSGF_DIALOGBOX)
  {
    char className[256];
    GetClassName(pMsg->hwnd, className, 255);
    if (strncmp(className, "Edit",4) == 0)
    {
      char aaa[4];
      SendMessage(pMsg->hwnd, WM_GETTEXT, 4, (LPARAM)aaa);
      if (strncmp(aaa, "aaa", 3) == 0)
        SendMessage(pMsg->hwnd, WM_SETTEXT, 0, (LPARAM)"bbb");
    }
  }
  return CallNextHookEx(hHook, nCode, wParam, lParam);
}

EDITHOOK_API void SetEditHook()
{
  hHook = SetWindowsHookEx(WH_SYSMSGFILTER, MySysMsgProc, hm, 0);
}

EDITHOOK_API void CancelEditHook()
{
  UnhookWindowsHookEx(hHook);
}
-------------------------------------------
Slon1.cpp
===========================================
#include <windows.h>
#include "EditHook.h"

int main(int argc, char* argv[])
{
  SetEditHook();
  for (int i = 0; i < 100; i++)
    Sleep(1000);
  CancelEditHook();
  return 0;
}
-------------------------------------------

So EditHook.cpp should be compiled to DLL.
Slon1.cpp should be compiled to console app. And EditHook.lib should be added to it.

After you have both of them simply run Slon1.exe and during its execution start any application with edit box. And try to enter "aaa" (without quotes) in that edit box. If they will change to "bbb" then your hook is working.

Good luck.

Pavlik.
0
 
LVL 1

Author Comment

by:RolandAZ
ID: 7006227
Thanks for great sample, Pavlik!
It works great...
But I'm looking for autocomplete comboboxex
I should give you some points though, i don't know how to do that though. If you know how I'll give 100 points to you.

I'm adding another 50 points ...

I'm willing to give 300 points for the best suitable answer.

Thanks again

Roland




0
 
LVL 1

Author Comment

by:RolandAZ
ID: 7006242
All Experts,
How do I give 100 points to Pavlik but still keep the question open?

Thanks again

Roland
0
 
LVL 1

Accepted Solution

by:
Pavlik earned 800 total points
ID: 7006711
Well actually this sample just shows you the way how to catch messages to any edit controls. Once you know how to do this you can catch messages to other controls (like comboboxes or buttons for example).

In your particular case all you need is to get the parent of edit control and check if it is ComboBox. And if it is so, do whatever you want.

In the example from my previous message you should replace MySysMsgProc with the code like this:
===========================================
LRESULT CALLBACK MySysMsgProc(
 int nCode,      // message flag
 WPARAM wParam,  // not used
 LPARAM lParam   // message data
)
{
 MSG* pMsg = (MSG*)lParam;
 if (nCode == MSGF_DIALOGBOX)
 {
   static const char EditName[] = "Edit";
   char className[256];
   GetClassName(pMsg->hwnd, className, sizeof(EditName));
   if (strncmp(className, EditName, sizeof(EditName)) == 0)
   {
     HWND hParent = GetParent(pMsg->hwnd);
     if (hParent != 0)
     {
       static const char ComboName[] = "ComboBox";
       GetClassName(hParent, className, sizeof(ComboName));
       if (strncmp(className, ComboName, sizeof(ComboName)) == 0)
       {
         char aaa[4];
         SendMessage(pMsg->hwnd, WM_GETTEXT, 4, (LPARAM)aaa);
         if (strncmp(aaa, "aaa", 3) == 0)
         {
           SendMessage(pMsg->hwnd, WM_SETTEXT, 0, (LPARAM)"aaabbb");
           SendMessage(pMsg->hwnd, EM_SETSEL, 3, -1);
         }
       }
     }
   }
 }
 return CallNextHookEx(hHook, nCode, wParam, lParam);
}
===========================================

This code won't work in completely all the cases. To write the final code you should know more about apps you are going to hook. Play with Spy++, look at the windows structure for your app, at the messages your comboboxes receive. Check the docs about SetWindowsHookEx() function. This will help you to find the right way of achieving your goal.

If you won't manage to control comboboxes in some app, just let me know. In this case i'll need to look at that app.

Good Luck.
0
 
LVL 1

Author Comment

by:RolandAZ
ID: 7031511
Thanks, again, Pavlik!

You deserve the points.

Roland
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question