我想用代码创建一个窗口 就从一个程序中摘录了一段创建创建串口的代码 结果编译通过 但是运行有错误 请大虾们看看怎么回事 或者帮忙写一段创建窗口的代码 
#include <winsock2.h> 
int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) 
{  
//注册窗口类 
WNDCLASSEX wx; 
WNDPROC WndProc; 
wx.cbSize=sizeof(WNDCLASSEX); 
wx.style=CS_HREDRAW|CS_VREDRAW; 
wx.lpfnWndProc=WndProc; 
wx.cbClsExtra=0; 
wx.cbWndExtra=0; 
wx.hInstance=hInstance; 
wx.hIcon=NULL; 
wx.hCursor=NULL; 
wx.hbrBackground=NULL; 
wx.lpszMenuName=NULL; 
wx.lpszClassName="CHAT"; 
wx.hIconSm=NULL; 
RegisterClassEx(&wx); 
//创建程序主窗口 
HWND hWnd=CreateWindow("CHAT","chat",WS_OVERLAPPEDWINDOW &~WS_MAXIMIZEBOX &~WS_THICKFRAME, 
200,200,505,410,NULL,NULL,hInstance,NULL); 
if(!hWnd) 
return FALSE; 
//显示窗口 
ShowWindow(hWnd,nCmdShow); 
UpdateWindow(hWnd); 

这样做我是想了解创建窗口的函数CreateWindow或者CreateWindowEx具体用法和使用后的效果 。初学者 

解决方案 »

  1.   


    #include <windows.h>                 // For all that Windows stuff
       
    LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);
       
    //======================================================================
    // Program entry point
    //
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine, int nCmdShow) {
        WNDCLASS wc;
        HWND hWnd;
        MSG msg;
       
        // Register application main window class.
        wc.style = 0;                             // Window style
        wc.lpfnWndProc = MainWndProc;             // Callback function
        wc.cbClsExtra = 0;                        // Extra class data
        wc.cbWndExtra = 0;                        // Extra window data
        wc.hInstance = hInstance;                 // Owner handle
        wc.hIcon = NULL,                          // Application icon
        wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
        wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
        wc.lpszMenuName =  NULL;                  // Menu name
        wc.lpszClassName = TEXT("MyClass");       // Window class name
       
        if (RegisterClass (&wc) == 0) return -1;
       
        // Create main window.
        hWnd = CreateWindowEx(WS_EX_ACCEPTFILES,       // Ex style flags
                              TEXT("MyClass"),    // Window class
                              TEXT("Hello"),      // Window title
                              // Style flags
                              WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
                              CW_USEDEFAULT,      // x position
                              CW_USEDEFAULT,      // y position
                              CW_USEDEFAULT,      // Initial width
                              CW_USEDEFAULT,      // Initial height
                              NULL,               // Parent
                              NULL,               // Menu, must be null
                              hInstance,          // Application instance
                              NULL);              // Pointer to create
                                                  // parameters
        if (!IsWindow (hWnd)) return -2;  // Fail code if not created.
       
        // Standard show and update calls
        ShowWindow (hWnd, nCmdShow);
        UpdateWindow (hWnd);
       
        // Application message loop
        while (GetMessage (&msg, NULL, 0, 0)) {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
        // Instance cleanup
        return msg.wParam;
    }
    //======================================================================
    // MainWndProc - Callback function for application window
    //
    LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                                  LPARAM lParam) {
        PAINTSTRUCT ps;
        RECT rect;
        HDC hdc;
       
        switch (wMsg) {
        case WM_PAINT:
            // Get the size of the client rectangle
            GetClientRect (hWnd, &rect);
       
            hdc = BeginPaint (hWnd, &ps); 
            DrawText (hdc, TEXT ("Hello Windows!"), -1, &rect, 
                      DT_CENTER | DT_VCENTER | DT_SINGLELINE);
       
            EndPaint (hWnd, &ps); 
            return 0;
       
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        }
        return DefWindowProc (hWnd, wMsg, wParam, lParam);
    }