Click here to Skip to main content
15,881,281 members
Articles / Desktop Programming / MFC

How to Customize the PickIconDlg with Hook

Rate me:
Please Sign up or sign in to vote.
2.75/5 (4 votes)
17 Sep 2006CPOL3 min read 45.5K   785   11   4
A simple class to show the PickIconDlg. Now this dialog also shows index of selected icon
Sample Image - PickIconDlg2.jpg

Introduction

You can see my previous article on showing the pick icon dialog. I did not just update that article, because for those who only want to show this dialog, it is a simple solution. In this article, I am going to customize this dialog. Actually, all this started as I am working on a project which needs me to install a hook in a DLL. So as a start and simple way to know how hooks work, I created this and it worked.

Changes

The first change I made is that now it is a simple class which wraps all the hell of calling API, making a Unicode string and converting it back and the hooking.

Hooking the Dialog

What I want to do is to show the index of the currently selected icon on that dialog. For this purpose, I want to put another static control on the dialog. But where do I place that static control. First I tried to shrink the icon list box but could not succeed. For a brief discussion, see my blog. Then I made the actual dialog taller, and moved the buttons down. For all this, I set a hook with SetWindowsHookEx() of type WH_CALLWNDPROC. In this hook's procedure, I made my changes. I handled two messages WM_INITDIALOG and WM_COMMAND.

Why Hook

As most of you have seen, most window dialog boxes and other such APIs provide us a facility of callback. What are these? A callback is a function in our application (or DLL) which is called by Windows operating system. Why is it called? When setting a callback for a dialog, it is stated in that dialog's documentation what the purpose of that callback is. When is it called? This will also be stated there. For example, SHBrowseForFolder dialog also provides us a way to tap it by setting a callback in BROWSEINFO.lpfn member. After setting this callback, you will be able to easily know what is happening in that dialog.  But in the case of PickIconDlg(), I have no such option, meaning this dialog does not give us a proper way to know about its events. That is why I hooked it.

How Hook Works

The working of the hook process is not much difficult, Just set a hook, then show the dialog you want to be hooked, and remember to unhook after you finished, normally after dialog is dismissed. After a hook is set, it is the same as you have set a callback for that dialog. Now for the lifespan of that dialog, you can watch every event of that dialog from creation to destruction of that dialog. In that hook function which is told at the time, the hook is set (here CallWndProc() is the function which will be called).

C++
mhHook = SetWindowsHookEx(WH_CALLWNDPROC ,CallWndProc, NULL, AfxGetApp()->m_nThreadID);

You will receive all messages of that dialog. Now it is upon you which messages you want to handle and which you want to ignore.

Messages Handled

The WM_INITDIALOG message is the place in which I make all visual changes of the dialog, I make the dialog taller, then move the buttons (there are three buttons, not two. One is hidden.) down and place my newly created static between listbox and buttons.

C++
LRESULT CALLBACK CPickIconDialog::CallWndProc(int nCode,WPARAM wParam, LPARAM lParam)
{
	if (nCode < 0) // do not process message 
		return CallNextHookEx(mhHook, nCode, wParam, lParam);
	CWPSTRUCT *msg = (CWPSTRUCT *)lParam;
	switch(msg->message)
	{
	case WM_INITDIALOG:
	{
	// handle message here 

The WM_COMMAND message is actually handled only for a notification LBN_SELCHANGE, which is sent every time selection changes in listbox. Here I update my static control.

History

  • 18 Sep 2006: Last update

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
tanvon malik ( real name Tanveer Ahmad )I am a CNC Programmer, cnc setter, cnc operator. want to know about me visit my websites.
CNC Programming
CNC Manuals
DXF & GCode Files with Online Viewers
Komment.me

I been in Switzerland MAG former +FMS+ for CNC training.


Most interesting technologies I like COM MFC DirectShow such as filter development. I am from Pakistan.
Have worked on projects mostly related video capturing, video data processing and real time object tracking on videos. For these I have worked on projects which use "Open CV Library". Which is mostly used for capturing data and its processing.

Comments and Discussions

 
Generalgud 1 Pin
K edar V16-Sep-06 1:33
K edar V16-Sep-06 1:33 
GeneralRe: gud 1 Pin
tanvon malik16-Sep-06 2:21
tanvon malik16-Sep-06 2:21 
GeneralRe: gud 1 Pin
K edar V16-Sep-06 2:39
K edar V16-Sep-06 2:39 
GeneralRe: gud 1 Pin
tanvon malik16-Sep-06 21:32
tanvon malik16-Sep-06 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.