close

DialogBox的新增方式詳見:http://www.codeproject.com/Articles/227831/A-dialog-based-Win-C-program

裡有詳細教學

 

// gggg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include "resource.h"
#include <string>

//***************************************************************
typedef struct S {
    std::wstring wstr;
}S;
S wstr_struct;

BOOL CALLBACK DialogProc( HWND hDlg, UINT msg, WPARAM wparam, LPARAM lparam ) {
    LPDRAWITEMSTRUCT lpdis; 
    
    HWND hwndList = GetDlgItem(hDlg, IDC_LIST1); // 要動控制項之前要先取得HWND

    switch ( msg ) {
        case WM_CREATE:
            
        case WM_SIZE:
            return true;
        case WM_INITDIALOG:
            return true;
        case WM_COMMAND:
            switch ( LOWORD(wparam) ) {
                case IDOK: 

                    SendMessage(hwndList, LB_SETHORIZONTALEXTENT, (int)(6.1 * wstr_struct.wstr.length()), 0);
                    // 使用LB_SETHORIZONTALEXTENT表示listbox內容會有的寬度,超出listbox外觀的寬度就會出現橫向捲軸了
                    // 不過他所謂的寬度表示為pixel,並非字元,而估算後*6.1大概為一個字元所佔寬度

                    for ( int  i = 0; i <20; i++ )
                    SendMessage(hwndList, LB_INSERTSTRING, 0, (LPARAM)wstr_struct.wstr.c_str()); 
                    // 這樣即可在listbox內加入項目(要先換成pointer的型別)

                    return true;
                case IDCANCEL:
                    EndDialog( hDlg, true );
                    PostQuitMessage(0);
                    return true;
            } // end switch
            break;
        case WM_DRAWITEM:
            lpdis = (LPDRAWITEMSTRUCT)lparam; 
            return true;
    } // end switch
    return false;
}

int main()
{
    
    wstr_struct.wstr = L"666663333333333333333339999999999999999994477777777777777777777777777777777444444444444444444555555555";
    MSG msg;

    HANDLE hErrMsgEvent = CreateEvent( NULL, FALSE, true, NULL );
    WaitForSingleObject( hErrMsgEvent, INFINITE ); // 等其他for迴圈跑完
    int b = DialogBox( NULL, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc );
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!GetMessage( &msg, NULL, NULL, NULL ))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
            break;
    }
}
arrow
arrow

    跪著讀 發表在 痞客邦 留言(0) 人氣()