Appending text to an edit control running out of room? - c++

So here is my problem, it works fine until it reaches a certain amount of data. I can't show a whole html file for example that is about 1MB in the window it gets cut off.
Window:
case WM_CREATE:
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_READONLY | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100, hWnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
MessageBox(hWnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
LogText(hEdit,logstring);
break;
Function:
void LogText(HWND hEdit, const char * logstring)
{
const char * logstring2="\r\n";
int iLength = GetWindowTextLength(hEdit);
SendMessage(hEdit, EM_SETSEL, iLength, iLength);
SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM) logstring);
SendMessage(hEdit, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);
int iLength2 = GetWindowTextLength(hEdit);
SendMessage(hEdit, EM_SETSEL, iLength2, iLength2);
SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM) logstring2);
SendMessage(hEdit, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);
}
Usage:
logstring="Test";
LogText(hEdit, logstring);
Like I said, it works perfect, except it stops working after its spit out an unknown amount of data which I've noticed is a very consistent number.

You can change the text limit of an edit box with the EM_SETLIMITTEXT message. Just define the upper limit before you attempt to assign text to the edit box.

Related

How do I create two different comboboxes with different strings of items?

I have created a combo box having a list of items. How do I create another one with different strings of items? Can I in anyway change the hWnd because it seems the first already has the hWndCombobxes. Thus, when I apply it to the second, I get an error message, indicating that there is a duplicate value.
Below is the code I have. What function should I call else?
case WM_CREATE: {
HWND hWndComboBox = CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 150, 200, 150,
hwnd ,(HMENU) ID_COMBOBOX 1, NULL, NULL);
// ADD 2 ITEMS
SendMessage (
hWndComboBox,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 1"));
SendMessage (
hWndComboBox ,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 2"));
// SEND THE CB_SETCURSEL MESSAGE TO DISPLAY AN INITIAL ITEM IN SELECTION FIELD
SendMessage (hWndComboBox , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);
// put this declaration somewhere up (or better move it to an include file)
#define ID_COMBOBOX_1 1001
#define ID_COMBOBOX_2 1002
// end defines
case WM_CREATE: {
// it is preferably to use SendDlgItemMessage instead of SendMessage
// this make things easier
// you will not need combobox's HWND, just ComboBox ID
TCHAR *Combo_1_Data[]={
TEXT("Item 1"),
TEXT("Item 2")
};
TCHAR *Combo_2_Data[]={
TEXT("Element 1"),
TEXT("Element 2")
};
int i;
// create two different ComboBoxs
CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 150, 200, 150,
hwnd ,(HMENU) ID_COMBOBOX_1, NULL, NULL);
CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
208, 150, 200, 150,
hwnd ,(HMENU) ID_COMBOBOX_2, NULL, NULL);
// Fill first Combo with its Data
for( i = 0 ; i < (sizeof(Combo_1_Data) / sizeof(Combo_1_Data[0]) ) ; i++ ){
SendDlgItemMessage (hwnd,ID_COMBOBOX_1 ,CB_ADDSTRING, 0, (LPARAM) Combo_1_Data[i]);
}
SendDlgItemMessage (hwnd, ID_COMBOBOX_1 , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);
// Fill second Combo with its Data
for( i = 0 ; i < (sizeof(Combo_2_Data) / sizeof(Combo_2_Data[0] )) ; i++ ){
SendDlgItemMessage (hwnd,ID_COMBOBOX_2 ,CB_ADDSTRING, 0, (LPARAM) Combo_2_Data[i]);
}
SendDlgItemMessage (hwnd, ID_COMBOBOX_2 , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);
Copy and paste the other combobox. Do the same for the 'Send message' function. Then change the hWnd of the second Combobox to hWndListBox. Do the same to 'Send message'.
HWND hWndListBox = CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 70, 200, 90,
hwnd ,(HMENU) NULL, NULL, NULL);
HWND hWndComboBox = CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 150, 200, 100,
hwnd ,(HMENU) NULL, NULL, NULL);
SendMessage (
hWndComboBox ,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 2"));
SendMessage (
hWndListBox ,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 1"));
// SEND THE CB_SETCURSEL MESSAGE TO DISPLAY AN INITIAL ITEM IN SELECTION FIELD
SendMessage (hWndComboBox , CB_SETCURSEL , (WPARAM) 0, (LPARAM) 0);
SendMessage (hWndListBox , CB_SETCURSEL , (WPARAM) 0, (LPARAM) 0);

check radiobutton state winapi

I use mingw32 compiler.
I created two radioButton:
radio1 = CreateWindowExW(WS_EX_TRANSPARENT , L"BUTTON", L"RadioButton1",
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON ,
0, 0, 0, 0,
hwnd, (HMENU)ID_RADIOBTN1,
GetModuleHandle(NULL), 0);
SendMessage(radioBtnDaily, WM_SETFONT, (WPARAM) font, TRUE);
radio2 = CreateWindowExW(WS_EX_TRANSPARENT , L"BUTTON", L"RadioButton2",
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON ,
0, 0, 0, 0,
hwnd, (HMENU)ID_RADIOBTN2,
GetModuleHandle(NULL), 0);
SendMessage(radio1, BM_SETCHECK , (WPARAM) font, TRUE);
SendMessage(radio2, WM_SETFONT, (WPARAM) font, TRUE);
And I handle the check state, something like this:
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_RADIOBTN1:
{
MessageBoxW( NULL, "radio1 is checked", L"radio", MB_OK | MB_ICONERROR );
}
break;
case ID_RADIOBTN2:
{
MessageBoxW( NULL, "radio2 is checked", L"radio", MB_OK | MB_ICONERROR );
}
break;
....
But this always run when I click the radiobutton so it not check the state.
How to check the state without use the click event?
Use IsDlgButtonChecked:
if(IsDlgButtonChecked(hwnd, ID_RADIOBTN1))
MessageBox(NULL, "radio1 is checked")
else
MessageBox(NULL, "radio1 is not checked")
Use the BM_GETCHECK message, or the Button_GetCheck() macro, to get the radio button's actual state, eg:
radio1 = CreateWindowExW(WS_EX_TRANSPARENT , L"BUTTON", L"RadioButton1",
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON ,
0, 0, 0, 0,
hwnd, (HMENU)ID_RADIOBTN1,
GetModuleHandle(NULL), 0);
SendMessage(radio1, WM_SETFONT, (WPARAM) font, TRUE);
radio2 = CreateWindowExW(WS_EX_TRANSPARENT , L"BUTTON", L"RadioButton2",
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON ,
0, 0, 0, 0,
hwnd, (HMENU)ID_RADIOBTN2,
GetModuleHandle(NULL), 0);
SendMessage(radio2, WM_SETFONT, (WPARAM) font, TRUE);
.
case WM_COMMAND:
if ((HIWORD(wParam) == BN_CLICKED)
{
switch(LOWORD(wParam))
{
case ID_RADIOBTN1:
{
switch (Button_GetCheck(radio1))
{
case BST_CHECKED:
MessageBoxW( NULL, L"radio1 is checked", L"radio", MB_OK );
break;
case BST_INDETERMINATE:
MessageBoxW( NULL, L"radio1 is indeterminate", L"radio", MB_OK );
break;
case BST_UNCHECKED:
MessageBoxW( NULL, L"radio1 is unchecked", L"radio", MB_OK );
break;
}
}
break;
case ID_RADIOBTN2:
{
switch (Button_GetCheck(radio2))
{
case BST_CHECKED:
MessageBoxW( NULL, L"radio2 is checked", L"radio", MB_OK );
break;
case BST_INDETERMINATE:
MessageBoxW( NULL, L"radio2 is indeterminate", L"radio", MB_OK );
break;
case BST_UNCHECKED:
MessageBoxW( NULL, L"radio2 is unchecked", L"radio", MB_OK );
break;
}
}
break;
}
}
break;
Well that depends on what event you want to check the status of the button so you need some sort of interaction (this can be another event like window minimized, resized etc or another thread checking but not modifying the state of your control)
It you need to test the state of the radio button, you can use (in pascal):
if SendMessage(hradiobutton, BM_GETCHECK, 0, 0) = BST_CHECKED
(this for checked state).
You can replace BST_CHECKED with all the constants listed at:
Documentation from Microsoft
Please, forgive my errors.

What could cause my C++ win32 app to run in the background?

I have been working on this app for at least 3-4 months and just recently it hasn't been working.
It started today, because yesterday it was running fine and smooth.
Now it runs, but the window doesn't become visible(I checked the process list in Task Manager and it was running). I have made sure that the
ShowWindow(hwnd, nFunsterStil); was not accidentally deleted, but it is still there.
I believe the problem to be something with WM_PAINT because that is what I was working on when the program stopped working.
This is what I had when it compiled and first started being invisible:
PAINTSTRUCT ps;
HDC hdc;
case WM_PAINT: {
hdc = BeginPaint(hwnd, &ps);
static RECT Background, Background2;
SetRect(&Background, 0, 0, 476, 555);
SetRect(&Background2, 300, 300, 200, 400);
FillRect(hdc, &Background, (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(50,55,50)));
FillRect(hdc, &Background2, (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(100,200,100)));
EndPaint(hwnd, &ps);
}
Now I tried getting rid of some of the lines in there, making the whole block into a comment, and even entirely deleting it but it did not help anything. I know I probably messed something up with my computer's memory. So my question is: What is wrong, and How can I fix it? Thanks in advance for any help.
Here is the full Message Switch:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CTLCOLORSTATIC: {
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(50,255,50));
SetBkColor(hdcStatic, RGB(0,0,0));
return (INT_PTR)CreateSolidBrush(RGB(0, 0, 0));
}
case WM_CTLCOLOREDIT: {
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(50,255,50));
SetBkColor(hdcStatic, RGB(0,0,0));
return (INT_PTR)CreateSolidBrush(RGB(0, 0, 0));
}
case WM_CREATE:
{
// ------------------MENU-------------------
HMENU hMenubar = CreateMenu();
HMENU hFile = CreatePopupMenu();
HMENU hCPP = CreatePopupMenu();
HMENU hHELP = CreatePopupMenu();
HMENU hCUSTOMIZE = CreatePopupMenu();
HMENU hTHEME = CreatePopupMenu();
HMENU hNEW = CreatePopupMenu();
AppendMenu(hFile, MF_STRING, ID_HOME, "Ho&me");
AppendMenu(hFile, MF_SEPARATOR, 0, 0);
AppendMenu(hFile, MF_POPUP, (UINT) hNEW, "N&ew...");
AppendMenu(hNEW, MF_STRING, ID_NEW, "Not&e");
AppendMenu(hFile, MF_STRING, ID_SAVE, "Sa&ve");
AppendMenu(hFile, MF_STRING, ID_SHOWCPP, "Ed&it");
AppendMenu(hFile, MF_SEPARATOR, 0, 0);
AppendMenu(hFile, MF_STRING, ID_CLOSE, "Cl&ose");
AppendMenu(hCPP, MF_STRING, ID_SHOWNOTES, "Li&st all notes");
AppendMenu(hCPP, MF_STRING, MENU_COREFILES, "Open 'Notes' fol&der");
AppendMenu(hCPP, MF_SEPARATOR, 0, 0);
AppendMenu(hCPP, MF_STRING, ID_SAVEFILE, "Sa&ve");
AppendMenu(hCPP, MF_STRING, ID_SHOWCPP, "Ed&it");
AppendMenu(hHELP, MF_STRING, 0, "Help Fil&es...");
AppendMenu(hHELP, MF_STRING, MENU_ABOUT, "Ab&out");
AppendMenu(hHELP, MF_SEPARATOR, 0, 0);
AppendMenu(hHELP, MF_STRING, 0, "Pro&duct Website");
AppendMenu(hCUSTOMIZE, MF_POPUP, (UINT) hTHEME, "Current& Theme");
AppendMenu(hTHEME, MF_STRING | MF_CHECKED, 0, "Pr&ompt");
AppendMenu(hTHEME, MF_STRING, 0, "No&te Pad");
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hFile, "Fi&le");
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hCPP, "No&tes");
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hHELP, "He&lp");
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_SEPARATOR, 0, 0);
AppendMenu(hMenubar, MF_STRING | MF_POPUP, (UINT) hCUSTOMIZE, "Custom&ise");
SetMenu(hwnd, hMenubar);
// WINDOW FEATURES!!!
TEST[1] = CreateWindow(TEXT("BUTTON"), TEXT("List all notes!"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
200,375,150,25,
hwnd,(HMENU) ID_SHOWNOTES, NULL, NULL);
MAIN = CreateWindow(TEXT("edit"), TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_READONLY | SBS_RIGHTALIGN | WS_VSCROLL,
10,20,516,350,
hwnd,(HMENU) 5001, NULL, NULL);
EDIT = CreateWindow(TEXT("edit"), TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | SBS_RIGHTALIGN | WS_VSCROLL,
10,20,516,350,
hwnd,(HMENU) 5001, NULL, NULL);
ShowWindow(EDIT, SW_HIDE);
NAME=CreateWindow(TEXT("edit"), TEXT("-> Title <-"),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
10, 0, 516, 20,
hwnd, (HMENU) ID_FILENAME, NULL, NULL);
ShowWindow(NAME, SW_HIDE);
TEST[2] = CreateWindow(TEXT("BUTTON"), TEXT("-> Home <-"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
200,375,150,25,
hwnd,(HMENU) ID_SHOWHOME, NULL, NULL);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(MAIN, DEFTEXT);
break;
}
case WM_COMMAND: {
// MENU ITEMS!!!
//Close
if( LOWORD(wParam) == ID_CLOSE ) {
PostQuitMessage (0);
}
if(LOWORD(wParam) == MENU_COREFILES) {
ShellExecute(NULL, "explore", "Notes", NULL, NULL, SW_SHOW);
}
if(LOWORD(wParam) == MENU_ABOUT) {
MessageBox(hwnd, "© 2012 Joseph Meadows. \r\nThis program is essentially a reference guide for whatever you want. It can be used to store school notes, or things completely opposite.", "About:",
MB_ICONINFORMATION | MB_SYSTEMMODAL | MB_OK);
}
if(LOWORD(wParam) == ID_SHOWCPP) {
if(home==3) {
MessageBox(hwnd, "Error: 9026. Cannot edit Home Page.", "Invalid Action!",
MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_OK);
}
else if(home==1) {
MessageBox(hwnd, "Error: 6502 Cannot edit System pages.", "Invalid Action!",
MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_OK);
}
else {
ShowWindow(EDIT, SW_SHOW);
ShowWindow(NAME, SW_SHOW);
ShowWindow(MAIN, SW_HIDE);
SetWindowText(StatButts[0],"List all notes!");
mode=2;
}
}
if(LOWORD(wParam) == ID_SAVE) {
char* tittle=SaveFile(hwnd, NAME, EDIT);
SetWindowText(MAIN, tittle);
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
mode=1;
}
if(LOWORD(wParam) == ID_HOME) {
if(mode==2){
int Savebox=MessageBox(hwnd, "Would you like to save this page?", "Save Page",
MB_ICONQUESTION | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_YESNOCANCEL);
switch(Savebox){
case IDYES: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
SetWindowText(StatButts[0],"List all notes!");
int len = GetWindowTextLength(EDIT)+1;
static char title[1000];
GetWindowText(EDIT, title, len);
SetWindowText(MAIN, title);
mode=1;
home=1;
break;
}
case IDNO: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
SetWindowText(StatButts[0],"List all notes!");
SetWindowText(MAIN, DEFTEXT);
mode=1;
home=1;
}
case IDCANCEL: {}
}
}
if(mode==1){
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
SetWindowText(MAIN, DEFTEXT);
SetWindowText(StatButts[0],"List all notes!");
}
}
if(LOWORD(wParam)==ID_NEW) {
if(mode==1) {
ShowWindow(EDIT, SW_SHOW);
ShowWindow(NAME, SW_SHOW);
ShowWindow(MAIN, SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
SetWindowText(EDIT, "");
mode=2;
}
}
if(LOWORD(wParam)==ID_SHOWHOME) {
if(mode==1) {
ShowWindow(MAIN, SW_SHOW);
ShowWindow(NAME, SW_HIDE);
ShowWindow(EDIT, SW_HIDE);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
home=1;
SetWindowText(MAIN, DEFTEXT);
SetWindowText(TEST[1], "List all notes!");
}
if(mode!=1) {
int Savebox=MessageBox(hwnd, "Would you like to save this page?", "Save Page?",
MB_ICONQUESTION | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_YESNOCANCEL);
switch(Savebox){
case IDYES: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
int len = GetWindowTextLength(EDIT)+1;
static char title[1000];
GetWindowText(EDIT, title, len);
SetWindowText(MAIN, title);
mode=1;
home=1;
break;
}
case IDNO: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
mode=1;
home=1;
}
}
}
}
if(LOWORD(wParam)==ID_SHOWNOTES) {
if(mode==1) {
std::string umm="";
ShowWindow(NAME,SW_SHOW);
ShowWindow(MAIN,SW_SHOW);
ShowWindow(EDIT,SW_HIDE);
ShowWindow(TEST[1], SW_HIDE);
ShowWindow(TEST[2], SW_SHOW);
SetWindowText(NAME, "-> Title <-");
WIN32_FIND_DATA File_Buffer;
HANDLE hFind = FindFirstFile(TEXT("Notes/*.NNF"), &File_Buffer);
PTSTR Files[8999];
int num=0;
SetWindowText(MAIN, "");
if(hFind != INVALID_HANDLE_VALUE) {
do {
PTSTR FileName = File_Buffer.cFileName;
Files[num] = FileName;
num=num+1;
string kkk(FileName);
stringstream nam(kkk);
string name;
getline(nam, name, '.');
umm = Stringadd(umm, name);
//================================================================================================
} while (FindNextFile(hFind, &File_Buffer));
std::stringstream out;
out << num;
std::string numb=out.str();
umm=umm+" \r\n \r\n "+"Number of Notes: "+numb;
const char * lol=umm.c_str();
SetWindowText(MAIN, lol);
FindClose(hFind);
}
}
if(mode!=1) {
int Savebox=MessageBox(hwnd, "Would you like to save this page?", "Save Page",
MB_ICONQUESTION | MB_SYSTEMMODAL | MB_DEFBUTTON1 | MB_YESNOCANCEL);
switch(Savebox){
case IDYES: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
int len = GetWindowTextLength(EDIT)+1;
static char title[1000];
GetWindowText(EDIT, title, len);
SetWindowText(MAIN, title);
mode=1;
home=1;
break;
}
case IDNO: {
ShowWindow(EDIT, SW_HIDE);
ShowWindow(NAME, SW_HIDE);
ShowWindow(MAIN, SW_SHOW);
ShowWindow(TEST[1], SW_SHOW);
ShowWindow(TEST[2], SW_HIDE);
SetWindowText(TEST[1],"List all notes!");
mode=1;
home=1;
}
}
}
}
// BUTTONS!!!
//Show CPP
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
static RECT Background;
SetRect(&Background, 0, 0, 476, 555);
FillRect(hdc, &Background, (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(50,55,50)));
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
default: /* for messages that we don't deal with */
break;
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
And here is the parts relevant to making the window:
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd, hwnd2; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) (INT_PTR)CreateSolidBrush(RGB(0,0,0));
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"VirtualNotes", /* Title Text */
WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
554, /* The programs width */
475, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-
You should perform an investigation to identify the problem. Then - ask for the solution.
In case you're creating a standard "overlapped" window (i.e. with title bar, system menu and min/max/close buttons) - all of the above should be drawn by the system, only the client area (i.e. the internals of the window) is drawn within WM_PAINT handling. So that if you see nothing on the screen - the problem is probably elsewhere.
There's a nice tool within the MSVC, called Spy++. Using it find your process/thread, check if it *really created the window, check your window style and placement.
You may also add a breakpoint within your WM_PAINT processing. You'll get there iff your window indeed exists and visible.
Some notes regarding your code:
No error checking (such as testing retval of BeginPaint). How do you know your window is indeed created (or at least its class is registered)? At least some debug-time macro (such as ASSERT) would be helpful.
If there a reason Why are you using static RECT? (in some very specific cases this may save some stack memory).
CreateSolidBrush returns you a brush handle, which you must free by DeleteObject. You have a resource leak actually.
Looks like you are missing a break at the end of your case.
When you fill your WNDCLASS struct, register and create your main window, you'll then call ShowWindow(handle, SW_SHOW/SW_HIDE); presumably. Make sure this call isn't saying SW_HIDE
Given that this section of your code isn't show in your current post I could be wrong but it would be a wise place to look.
EDIT: On line 55ish, ShowWindow(); change nFunsterStil to "SW_SHOW" and see if that solves your problem.
#Joseph

Wrong symbols in the buffer using GetWindowText

There is combo box with two items and button on the main window. Combobox:
HWND hCombo;
hCombo = CreateWindow(L"COMBOBOX", L"combobox",
WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST,
10, 55, 232, 500, hWnd, 0, hInstance, 0);
const wchar_t *langEnglish = L"English";
const wchar_t *langRussian = L"Russian";
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langEnglish);
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langRussian);
SendMessage(hCombo, CB_SETCURSEL, 0, 0);
I am trying to get selected item text in the WndProc by clicking on the button:
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BUTTON_OK:
wchar_t buf[10];
hCombo = GetDlgItem(hWnd, IDC_COMBO);
GetDlgItemText(hCombo, IDC_COMBO, (LPWSTR)buf, 10);
MessageBox(hWnd, (LPCWSTR)buf, NULL, MB_OK);
break;
}
} break;
I am using breakpoint in the MSVS2010 to see buf variable. It contains chinese symbols!!! Message box shows empty message (With the title "Error"). I want to see english text. What is wrong?
This code
nIndex = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
SendMessage(hCombo, CB_GETLBTEXT, nIndex, (LPARAM)buf);
fills buf with the same chinese symbols
SOLUTION:
hCombo = CreateWindow(L"COMBOBOX", L"combobox",
WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST,
10, 55, 232, 500, hWnd, (HMENU)IDC_COMBO, hInstance, 0);
In order to get currently selected item from CBS_DROPDOWNLIST styled combo box you need CB_GETCURSEL to get selection index and then CB_GETLBTEXT to get the string.

problem initializing global variables

I have recently begun learning the Win32 API using this tutorial:
http://www.winprog.org/tutorial/ (though I'm using C++, not C as in the tutorial)
I'm currently experimenting with the "edit box"-function where I'm trying to compare the text written in the edit box with another line of characters.
Code:
#define IDC_MAIN_EDIT 101
Code:
case WM_CREATE:
{
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
hfDefault = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
break;
case WM_SIZE:
{
HWND hEdit;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
break;
Code:
bool comparison (HWND hEdit) {
LPWSTR pszText;
DWORD dwTextLength;
DWORD dwBufferSize;
dwTextLength = GetWindowTextLength(hEdit);
dwBufferSize = dwTextLength + 1;
GetWindowText(hEdit, pszText, dwBufferSize);
if(pszText == TEXT("3")) {
return true;
}
else {
return false;
}
}
The problem when I call the "comparison"-function is that pszText and hEdit aren't initialized. I get why pszText isn't and I've tried using the new/delete to fix it, but I don't get it to work. I have no clue about hEdit. Am I perhaps using the GetWindowText-function wrong?
Warnings
Code:
warning C4700: uninitialized local variable 'pszText' used
warning C4700: uninitialized local variable 'hEdit' used
Run-Time Check Failure (appear when I'm using the function, and this is just one of them)
Code:
Run-Time Check Failure #3 - The variable 'hEdit' is being used without being initializ
pszText is a pointer type. So you'need to allocate memory to it before you use it.
Do this:
wchar_t *pszText = new wchar_t[size]; //calculate or guess `size`
Yes, you must allocate a buffer for GetWindowText() to store its data inside. The "LP" in LPWSTR means that the variable is actually a pointer and not an allocated object.
This style is called "Hungarian notation" and in my own personal opinion, its only use is to hide away fundamental C syntax from the programmer, in order to create more bugs.
Look into the very important concept of scope. refer this page http://www.cplusplus.com/doc/tutorial/variables/
c++ Code:
case WM_CREATE:
{
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
hfDefault = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
The variable hEdit only exists for the duration of the block in which it's declared, which is within the {}s for the WM_CREATE case.