在哪里操作,才能将伪代码中令人头晕的 if 转换为switch?
源代码:
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"Char is %d",wParam);
MessageBox(hwnd,szChar,"字符",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"谁让你单击鼠标左键?","鼠标",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"编程",strlen("编程"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hdcpaint;
PAINTSTRUCT ps;
hdcpaint=BeginPaint(hwnd,&ps);
TextOut(hdcpaint,0,100,"我爱你",strlen("我爱你"));
EndPaint(hwnd,&ps);
ReleaseDC(hwnd,hdcpaint);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"退出吗?","确认",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam); //
}
return 0;
}
生成的伪代码:
LRESULT __stdcall WinSunProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
HWND Hwnd; // esi@7
HWND hWnd; // esi@5
HDC hdcpaint; // ebx@5
HWND HWnd; // esi@12
HDC hdc; // ebx@12
struct tagPAINTSTRUCT ps; // [sp+20h] [bp-40h]@5
char szChar; // [sp+Ch] [bp-54h]@13
if ( uMsg <= (unsigned int)WM_CLOSE )
{
if ( uMsg != WM_CLOSE )
{
if ( uMsg == WM_DESTROY )
{
PostQuitMessage(0);
return 0;
}
if ( uMsg == 15 )
{
hWnd = hwnd; // 顺便问一句:这2个变量能合而为一吗?
hdcpaint = BeginPaint(hwnd, &ps);
TextOutA(hdcpaint, 0, 100, "我爱你", strlen("我爱你") - 1);
EndPaint(hWnd, &ps);
ReleaseDC(hWnd, hdcpaint);
return 0;
}
return DefWindowProcA(hwnd, uMsg, wparam, lparam);
}
Hwnd = hwnd;
if ( MessageBoxA(hwnd, "退出吗?", "确认", MB_YESNO) == IDYES )
{
DestroyWindow(Hwnd);
return 0;
}
return 0;
}
if ( uMsg == WM_CHAR )
{
sprintf(&szChar, "Char is %d", wparam);
MessageBoxA(hwnd, &szChar, "字符", 0);
return 0;
}
if ( uMsg != 513 )
return DefWindowProcA(hwnd, uMsg, wparam, lparam);
HWnd = hwnd;
MessageBoxA(hwnd, "谁让你单击鼠标左键?", "鼠标", 0);
hdc = GetDC(HWnd);
TextOutA(hdc, 0, 50, "编程", strlen("编程") - 1);
ReleaseDC(HWnd, hdc);
return 0;
}