Jump to content
  • Advertisement
Sign in to follow this  
Death100

SetWindowText

This topic is 4252 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!