Solved

subclass edit control

Posted on 2012-03-11
5
810 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now