If you have any problems with compiling, ask in a compiler-specific
newsgroup.
If you have any problems getting the program to run, ask in a Windows-specific
newsgroup.
For C++ always #define STRICT and NOMINMAX before this #include.
Preferentially use
#include <cstring>
long WINAPI MainWndProc(HWND,UINT,WPARAM,LPARAM);
HWND hWnd;
Don't use global variables.
HWND hwndEdit;
HWND hwndButton;
HWND hwndClearButton;
char szMessage[50] = "";
Here you're into buffer overflow territory; instead consider
std::string.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Check out your compiler's support for standard 'main'. g++ supports
that directly for Windows GUI application. Use standard 'main'.
WNDCLASS wc;
wc.lpszClassName = "Style1";
wc.lpfnWndProc = MainWndProc;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
wc.lpszMenuName = "";
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
RegisterClass(&wc);
A simpler way might be to use an initializer, "WNDCLASS wc = { ... };".
Anyway, to ensure all non-assigned fields are zero, declare it like
WNDCLASS wc = {0};
hWnd = CreateWindow("Style1","Border Demonstration",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
185,265,NULL,NULL,hInstance,NULL);
hwndEdit = CreateWindow("EDIT",NULL,
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
10,10,155,20,hWnd,NULL,hInstance,NULL);
hwndButton = CreateWindow("BUTTON","Message",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10,40,35,35,hWnd,NULL,hInstance,NULL);
hwndClearButton = CreateWindow("BUTTON","Clear",
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
50,40,35,35,hWnd,NULL,hInstance,NULL);
Error checking might be a good idea... For example using a function
template< typename T >
inline void throwIf0( T const& x )
{
if( !x ) { throw std::runtime_error( "Ooops." ); }
}
Then
hWnd = CreateWindow("Style1","Border Demonstration",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
185,265,NULL,NULL,hInstance,NULL);
throwIf0( hWnd );
of course with a 'catch' somewhere.
ShowWindow(hWnd,nCmdShow);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
Here should be cast to the return type, i.e.
}//end WinMain()
long WINAPI MainWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM
lParam)
{
HWND hwndCtl = (HWND)lParam;
Don't use C style casts.
In this case, use a reinterpret_cast or the appropriate system-specific
macro/function (off-topic hint: <windowsx.h>).
Anyway the logic is flawed: you don't yet have enough information to
say that this argument represents what you think it does. But it works.
With a bit of maintainance it will cease to work.
switch(msg)
{
case WM_COMMAND:
switch(wParam)
{
case BN_CLICKED:
if(hwndCtl == hwndButton)
{
strcpy(szMessage,"This program is an overlapped style Window.");
Buffer overflow?
Indentation!
SetWindowText(hwndEdit,szMessage);
}
else if(hwndCtl == hwndClearButton)
{
strcpy(szMessage,"");
SetWindowText(hwndEdit,szMessage);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
Unreachable.
}//end MainWndProc()