-
Try re-posting - the fourms are hosed. I see no post at ll.
:D
-
The heck happened?
Oh well, here's my problem:
This is a WinAPI application. I have a textbox and a pushbutton. The user clicks on it, and a message box would appear and say what the textbox says. Well my problem is this, what messages does my program need to catch in order to know that the user pushed the pushbutton? I know to use BM_CLICK, but can someone write it in code so the program knows what pushbutton I pressed?
-
Dang, you people are picky. Well anyway here's my code:
#include <windows.h>
#include "resource.h"
#include "zstedit.h"
#define CYA 9000
#define LATER 9001
#define SECOND_TIME 9002
const char g_szClassName[] = "myWindowClass";
HINSTANCE hinstance;
HWND hEdit, hButton;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
HFONT hfDefault;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, CYA, "Goodbye");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT) hSubMenu, "File");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, LATER, "Haha!");
AppendMenu(hSubMenu, MF_STRING, SECOND_TIME, "Hmmm!");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT) hSubMenu, "Edit");
SetMenu(hwnd, hMenu);
hEdit = CreateWindowEx(0, "EDIT", "",
WS_CHILD | WS_VISIBLE | ES_NUMBER |
ES_AUTOHSCROLL | WS_DLGFRAME,
20, 20, 200, 20, hwnd, NULL, hinstance, NULL);
if(!hEdit)
break;
hfDefault = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM) hfDefault, MAKELPARAM(FALSE,0));
hButton = CreateWindowEx(0, "BUTTON", "Push me",
WS_CHILD | WS_VISIBLE | BS_CENTER,
240, 20, 100, 20, hwnd, NULL, hinstance, NULL);
if(!hButton)
break;
SendMessage(hButton, WM_SETFONT, (WPARAM) hfDefault, MAKELPARAM(FALSE,0));
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case CYA:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case LATER:
MessageBox(hwnd, "Haha! Fooled Ya!", "Read below", 0);
break;
case SECOND_TIME:
MessageBox(hwnd, "Yea! It works!", "Read it and weep, antiprogs!", 0);
break;
}
/* my prob */ switch(HIWORD(wParam))
case BM_CLICK:
if (((HWND) lParam)==hButton)
{
char get[100];
GetWindowText(hEdit, get, 100);
MessageBox(hwnd, get, "Hello", 0);
break;
} /* my prob */
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
hinstance = hInstance;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
100, 100, 500, 250,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
-
This is the time we go on with OOP. Why don't we change our manner.
That is ever easy to be done just with MS VC++ 6.0.
In the MFC project wizard, choose the dialog-based appication.
Open the dialog box resource then add the two controls we needed: an edit box and a push button.
Press the combound key: Ctrl+W (Class wizard), then choose message tab. Select the ID of the push button in the list below, then choose the On_Click option in the next message list, then click OK. Select the 'Edit Code' button to go to the edit window, we type the following code in the On_?????_Click function
CString s;
CEdit* ed = (CEdit*) GetDialogItem(ID_BUTTON???);
s = ed->GetWindowText();
MessageBox(s, "Message Typed", MB_OK);
That is all.
Note: I have no ready VC++6.0 software on the computer i used to answer your question. So, my code may take some mistakes. Please correct it for me. Thank you.
-
In the future can you put your code in the correct tags, so we can read it better :)
Ok any way, you are close. The way I do it is to define a resource to it. You can do that by using define:
#define ID_BUTTONME 114
Then in your button creation:
hButton = CreateWindowEx(0, "BUTTON", "Push me",
WS_CHILD | WS_VISIBLE | BS_CENTER,
240, 20, 100, 20, hwnd, (HMENU)ID_BUTTONME , hinstance, NULL);
Then in your message handler:
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_BUTTONME:
break;
-
dubae: MFC was not made for those who don't know how to do API. It was made for those who know API and don't want to do the same things for every app they make.
To differ between different notifications, do a switch on HIWORD(wParam)
-
I still can't get it to work. I did this (even outside the WM_COMMAND case and any other switch and with no control-specific information, which I don't believe is required),
switch (HIWORD(wParam) {
case BM_CLICK:
{
//my code
}
}
, and it still didn't do anything when I pressed the button! I have no clue what to do in this matter! I really truly would like some sample code posted. Thanks.
-
Never mind! Figured it out! I was supposed to be using the notification messages, not the normal messages. I was supposed to use BN_CLICKED not BM_CLICK.
-
Just as a FYI, Microsoft are fairly consistent (I said fairly, before anyone starts picking holes) in their naming:
BN_CLICKED => BN = Button Notification
BM_CLICK => BM = Button Message
Chances are, you want to catch the version with the "N" in for everything, for example EN_CHANGED for an edit box...similarly, you send the message which is ?M_WHATEVER :)