?
Solved

subclass edit control

Posted on 2012-03-11
5
Medium Priority
?
911 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
[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
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

719 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