基于 SDK 的对话框程序框架

 转自:

刘振华的博客

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值