像 前面的 example : http://realchecko.blogspot.com/2010/05/win32-listbox-control.html
改用 WTL 來寫,就是:
#pragma once
#define WINVER 0x0600
#include <atlbase.h>
#include <atlapp.h>
#include <atlwin.h>
#include <atlctrls.h>
#include "resource.h"
class CMainDialog : public CDialogImpl<CMainDialog>
{
public:
enum {IDD = IDD_DIALOG1 };
int addi;
CListBox list;
BEGIN_MSG_MAP(CMainDialog)
MESSAGE_HANDLER(WM_INITDIALOG,OnInitDialog)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
COMMAND_ID_HANDLER(IDC_BTNADD,OnBtnAdd)
END_MSG_MAP()
LRESULT OnInitDialog(UINT,WPARAM,LPARAM,BOOL&)
{
addi=0;
RETAILMSG(1,(TEXT("OnInitDialog\r\n")));
list.Attach(GetDlgItem(IDC_LIST1));
return TRUE;
}
LRESULT OnCancel(WORD,WORD wID,HWND,BOOL&)
{
::PostQuitMessage(wID);
return 0;
}
LRESULT OnBtnAdd(WORD,WORD wID,HWND,BOOL& hHandled)
{
TCHAR outmsg[10];
swprintf(outmsg,_T("%d"),addi++);
list.AddString(outmsg);
int cnt = list.GetCount();
if(cnt>30)
list.DeleteString(0);
cnt = list.GetCount();
if(cnt>=14)
list.SetTopIndex(cnt-14);
RETAILMSG(1,(TEXT("Button Add\r\n")));
return TRUE;
}
};
CAppModule _Module;
int WINAPI _tWinMain(HINSTANCE hInstance,HINSTANCE,LPTSTR lpstrCmdLine,int nCmdShow)
{
HRESULT hRes = ::CoInitializeEx(NULL,COINIT_MULTITHREADED);
::DefWindowProc(NULL,0,0,0L);
AtlInitCommonControls(NULL);
hRes = _Module.Init(NULL,hInstance);
CMainDialog dlgMain;
hRes = dlgMain.DoModal();
::CoUninitialize();
return hRes;
}
可以看到,大概是用 Attach( GetDlgItem(IDC_LIST1)) 把 WTL class: CListBox 和 ListBox control 建立關係。
相關的說明: http://www.cchsu.com/arthur/prg_bg5/wtl3.htm
沒有留言:
張貼留言