Pelles C forum
C language => Windows questions => Topic started by: tpekar on June 28, 2011, 10:39:09 PM
-
I tried following an example from theForger. But I must be doing something wrong. Here is my code:
#include <windows.h>
#include <stdio.h>
#define ID_MYBUTTON 101
#define ID_TAB 201
#define WM_CTLCOLORDLG 0x0136
#define F1 3501
#define IDC_LIST 1002
LPSTR szClassName = "MyClass";
HINSTANCE hInstance;
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwndText[3];
HWND hwndList;
HANDLE hinstAcc;
FILE *fp;
int update=0;
int iShow;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
WNDCLASS wnd;
MSG msg;
HWND hwnd;
HWND hWndBtn1;
HACCEL haccel; // handle to accelerator table
HDC hdc;
iShow=iCmdShow;
hInstance = hInst;
wnd.style = CS_HREDRAW | CS_VREDRAW; //we will explain this later
wnd.lpfnWndProc = MyWndProc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hInstance = hInstance;
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //default icon
wnd.hCursor = LoadCursor(NULL, IDC_ARROW); //default arrow mouse cursor
wnd.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wnd.lpszMenuName = NULL; //no menu
wnd.lpszClassName = szClassName;
if(!RegisterClass(&wnd)) //register the WNDCLASS
{
MessageBox(NULL, "This Program Requires Windows NT",
"Error", MB_OK);
return 0;
}
hwnd = CreateWindow(szClassName,
"Window Title",
WS_OVERLAPPEDWINDOW, //basic window style
CW_USEDEFAULT,
CW_USEDEFAULT, //set starting point to default value
CW_USEDEFAULT,
CW_USEDEFAULT, //set all the dimensions to default value
NULL, //no parent window
NULL, //no menu
hInstance,
NULL); //no parameters to pass
// create a text box and store the handle
hwndText[0] = CreateWindow(
TEXT("edit"), // The class name required is edit
TEXT("Enter text here"), // Default text.
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, // the styles
100,100, // the left and top co-ordinates
300,30, // width and height
hwnd, // parent window handle
(HMENU) ID_TAB, // the ID of your combobox
hInstance, // the instance of your application
NULL
); // extra bits you dont really need
hwndText[1] = CreateWindow(
TEXT("edit"), // The class name required is edit
TEXT("2nd field"), // Default text.
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_PASSWORD, // the styles
100,200, // the left and top co-ordinates
300,30, // width and height
hwnd, // parent window handle
NULL, // the ID of your combobox
hInstance, // the instance of your application
NULL
); // extra bits you dont really need
hwndList = CreateWindow(
TEXT("listbox"), // The class name required is edit
NULL, // Default text.
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | WS_VSCROLL /*| LBS_NOTIFY */, // the styles
100,300, // the left and top co-ordinates
300,300, // width and height
hwnd, // parent window handle
NULL, // the ID of your combobox
hInstance, // the instance of your application
NULL
); // extra bits you dont really need
int index= SendDlgItemMessage(hwndList,IDC_LIST, LB_ADDSTRING, 0, (LPARAM)"Hi there!");
SetWindowText(hwndText[0],"revised");
SetFocus(hwndText[0]);
hdc=(HDC)hwndText[1];
SetTextColor(hdc,RGB(255,255,255));
CreateWindow(TEXT("STATIC"), TEXT("Text1:"), WS_CHILD|WS_VISIBLE, 25, 100, 55, 22, hwnd, 0, hInstance, 0);
hWndBtn1 = CreateWindow(TEXT("BUTTON"), TEXT("Update"), WS_CHILD|WS_VISIBLE, 5, 5, 55, 22, hwnd, (HMENU) ID_MYBUTTON, hInstance, 0);
ShowWindow(hwnd, iCmdShow); //display the window on the screen
UpdateWindow(hwnd); //make sure the window is updated correctly
haccel=LoadAccelerators(hinstAcc,"test");
if (haccel == NULL)
MessageBox(hwnd,"accelerator could not load","error",MB_OK);
while(GetMessage(&msg, NULL, 0, 0)) //message loop
{
if (!TranslateAccelerator(hwnd,haccel,&msg)) {
if (!IsDialogMessage(hwnd, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return msg.wParam;
}
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
char data[256];
HDC hdc;
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam)) {
case F1:
MessageBox(hwnd,"F1 was pressed","notice",MB_OK);
break;
default:
break;
case IDC_LIST:
// It's our listbox, check the notification code
switch(HIWORD(wParam))
{
case LBN_SELCHANGE:
// Selection changed, do stuff here.
break;
}
break;
}
if(ID_MYBUTTON == LOWORD(wParam))
{
if (BN_CLICKED==HIWORD(wParam)) {
if ((fp=fopen("wikiwin.txt","w"))==NULL)
MessageBox(hwnd,"could not open wikiwin.txt","error",MB_OK);
fprintf(fp,"%s\n",data);
fclose(fp);
ShowWindow(hwndText[0],SW_HIDE); // hide the window
ShowWindow(hwndText[1],SW_HIDE); // hide the window
ShowWindow(hwndText[0],SW_SHOW); // show the hidden window again
ShowWindow(hwndText[1],SW_SHOW); // show the hidden window again
SetFocus(hwndText[1]);
PostMessage(hwndText[1],WM_CTLCOLOREDIT,(int)hwndText[1],(long)hwndText[1]);
if (update==0) update=1;
else update=0;
}
}
case WM_CTLCOLOREDIT:
if (update>0) {
hdc = (HDC)wParam;
SetTextColor(hdc, RGB(0, 0, 0)); // black
SetBkColor(hdc, RGB(255, 0, 0)); // red
return (LRESULT)CreateSolidBrush(RGB(255, 0, 0)); // red
}
ShowWindow(hwndText[1],SW_SHOW);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
-
Wrong...
int index= SendDlgItemMessage(hwndList,IDC_LIST, LB_ADDSTRING, 0, (LPARAM)"Hi there!");
Right... (or at least less wrong :D )
SendMessage(hwndList,LB_ADDSTRING,0,(LPARAM)"This is a test");
The difference is that SendDlgItemMessage() needs the handle of a dialog box and the index of the control. From that it figures out the handle of the control itself and then uses SendMessage() to finish the task. When you are not in a dialog box, you need to use the control's handle directly with SendMessage()... (This is, of course, why I suggested saving the window handles globally, in an earlier message)
-
Thanks. That worked.