SetWindowText

Started by
6 comments, last by mutex 17 years, 2 months ago
I don't know why but whenever i use SetWindowText() in the case statement for my menu, it doesn't work.. heres my code:

//Include our include file
#include "resource.h"
#include "includes.h"

//about dialog window
BOOL CALLBACK dgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
     switch (msg) {
            case WM_INITDIALOG:
                 return TRUE;
                 break;
                 
            case WM_COMMAND:
                 switch (LOWORD(wParam)) {
                        case IDOK:
                             EndDialog(hwnd, IDOK);
                             break;
                             
                 }
                 
            default:
                    return FALSE;     
     }
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
        switch (msg) {
               //create
               case WM_CREATE:
                    //somevariables
                    HWND edit;
                    //create our edit control
                    edit = 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)E,
                    GetModuleHandle(NULL),
                    NULL);
                    break;
                    
               //resize
               case WM_SIZE:
                    edit = GetDlgItem(hwnd, E);
                    SetWindowPos(edit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_NOZORDER);
                    break;
                    
               //close
               case WM_CLOSE:
                    PostQuitMessage(0);
                    break;
                    
               //menu
               case WM_COMMAND:
                    switch (LOWORD(wParam)) {
                           case IDM_ABOUT:
                                DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, dgProc);
                                break;
                           
                           case IDM_OPEN:
                                //set ofn to open
                                win.ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                                
                                //open diaolog
                                GetOpenFileName(&win.ofn);
                                
                                //load file
                                LoadTXT(edit, win.fileStr);
                                break;
                                
                           case IDM_EXIT3:
                                PostQuitMessage(0);
                                
                    }
                    break;
                    
               default:
                       return DefWindowProc(hwnd, msg, wParam, lParam);
        }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmd, int show) { 
    //make the window
    win.makeWindow(hInstance, "G - Text");
    
    //show the window
    win.showWindow(show);
    
    //Message loop
    while(GetMessage(&win.msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&win.msg);
        DispatchMessage(&win.msg);
    }          
}

and heres the loadTXT function

//here is some misc functions

//load text file
bool LoadTXT(HWND edit, char* FileName) {
     //create file handle
     FILE *file;
     file = fopen(FileName, "r");
     long size;
     char *buffer;
     
     //make char buffer
     fseek(file, 0, SEEK_END);
     size = ftell(file);
     
     //go back to start of file
     rewind(file);
     
     //allocate size of array
     buffer = (char*)malloc (sizeof(char) * size);
     
     //load text into string
     fread(buffer, sizeof(char), size, file);
     
     //put text in text box
     SetWindowText(edit, buffer);
}

Any idea why it's not working?
http://furboxes.com/forum
Advertisement
Try declaring HWND edit; outside of the WndProc switch statement - and make it a static variable too.

static HWND edit;

Alternatively, slap a call to

edit = GetDlgItem(hwnd, E);

before calling

LoadTXT(edit, win.fileStr);

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
thanks that fixed it..but the text that shows up in the box doesn't have any new lines..even if the text file i load does...how do i fix that?

[Edited by - Death100 on January 31, 2007 3:18:52 PM]
http://furboxes.com/forum
bump
http://furboxes.com/forum
I think edit boxes in Win32 expect CR+LF (carriage return + linefeed) line separators - in other words, of the format \r\n, not just \n like your text files probably contain. If you insert a \r before every newline character in your text, it should appear correctly in the edit box.
grr..that's annoying..oh well i guess i can fix that
http://furboxes.com/forum
I was just tinkering with my loadTXT code and if I open the txt in binary mode ("rb") the new lines work for some wird reason
http://furboxes.com/forum
Quote:Original post by Death100
I was just tinkering with my loadTXT code and if I open the txt in binary mode ("rb") the new lines work for some wird reason
From MSDN documentation on fopen:
Quote:Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output.
Your text files probably have CR+LFs in them, so "b" (binary) mode preserves them and makes the edit boxes happy. Without "b" (text mode) they'd be converted to LFs.

This topic is closed to new replies.

Advertisement