Window isn't being created

This is starting to REALLY frustrate me. Out of boredom, I thought I would program a simple text editor. I followed the standard method to create a window to the dot - it's almost exactly the same as an example I found except for error-checking and window names. But, every time I run it, it says that it wasn't able to create the window. WTF? Can anyone see an error in this?

http://pastebin.com/7JR9xWkQ
1. You need a default case in your window procedure that passes unused windows messages onto the default windows message processing function:

2. Your message loop was incorrect - The way it was written you could close the window but the program would still be running!

3. Your creation of the editbox was incorrect (the CW_DEFAULT isn't suitable for child windows normally) - I also made it visible and gave it an ID

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <windows.h>
#include <string>
using std::string;

//global variables
HINSTANCE inst;
HWND hwnd;
HWND txtbox;
char buffer[256];

const int IDC_EDITBOX =3;

//useful functions
void show_error(const char* msg,unsigned error){
    _snprintf(buffer,256,"ERROR: %s %i",msg,error);
    MessageBox(0,buffer,"ERROR",MB_OK|MB_ICONEXCLAMATION);}

LRESULT CALLBACK pipad_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_CREATE:
            txtbox=CreateWindow("EDIT","",WS_BORDER|WS_VISIBLE|WS_CHILD|ES_AUTOVSCROLL|ES_MULTILINE|WS_VSCROLL/*|ES_WANTRETURN*/,0,0,
            300,300,hwnd,(HMENU)IDC_EDITBOX,inst,NULL);
            if(txtbox==NULL){
                show_error("Failed to create the text box\n\nError code:",GetLastError());
                DestroyWindow(hwnd);}
            break;
     default:
        return DefWindowProc(hwnd, msg, wparam, lparam);
            }
    return 0 ;
    }

int WINAPI WinMain(HINSTANCE hinst,HINSTANCE hprev,LPSTR cmdline,int cmdshow){
    WNDCLASSEX wndclass;
    MSG msg;
    
     inst=hinst;   
    
    wndclass.cbSize=sizeof(WNDCLASSEX);
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc=pipad_proc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hinst;
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName="PIpad";
    wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
    if(!RegisterClassEx(&wndclass)){
        show_error("Failed to register the window class\n\nError code:",GetLastError());
        return 1;}
    hwnd=CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,"PIpad","Untitled - PIpad",WS_OVERLAPPEDWINDOW|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hinst,NULL);
    if(!hwnd){
        show_error("Failed to create the window\n\nError code:",GetLastError());
        return 2;}
    ShowWindow(hwnd,cmdshow);

    while(GetMessage(&msg,hwnd,0,0) >0){
        TranslateMessage(&msg);
        DispatchMessage(&msg);}
    return msg.wParam;}

Last edited on
Topic archived. No new replies allowed.