Solved

subclass edit control

Posted on 2012-03-11
5
840 Views
Last Modified: 2012-03-11
I am trying to learn how to subclass an edit control and intercept messages.

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "resource.h"
#include <Commctrl.h>
#pragma comment(lib, "Comctl32.lib")

HINSTANCE hInst;
LRESULT CALLBACK OwnerDrawButtonProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
                               LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
             HWND button = GetDlgItem(hwndDlg, IDC_EDIT1);
            SetWindowSubclass(button, OwnerDrawButtonProc, 0, 0);

            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION);
                    return TRUE;
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}

LRESULT CALLBACK OwnerDrawButtonProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
                               LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (uMsg)
    {
    case WM_PAINT:

        return TRUE;
    // Other cases...
    }
    return DefSubclassProc(hwndDlg, uMsg, wParam, lParam);
}

Open in new window



I am getting errors, and cant figure it out! Any ideas?
0
Comment
Question by:edvinson
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 37707842
There are a few issues, see the comments I left in the code - should compile now:

#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include "resource.h"

#define _WIN32_WINNT 0x501 // for 'SetWindowSubclass()'
#include <Commctrl.h>
#pragma comment(lib, "Comctl32.lib")

HINSTANCE hInst;
LRESULT CALLBACK OwnerDrawButtonProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
                               LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
        { // need a scope here to use a local variable in 'switch'
             HWND button = GetDlgItem(hwndDlg, IDC_EDIT1);
            SetWindowSubclass(button, OwnerDrawButtonProc, 0, 0);

            return TRUE;
        }

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION);
                    return TRUE;
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}

LRESULT CALLBACK OwnerDrawButtonProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
                               LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (uMsg)
    {
    case WM_PAINT:

        return TRUE;
    // Other cases...
    }
    return DefWindowProc(hwndDlg, uMsg, wParam, lParam); // there is no 'default subclass' proc, did you mean this one?
}
                                  

Open in new window

0
 
LVL 1

Author Comment

by:edvinson
ID: 37707894
Thanks JKR it compiles.

Can you please just point me to how to intercept a mouse over event on the edit?

Points will be awarded regardless, but that is ultimately my goal.

Thank you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 37707912
Are you thinking about a WM_MOUSEMOVE?
0
 
LVL 1

Author Comment

by:edvinson
ID: 37708137
Please look for next question from me.

It is unfair to combine my questions.

Thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 37708259
OK ;o)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

839 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