VC6是Microsoft非常经典的开发环境,尤其是Windows API方式开发,自从Quick C for win以来基本保持着同样的风格和API,在它上面做习练很不错。下面是习练完成的界面,它是在自动创建的WIN32 application模板下,增加一个Dialog,然后将Dialog嵌入到默认页面中的,这样Dialog即为应用窗体,而Dialog则可以借助于Dialog Editor摆放控件,接近于RAD方式开发。
步骤一:创造WIN32应用程序。
创建后大致浏览一下WIN32开发套路,建应用程序结构、注册窗口、显示并update窗口,然后进入消息主循环。这种结构是基础,在Dev C、PowerBASIC, FreeBASIC的Win32开发也是一样的,与开发语言没关系,是WIN32SDK 开发结构上的要求。
步骤二:SDI界面创建后,马上创建自己的Dialog
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{HWND hWnd;hInst = hInstance; // Store instance handle in our global variablehWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if (!hWnd){return FALSE;}hWndGlobal = hWnd;SetWindowText(hWnd, "Dialog as Application - VC6");nCmdShow = SW_SHOW;ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);// Create DIALOG boxHWND hDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_MAINFORM), hWnd, (DLGPROC)Mainform);// Show DIALOG boxShowWindow(hDlg, SW_SHOWNA);UpdateWindow(hWnd);RECT rectDlgw, rectDlgc, rectMainw, rectMainc;GetWindowRect(hWnd, &rectMainw); //Dialog window areaGetClientRect(hWnd, &rectMainc); //Dialog client areaGetWindowRect(hDlg, &rectDlgw); //Dialog window areaGetClientRect(hDlg, &rectDlgc); //Dialog client areaRECT rect;HWND hwndDT=GetDesktopWindow(); GetClientRect(hwndDT,&rect); LONG dtWidth=rect.right-rect.left; LONG dtHeight=rect.bottom-rect.top; RECT rectbar;HWND taskbar = FindWindow("Shell_TrayWnd", "");GetWindowRect(taskbar, &rectbar);//if(rectbar.top>0) //TASKBAR at bottom//if(rectbar.left>0) //TASKBAR at left//Center MAINWINDOW at desktop client areaMoveWindow(hWnd, (rectbar.right-rectbar.left)+(dtWidth-(rectMainw.right-rectMainw.left))/2, (dtHeight-(rectMainw.bottom-rectMainw.top))/2, (rectDlgw.right-rectDlgw.left), (rectDlgw.bottom-rectDlgw.top) + (rectDlgc.bottom-rectDlgc.top)-(rectMainc.bottom-rectMainc.top)+1, 1);//Align inner DIALOG to MAINWINDOWMoveWindow(hDlg, 0, 0, rectDlgw.right, rectDlgw.bottom, 1);UpdateWindow(hWnd);return TRUE;
}
上面这两句是重点,这两句下面的功能是计算后调整SDI窗体,让它刚好将Dialog装入进去,并将它们显示在屏幕中央位置。
(DLGPROC)Mainform 参数指的是回调函数,在CALLBACK函数Mainform中,对Dialog Editor摆放的控件进行操作。
步骤三:操作Dialog 控件
LRESULT CALLBACK Mainform(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{switch (message){case WM_INITDIALOG:{//Change DIALOG iconHICON hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON3));SetClassLong(hDlg, GCL_HICONSM, (LONG)hIcon);GetDlgItem(hDlg, IDC_PROGRESS1);SendMessage(GetDlgItem(hDlg, IDC_PROGRESS1), PBM_SETRANGE, MAKEWPARAM(0, 0), MAKELPARAM(0, 59)); //SendMessage(GetDlgItem(hDlg, IDC_PROGRESS1), PBM_SETPOS, (WPARAM)(0, 65), (LPARAM)(0, 0)); SetTimer(hDlg, 1, 1000, NULL);//KillTimer(hDlg, 1);break;/*typedef struct _SYSTEMTIME {WORD wYear;WORD wMonth;WORD wDayOfWeek;WORD wDay;WORD wHour;WORD wMinute;WORD wSecond;WORD wMilliseconds;} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;*/SYSTEMTIME st, lt;GetSystemTime(&st);GetLocalTime(<);SendMessage(GetDlgItem(hDlg, IDC_PROGRESS1), PBM_SETPOS, (WPARAM)(0, st.wSecond), (LPARAM)(0, 0)); break;case WM_TIMER:{SYSTEMTIME st, lt;GetSystemTime(&st);GetLocalTime(<);SendMessage(GetDlgItem(hDlg, IDC_PROGRESS1), PBM_SETPOS, (WPARAM)(0, st.wSecond), (LPARAM)(0, 0)); }break;case WM_PAINT:break;case WM_CTLCOLORDLG:break;case WM_ERASEBKGND:case WM_NCPAINT:break;case WM_SYSCOMMAND:if (wParam == SC_CLOSE){EndDialog (hDlg, TRUE);KillTimer(hDlg, 1);PostQuitMessage(0);return(TRUE);}break;case WM_COMMAND://if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) if (LOWORD(wParam) == IDOK) {EndDialog(hDlg, LOWORD(wParam));KillTimer(hDlg, 1);PostQuitMessage(0);return TRUE;}if (LOWORD(wParam) == IDCANCEL){if (!IsDlgButtonChecked(hDlg, IDC_CHECK1)){CheckDlgButton(hDlg, IDC_CHECK1, BST_CHECKED);}if (!IsDlgButtonChecked(hDlg, IDC_RADIO1)){CheckDlgButton(hDlg, IDC_RADIO1, BST_CHECKED);}}if (LOWORD(wParam) == IDC_BUTTON4){SendMessage(hDlg, WM_SYSCOMMAND, SC_MAXIMIZE, 0); }if (LOWORD(wParam) == IDC_BUTTON3){SendMessage(hDlg, WM_SYSCOMMAND, SC_MINIMIZE, 0); }if (LOWORD(wParam) == IDC_BUTTON2){SetWindowText(GetDlgItem(hDlg, IDC_EDIT1),"Message 1"); SetWindowText(GetDlgItem(hDlg, IDC_EDIT2),"Message 2"); SetWindowText(GetDlgItem(hDlg, IDC_EDIT3),"Message 3"); }break;}return FALSE;
}
上面的代码包括对Edit文本框的操作,对Button的操作,对复选和Radio的操作,还用到了一个SYSTEMTIME结构和操作函数,让界面上的指示条随秒而动。对MFC的习练比WIN32功能多一些,编写程序比WIN32方便,但代码不如WIN32程序精练直接。
祝福CSDN朋友们节日快乐,学业进步、职场升级,身体健康!