运行程序。
点击Open_Dialog菜单出现下图:
输入Welcome to Binzo‘s blog!
点击确定,“Welcome to Binzo‘s blog!” 将出现在窗口左侧。
代码:
//Created by Binzo
#include
#include "resource.h "
#include
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK DlgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
HWND hDlg;
HINSTANCE hInst;
TCHAR str[] = TEXT("hello");
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]= TEXT( "hello, vc++ ");
static TCHAR lpszTitle[]= TEXT( "My First program ");
HWND hwnd;
MSG msg;
hInst=hInstance;
WNDCLASS wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szAppName;
HMENU hmenu;
hmenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
if(!RegisterClass(&wcex))
{
MessageBox(NULL,TEXT( "This program reguire Windows NT! "),szAppName,MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,TEXT( "The hello program "),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,NULL,hmenu,hInstance,NULL);
if(!hwnd)
{
MessageBox(NULL,TEXT( "can 't creat windows "),szAppName,MB_ICONERROR);
return 0;
}
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
if(hDlg == 0 || !IsDialogMessage(hDlg,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_OPENDLG:
if(hDlg)
DestroyWindow(hDlg);
hDlg=CreateDialog(hInst,MAKEINTRESOURCE(IDD_DIALOG1),
hwnd,(DLGPROC)DlgProc);
ShowWindow(hDlg, SW_SHOW);
break;
}
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT( "hello Binzo "),15 - 1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
DrawText(hdc,str,sizeof(str)/2 - 1,&rect,DT_SINGLELINE|DT_LEFT|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
BOOL CALLBACK DlgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hwndParent, hCtrl;
hwndParent = GetParent(hDlg);
switch(message)
{
case WM_INITDIALOG:
SetFocus(GetDlgItem(hDlg, IDC_EDIT1));
return 1;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_EDIT1:
break;
case IDOK:
GetDlgItemText(hDlg, IDC_EDIT1, str, 40);
InvalidateRect(hwndParent, NULL, TRUE);
if(hDlg){
DestroyWindow(hDlg);
EndDialog(hDlg,0);
}
return 1;
}
break;
case WM_CLOSE:
GetDlgItemText(hDlg, IDC_EDIT1, str, 40);
if(hDlg)
DestroyWindow(hDlg);
EndDialog(hDlg,0);
return 1;
}
return 0;
}