回 帖 发 新 帖 刷新版面

主题:新手求教!

#include<windows.h>
#include<stdlib.h>
#include<string.h>

#define MAXEVN 4096
static TCHAR szAppname[]="Environ";

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   PSTR szCmdLine, int iCmdShow)
{
    HWND        hwnd;
    MSG         msg;
    WNDCLASS    wndclass;

    wndclass.cbClsExtra         = 0;
    wndclass.cbWndExtra         = 0;
    wndclass.hbrBackground      = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor            = LoadCursor(NULL, IDC_ARROW);
    wndclass.hIcon              = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hInstance          = hInstance;
    wndclass.lpfnWndProc        = WndProc;
    wndclass.lpszClassName      =NULL;
    wndclass.lpszMenuName       =NULL;
    wndclass.style              = CS_HREDRAW | CS_VREDRAW;

    if(!RegisterClass(&wndclass))
        return FALSE;

    hwnd = CreateWindow(szAppname,"EnvironList",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        CW_USEDEFAULT, CW_USEDEFAULT,
                        NULL,NULL, hInstance, NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}


回复列表 (共4个回复)

沙发

LRESULT CALLBACK WndProc(HWND  hwnd, UINT imsg, WPARAM wParam, LPARAM lParam)
{
    static char szBuffer[MAXEVN+1];
    static HWND  hwndList, hwndText,hwndEdit, Addbutton,Deletebutton;
    HDC  hdc;
    int  i;
    TEXTMETRIC tm;
    switch(imsg)
    {
    case WM_CREATE:
        hdc = GetDC(hwnd);
        GetTextMetrics(hdc,&tm);
        ReleaseDC(hwnd, hdc);

        hwndList = CreateWindow("ListBox",NULL,
                               WS_CHILD | WS_VISIBLE |
                               LBS_STANDARD |LBS_SORT |WS_VSCROLL,
                               tm.tmAveCharWidth*4,tm.tmHeight*3,
                               tm.tmAveCharWidth*30 + GetSystemMetrics(SM_CXVSCROLL),
                               tm.tmHeight*10,hwnd, (HMENU)1,
                               (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    
        hwndText = CreateWindow("static",NULL,
                               WS_CHILD |WS_VISIBLE |SS_LEFT,
                               tm.tmAveCharWidth*2,   tm.tmHeight - 10,
                               tm.tmAveCharWidth*MAXEVN/2, tm.tmHeight,
                               hwnd, (HMENU)2,
                               (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

        hwndEdit = CreateWindow("Edit","ItemEdit",
                                WS_CHILD |WS_VISIBLE | WS_BORDER |
                                ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL |
                                ES_AUTOVSCROLL, tm.tmAveCharWidth*35+GetSystemMetrics(SM_CXVSCROLL),
                                tm.tmHeight *3,
                                tm.tmAveCharWidth*20+GetSystemMetrics(SM_CXVSCROLL),
                                tm.tmHeight*2,
                                hwnd, (HMENU)3,
                                ((LPCREATESTRUCT)lParam)->hInstance, NULL);

        Addbutton = CreateWindow("Button", "Add",
                                  WS_CHILD |WS_VISIBLE | BS_PUSHBUTTON,
                                  tm.tmAveCharWidth*39,
                                  tm.tmHeight*6,
                                  tm.tmAveCharWidth*9,
                                  tm.tmHeight*2,
                                  hwnd, (HMENU)4,
                                  ((LPCREATESTRUCT)lParam)->hInstance, NULL);

        

板凳

Deletebutton = CreateWindow("button" ,"Delete",
                                  WS_CHILD |WS_VISIBLE | BS_PUSHBUTTON,
                                  tm.tmAveCharWidth*50,
                                  tm.tmHeight*6,
                                  tm.tmAveCharWidth*9,
                                  tm.tmHeight*2,
                                  hwnd, (HMENU)5,
                                  ((LPCREATESTRUCT)lParam)->hInstance, NULL);
        
        SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)"Good");
        return 0;
    case WM_SETFOCUS:
        SetFocus(hwndEdit);
        return 0;
    case WM_COMMAND:
        if(LOWORD(wParam)==1 && HIWORD(wParam)==LBN_SELCHANGE)
        {
            i = SendMessage(hwndList, LB_GETCURSEL,0, 0);

            i = SendMessage(hwndList, LB_GETTEXT,i, (LPARAM)szBuffer);
            SetWindowText(hwndText,szBuffer);
            return 0;
        }
        switch(LOWORD(wParam))
        {
        case 4:
            GetDlgItemText(hwnd, 3, szBuffer, 10);
            SendMessage(hwndList,LB_ADDSTRING,0, (LPARAM)szBuffer);
            return 0;

        case 5:
            i = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
            SendMessage(hwndList,LB_DELETESTRING,i, wParam);
            SendMessage(hwndList,LB_GETCURSEL,i-1, 0);
        default:
            return 0;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, imsg, wParam, lParam);
}这是书上的冽题,为什么看不到界面,一闪而过,请各位指点一下!

3 楼

你初始窗口类时错了
把wndclass.lpszClassName =NULL;
改为wndclass.lpszClassName =szAppname
就OK了

4 楼

谢谢!哥们!

我来回复

您尚未登录,请登录后再回复。点此登录或注册