原创 基于Win32的Dialog

2008-12-5 20:40 6659 8 8 分类: 软件与OS

最近在学WinCE,就拿VC的Win32来练练手,初学第一个工程是基于Win32的Dialog,首先创建模式对话框,并注册消息处理函数。


int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 INITCOMMONCONTROLSEX icex;


 // Ensure that the common control DLL is loaded.
 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
 icex.dwICC  = ICC_LISTVIEW_CLASSES|ICC_TAB_CLASSES|ICC_LISTVIEW_CLASSES;
 InitCommonControlsEx(&icex);


 g_hInstance = hInstance;


 //------------ Prevent multiple instance  ------------------
 HANDLE hMutex = CreateMutex(NULL,FALSE,APPNAME);


 if( GetLastError() == ERROR_ALREADY_EXISTS )
 {
      HWND hwndPrev = FindWindow( L"Dialog" , APPNAME );
      if( hwndPrev )
      {
             ShowWindow(hwndPrev, SW_SHOWNORMAL);
             SetForegroundWindow(hwndPrev);
        }
        return 0;
    }

    DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDLG), NULL, (DLGPROC)  DlgProc);
    return 0;
}


c3c9f2a8-0820-43f4-9a17-df2094bfd7dc.JPG


static BOOL CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
#if TRACE_DEBUG_USE
 TRACE(_T("MSG = 0x%08X),wParam = 0x%08X, lParam = 0x%08X\n"), Msg, (DWORD)wParam,(DWORD)lParam);
#endif
 switch(Msg)
 {
// ----------------------------------------------------------
 case WM_INITDIALOG:
 {
  g_hDlgHwnd = hDlg;
  DlgLayout(hDlg);  // 排布对话框
  memset(&g_LCDMemory[0][0],0x00,APP_LCD_Wbyte*APP_LCD_H);;
  InvalidateRect(GetDlgItem(hDlg, IDC_BUTTON_LCD), NULL, FALSE);// 画按钮
  break;
 }
// ----------------------------------------------------------
 case WM_NOTIFY:
  break;
// ----------------------------------------------------------
 case WM_SIZE:
 {
  RECT rt;
  SetRect(&rt, 0, 0, LOWORD(lParam), HIWORD(lParam));
  break;
 }
 case WM_DRAWITEM:
 {
  LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam;
  if (lpdis) DrawGraph(lpdis->hwndItem);
  return TRUE;
 }
// ----------------------------------------------------------
 case WM_COMMAND:
  switch(LOWORD(wParam))
  {
  case IDOK:
   return TRUE;
  case IDCANCEL:
   return TRUE;
  case IDC_BUTTON_UP:
   CInitWindow(0x44);
   InvalidateRect(GetDlgItem(hDlg, IDC_BUTTON_LCD), NULL, FALSE);
   return TRUE;
  case IDC_BUTTON_DOWN:
   CInitWindow(0x66);
   InvalidateRect(GetDlgItem(hDlg, IDC_BUTTON_LCD), NULL, FALSE);
   return TRUE;
  case IDC_BUTTON_LEFT:
   CInitWindow(0x77);
   InvalidateRect(GetDlgItem(hDlg, IDC_BUTTON_LCD), NULL, FALSE);
   return TRUE;
  case IDC_BUTTON_RIGHT:
   CInitWindow(0x00);
   InvalidateRect(GetDlgItem(hDlg, IDC_BUTTON_LCD), NULL, FALSE);
   return TRUE;
  case IDC_BUTTON_BACK:
   CInitWindow(0x22);
   InvalidateRect(GetDlgItem(hDlg, IDC_BUTTON_LCD), NULL, FALSE);
   return TRUE;
  case IDC_BUTTON_ENTER:
   CInitWindow(0xFF);
   InvalidateRect(GetDlgItem(hDlg, IDC_BUTTON_LCD), NULL, FALSE);
   return TRUE;
  }
  break;
// ----------------------------------------------------------
 case WM_SYSCOMMAND:
 {
  switch((DWORD)wParam)
  {
  case SC_CONTEXTHELP: // XP下用这个消息显示About,WinCE下是另外一个
   DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), hDlg, (DLGPROC)DlgProcHelp);
   return TRUE;
  case SC_CLOSE:
   break;
  case SC_MINIMIZE:
   break;
  case SC_MAXIMIZE:
   break;
  }
  break;
 }
// ----------------------------------------------------------
 case WM_TIMER:
  break;
// ----------------------------------------------------------
 case WM_CLOSE:
  EndDialog(hDlg, 0); // 关闭对话框
  DestroyWindow(hDlg);
  g_hDlgExist = FALSE;
  return TRUE;
// ----------------------------------------------------------
 case WM_DESTROY:
  return FALSE;
 }
 return FALSE;
}

文章评论0条评论)

登录后参与讨论
我要评论
0
8
关闭 站长推荐上一条 /2 下一条