-
EditBoxes
im having some problems with editboxes they dont seem to stay checked :rolleyes: how can i fix this?
Code:
#include <windows.h>
#define BOX0 500
#define BUTTON1 501
#define GBOX3 502
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Generate Random Cords"),
WS_CHILD|WS_VISIBLE|BS_CHECKBOX,
10,
10,
200,
50,
hwnd,
(HMENU)BOX0,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Start"),
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
250,
10,
100,
40,
hwnd,
(HMENU)BUTTON1,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Results"),
WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
100,
100,
400,
400,
hwnd,
(HMENU)GBOX3,
NULL,
NULL);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case BOX0:
MessageBox(NULL,"Checked","Message",MB_OK);
break;
return 0;
case BUTTON1:
if(BOX0 == BST_CHECKED)
{
MessageBox(NULL,"Box Checked","Message", MB_OK);
}
else
{
MessageBox(NULL,"box unchecked","Message",MB_OK);
}
break;
}
return 0;
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;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"CordGen",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
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;
}
-
Checkboxes are not 'editboxes'.
BS_AUTOCHECKBOX.
-
thanks, would you happen to know how i could make the window so it cant be resized by the user?
-
-
i got one last problem im having trouble with generating random numbers and sending them to the EditBox
Code:
#include <windows.h>
#include <ctime>
#include <iostream>
using namespace std;
#define BOX0 500
#define BUTTON1 501
#define GBOX3 502
#define EDIT5 503
#define BUTTON7 505
const char g_szClassName[] = "WindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Generate Random Cords"),
WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,
10,
10,
200,
50,
hwnd,
(HMENU)BOX0,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Start"),
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
250,
10,
100,
40,
hwnd,
(HMENU)BUTTON1,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Results"),
WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
10,
150,
350,
200,
hwnd,
(HMENU)GBOX3,
NULL,
NULL);
CreateWindowEx(0,
TEXT("EDIT"),
NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER,
30,
170,
300,
130,
hwnd,
(HMENU)EDIT5,
NULL,
NULL);
CreateWindowEx(0,
TEXT("BUTTON"),
TEXT("Quit"),
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
370,
10,
100,
40,
hwnd,
(HMENU)BUTTON7,
NULL,
NULL);
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case BUTTON1:
srand ((unsigned) time (0));
int number = rand()%250;
SetDlgItemText(hwnd,EDIT5,number);
break;
case BUTTON7:
SendMessage(hwnd,WM_CLOSE,0,0);
return 0;
break;
}
return 0;
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;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"CordGen",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
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;
}
Code:
--------------------Configuration: tesy - Win32 Release--------------------
Compiling...
main.cpp
C:\C++\tesy\main.cpp(90) : error C2664: 'SetDlgItemTextA' : cannot convert parameter 3 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\C++\tesy\main.cpp(92) : error C2360: initialization of 'number' is skipped by 'case' label
C:\C++\tesy\main.cpp(88) : see declaration of 'number'
Error executing cl.exe.
tesy.exe - 2 error(s), 0 warning(s)
-
If you wish to declare a variable in side a case you need to use brackets. If you want to set the text of a dialog box control to a number, you can use the SetDlgItemInt() function.
Code:
case BUTTON1:
{
srand ((unsigned) time (0));
int number = rand()%250;
SetDlgItemInt(hwnd,EDIT5,number);
break;
}
-
would you happen to know how i could add text to a editbox without deleating the old text?
-
-
Ummm........picky....
Check the button has recieved a BN_CLICKED msg.
case BUTTON1:
if (BN_CLICKED == HIWORD(wParam))
You should only 'seed' (srand) once, put it in the init function.
Else the random number will be the same if the time is the same. Probably not going to happen in this app.
edit: I think this another thread?