私たちは、チュートリアル1で、最初のウィンドウを作りました。
引き続き、私たちの小さなダイアログ例に、いくつかのコントロールを付けましょう。
出来上がりは、こんなものです。
注:
これから先、MS VC++ のように、textboxe を テキスト・ボックス(editboxe) と呼び、label は ラベル(static) と呼ぶことにします。
このチュートリアルで、つぎの Visual Basic を、Windows API/FreeBASIC に書き直してみます。
If Text1.Text <> VBNullstring
then
Text2.Text = Text1.Text
else Msgbox "You didnt enter any text!", vbinformation
End
if
簡単です。コンパイラが、あなたのために、面倒な Windows API 作業のすべてをしてくれます。
C/C++ でも、FreeBASIC でも、同じです。
この仕掛けを、確認していきます。さあ、始めましょう。
ここでは、テンプレートとして、DialogAsMain.tpl を使って、新しいプロジェクトを作ります。
プロジェクト名は、Editbox とします。
プロジェクト内容は、Editbox sample とします。
この新しいテンプレートは、前もって、あなたが必要な全てを含んだ、ほとんど完全な、アプリケーションです。
あなたがすることは、コントロールを置いて、それらのコードを書くだけです。残りは、Editor によって行われます。
「Go」で実行してみると、既に、ドロップダウン・メニューの、「Exit」と「About.」ができあがっている!ことが分かります。
FbEdit の IDD_DIALOG をダブル・クリックすると、下の画面のようになります。
これから作るダイアログに、左側のツールバーのアイコンを使って、
2つのテキスト・ボックス(editboxe)と、4個のコマンド・ボタン、および、必要に合わせて、いくつかのラベル(static) を置いていきます。
2つの、テキスト・ボックス(editboxe) を、Text1 と Text2 と、名前を付けます。
状況に合わせて、Text2 の Locked を TRUE に設定できます。
4個のボタンに、左から右方向に順に、Command1、Command2、Command3、Command4と、名前を付けます。
ダイアログとボタンに、以下の画面のように、名前を付けます。
また、プログラムに合ったアイコンを選んで、\resフォルダに入れて下さい。そして、リソース・ダイアログに、それを入れることを、忘れないで下さい。(参照:チュートリアル 1 )
IDD_DIALOG Dialog の Caption を「Editbox sample」に変更します。
テストするために Go をクリックすると、下のようなメッセージが表示されるはずです。
NOTE: For example you place a STATIC on your dialog and add following text to your static: >> Press "enter" to continue. << When trying to assemble&link you will get an error, thats because you have to DOUBLE "" the quotes each time you use them on a static, the same is for the EDITBOX caption. |
#Define IDM_MENU 10000
#Define IDM_FILE_EXIT 10001
#Define IDM_HELP_ABOUT 10101
Dim Shared hInstance As HMODULE
Dim Shared CommandLine As ZString Ptr
Dim Shared hWnd As HWND
Dim Shared hIcon As HICON
Const ClassName="DLGCLASS"
Const AppName="Dialog as main"
Const AboutMsg=!"FbEdit Dialog as main\13\10Copyright ゥ FbEdit 2007"
Const Warning="You didnt type any text"
Const Copyright="This is my software"
ここまで登録できたら、Editbox.bi ファイルを閉じることができます。
次に、Editbox.basをダブルクリックします。
コードには、既に灰色の部分ができあがっているはずです。
赤の部分について、追加登録して下さい。
#Include Once "windows.bi"
#Include Once "win/commctrl.bi"
#Include Once "win/commdlg.bi"
#Include Once "win/shellapi.bi"
#Include "Editbox.bi"
Function WndProc(ByVal hWin As HWND,ByVal uMsg As UINT,ByVal wParam As WPARAM,ByVal lParam As LPARAM) As Integer
Dim buff As ZString*256
Select Case uMsg
Case WM_INITDIALOG
hWnd=hWin
hIcon=LoadIcon(hInstance,Cast(ZString Ptr,500))
SendMessage(hWin,WM_SETICON,NULL,Cast(LPARAM,hIcon))
'
Case WM_COMMAND
Select Case HiWord(wParam)
Case BN_CLICKED,1
Select Case LoWord(wParam)
Case IDM_FILE_EXIT
SendMessage(hWin,WM_CLOSE,0,0)
'
Case IDM_HELP_ABOUT
ShellAbout(hWin,@AppName,@AboutMsg,NULL)
'
Case Command1
' first we check if the user has inserted any text
' if so we copy it to the second textbox
If GetWindowTextLength(GetDlgItem(hWin,Text1)) Then
' ok here we get only if user has typed some text
GetDlgItemText(hWin,Text1,@buff,SizeOf(buff))
SetDlgItemText(hWin,Text2,@buff)
Else
' User didnt enter any text
MessageBox(hWin,@Warning,@AppName,MB_ICONERROR)
EndIf
'
Case Command2
' Win API for a messagebox
MessageBox(hWin,@AppName,@Copyright,MB_OK)
'
Case Command3
' Windows API: Unload me.
SendMessage(hWin,WM_CLOSE,0,0)
'
Case Command4
MessageBox(hWin,"Hello, this is a test","Messagebox caption",MB_ICONINFORMATION)
'
End Select
'
End Select
'
Case WM_SIZE
'
Case WM_CLOSE
DestroyWindow(hWin)
'
Case WM_DESTROY
PostQuitMessage(NULL)
'
Case Else
Return DefWindowProc(hWin,uMsg,wParam,lParam)
'
End Select
Return 0
End Function
Function WinMain(ByVal hInst As HINSTANCE,ByVal hPrevInst As HINSTANCE,ByVal CmdLine As ZString Ptr,ByVal CmdShow As Integer) As Integer
Dim wc As WNDCLASSEX
Dim msg As MSG
' Setup and register class for dialog
wc.cbSize=SizeOf(WNDCLASSEX)
wc.style=CS_HREDRAW Or CS_VREDRAW
wc.lpfnWndProc=@WndProc
wc.cbClsExtra=0
wc.cbWndExtra=DLGWINDOWEXTRA
wc.hInstance=hInst
wc.hbrBackground=Cast(HBRUSH,COLOR_BTNFACE+1)
wc.lpszMenuName=Cast(ZString Ptr,IDM_MENU)
wc.lpszClassName=@ClassName
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION)
wc.hIconSm=wc.hIcon
wc.hCursor=LoadCursor(NULL,IDC_ARROW)
RegisterClassEx(@wc)
' Create and show the dialog
CreateDialogParam(hInstance,Cast(ZString Ptr,IDD_DIALOG),NULL,@WndProc,NULL)
ShowWindow(hWnd,SW_SHOWNORMAL)
UpdateWindow(hWnd)
' Message loop
Do While GetMessage(@msg,NULL,0,0)
TranslateMessage(@msg)
DispatchMessage(@msg)
Loop
Return msg.wParam
End Function
' Program start
hInstance=GetModuleHandle(NULL)
CommandLine=GetCommandLine
InitCommonControls
WinMain(hInstance,NULL,CommandLine,SW_SHOWDEFAULT)
ExitProcess(0)
End
コードを追加登録して、「Go」ボタンを押してみて下さい。
表示された画面で「Text」ボタンを押すと、下ようなメッセージ・ボックスが表示されるはずです。
これで、できあがりです!
この事例のコードを、
ここで、ダウンロードできます。