Solved

subclass edit control

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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