刘振华的博客
http://liuzhenhua.bokee.com/842168.html
#include "stdafx.h"
#include "resource.h"
#include <stdlib.h>
#include <stdio.h>
HICON hIcon;
HINSTANCE g_hInstance;
BOOL CALLBACK fnDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CENTER_WINDOW(HWND hWnd, HWND hParent);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hDlg;
MSG msg;
g_hInstance = hInstance;
hDlg = CreateDialog(hInstance, (LPCTSTR)IDD_DIALOG1, 0, fnDlgProc);
ShowWindow(hDlg, SW_SHOW);
while(GetMessage(&msg, hDlg, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
BOOL CENTER_WINDOW(HWND hWnd, HWND hParent)
{
if (!IsWindow(hWnd))
return FALSE;
if (!IsWindow(hParent) || 0 == hParent)
hParent = GetDesktopWindow();
RECT rcWnd;
GetWindowRect(hWnd, &rcWnd);
RECT rcParent;
GetWindowRect(hParent, &rcParent);
POINT ptNew;
int nWidth;
int nHeight;
int nParentWidth;
int nParentHeight;
nWidth = rcWnd.right - rcWnd.left;
nHeight = rcWnd.bottom - rcWnd.top;
nParentWidth = rcParent.right - rcParent.left;
nParentHeight = rcParent.bottom - rcParent.top;
ptNew.x = rcParent.left + (nParentWidth - nWidth) / 2;
ptNew.y = rcParent.top + (nParentHeight - nHeight) / 2;
return MoveWindow(hWnd, ptNew.x, ptNew.y, nWidth, nHeight, TRUE);
}
BOOL CALLBACK fnDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG: // 初始化对话框
HMODULE hHandle;
hHandle = GetModuleHandle(NULL);
hIcon = LoadIcon(hHandle, MAKEINTRESOURCE(IDI_ICON1));
SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon);
HWND hDeskTopWindow;
hDeskTopWindow = GetDesktopWindow();
CENTER_WINDOW(hDlg, hDeskTopWindow);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) // 结束对话框
{
PostMessage(hDlg, WM_QUIT, 0, 0);
return TRUE;
}
break;
}
return FALSE;
}