vs2010新建一個win32應用程式,然后添加一個對話框資源idd_dialog1。點擊確定按鈕會彈出一個messagebox,但是點擊訊息框的確定按鈕無反應。
洗掉系統自動生成的代碼,將下面的代碼復制到工程中。
#include "stdafx.h"
#include "testdlg.h"
#define MAX_LOADSTRING 100
// 全域變數:
HINSTANCE hInst; // 當前實體
TCHAR szTitle[MAX_LOADSTRING]; // 標題欄文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類名
BOOL InitInstance(HINSTANCE, int);
INT_PTR CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// 主訊息回圈:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!IsDialogMessage(msg.hwnd,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 將實體句柄存盤在全域變數中
hWnd =CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,WndProc);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
INT_PTR CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析選單選擇:
switch (wmId)
{
case IDOK:
MessageBox(hWnd,_T("無法關閉!"),_T("郁悶"),MB_OK);
break;
case IDCANCEL:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意繪圖代碼...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
uj5u.com熱心網友回復:
CreateDialog 時用獨立的 WndProc參考 App 向導生成的 Hello word 的 About
uj5u.com熱心網友回復:
default:
//return DefWindowProc(hWnd, message, wParam, lParam);
break;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/128912.html
標籤:基礎類
上一篇:界面美化問題,跪求。
