Har lige leget lidt videre med BertelBranders eksempel:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define ID_CHECKBUTTON1 1200
#define IDC_PUSH1 1210
#define IDC_PUSH2 1220
HINSTANCE InstanceHandle;
void ShowLastError(LPVOID caption)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, (char *)lpMsgBuf, (char *)caption, MB_ICONERROR );
LocalFree( lpMsgBuf );
}
void CreateRadio(HWND hwndDlg)
{
int i;
HWND temp;
HANDLE hFile;
char lpBuffer[70000];
int number;
DWORD dwRead;
DWORD dwSize;
DWORD dwError;
hFile = CreateFile( "
C://number.txt", // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
ShowLastError("CreateFile");
return;
}
dwSize = GetFileSize (hFile, NULL);
if (dwSize == INVALID_FILE_SIZE)
{
ShowLastError("GetFileSize");
CloseHandle(hFile);
return;
}
LPSTR pszFileText;
pszFileText = (LPSTR)GlobalAlloc(GPTR, dwSize + 1);
if(!ReadFile(hFile, pszFileText, dwSize, &dwRead, NULL))
{
ShowLastError("ReadFile");
CloseHandle(hFile);
return;
}
number = atoi(pszFileText);
GlobalFree(pszFileText);
CloseHandle(hFile);
for(i = 0; i < number; i++)
{
char Title[32];
sprintf(Title, "CheckMe %d", i);
temp = CreateWindow( "BUTTON",
Title,
WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,
10, 10 + i * 30, 90, 14,
hwndDlg,
(HMENU)(ID_CHECKBUTTON1 + i),
InstanceHandle,
NULL);
ShowWindow(temp, SW_SHOW);
}
}
void CreateButtons(HWND hwndDlg)
{
CreateWindow( "BUTTON",
"Create",
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
10, 320, 90, 20,
hwndDlg,
(HMENU)(IDC_PUSH1),
InstanceHandle,
NULL);
CreateWindow( "BUTTON",
"PushMe",
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
10, 360, 90, 20,
hwndDlg,
(HMENU)(IDC_PUSH2),
InstanceHandle,
NULL);
}
void OnButton(HWND hwndDlg)
{
int i;
char Msg[1023] = "State: ";
for(i = 0; i < 10; i++)
{
if(SendDlgItemMessage(hwndDlg, ID_CHECKBUTTON1 + i, BM_GETCHECK, 0, 0) == BST_CHECKED)
strcat(Msg, "1");
else
strcat(Msg, "0");
}
MessageBox(hwndDlg, Msg, "Whatever", MB_OK);
}
LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
CreateButtons(hwndDlg);
break;
case WM_COMMAND:
if(LOWORD(wParam) == IDC_PUSH2 && HIWORD(wParam) == BN_CLICKED)
{
OnButton(hwndDlg);
}
if(LOWORD(wParam) == IDC_PUSH1 && HIWORD(wParam) == BN_CLICKED)
{
CreateRadio(hwndDlg);
}
break;
}
return DefWindowProc(hwndDlg, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
if( !hPrevInstance )
{
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = DialogProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "WhateverClass";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if (RegisterClass( &wc ) == NULL)
{
ShowLastError("RegisterClass");
return false;
}
}
InstanceHandle = hInstance;
HWND WindowHandle = CreateWindow( "WhateverClass",
"Whatever",
WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION | WS_BORDER | WS_SYSMENU,
100, 100, 100, 410,
NULL,
NULL,
InstanceHandle,
0);
MSG Msg;
while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
{
if(!IsDialogMessage(WindowHandle, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return 0;
}