While I agree that this is a poorly phrased (and researched!) question, there's an answer that comes straight from one of the project templates in my ide of choice, Code::Blocks. All I've done is add the button and the code to handle presses of this button.
It's been some 7 years or more since I last looked at DevC++ - I've no idea if it has a tchar.h, though I'd imagine it would. If it doesn't (and the code wont compile as a result), you can make a few fairly simple changes:
1. Remove the the code before
#include <windows.h>
and replace it with
#define UNICODE 1
2. Remove all instances of
_T()
. Replace this with
L
.
I.e
_T("You pressed button1.")
becomes
L"You pressed button1."
Finally, have a look into resource editors and dialog apps. I and many others use ResEdit to create the resource (.rc) file.
Note: You should link the following libraries: gdi32, user32, kernel32, comctl32
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#define IDC_BUTTON1 1000
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
_T("Code::Blocks Template Windows App"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
CreateWindow( _T("Button"), _T("Button1"), WS_VISIBLE|WS_CHILD, 20,20, 75,23, hwnd, (HMENU)IDC_BUTTON1, hThisInstance, NULL);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_COMMAND:
{
switch LOWORD(wParam)
{
case IDC_BUTTON1:
MessageBox(hwnd, _T("You pressed button1."), _T("Attention"), MB_ICONEXCLAMATION);
break;
}
}
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}