Win32SDK对话框模板

前言

vc6向导生成Win32SDK时,没有对话框模板。
整理了一个, 写测试程序用.

Win32SDK对话框模板

// hw.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

#include <Shellapi.h>
#pragma comment(lib, "Shell32.lib")

#include "resource.h"
#include "constDefine.h"
#include "constDefine.h"

HINSTANCE g_hInstance = NULL;
HWND g_hMainWnd = NULL;

LRESULT CALLBACK    MainDlgProc(HWND, UINT, WPARAM, LPARAM);
void fnCenterWindow(HWND hWnd);
void fnSetWndText(HWND hWnd, const TCHAR* pcTip);
void fnSetCtrlText(int iCtrlId, const TCHAR* pcTip);

LRESULT OnInitDialog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnClose(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnCancel(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnOk(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnDropFiles(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// 做一些测试任务, 发行时,里面的内容会被关掉
void fnTest_On_WinMain();

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    g_hInstance = hInstance;
    fnTest_On_WinMain();
    return DialogBox(hInstance, (LPCTSTR)IDD_MAIN_DLG, GetDesktopWindow(), (DLGPROC)MainDlgProc);
}

LRESULT CALLBACK MainDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    BOOL bRc = TRUE;
    WORD wCtrlId = LOWORD(wParam);
    WORD wNotifyCode = HIWORD(wParam);
    HWND hSubWnd = (HWND)lParam;

    switch (message) {
        case WM_INITDIALOG: {
                OnInitDialog(hWnd, message, wParam, lParam);
            }
            break;
        case WM_COMMAND:

            switch (wCtrlId) {
                case IDCANCEL:
                    OnCancel(hWnd, message, wParam, lParam);
                    break;
                case IDOK:
                    OnOk(hWnd, message, wParam, lParam);
                    break;
                default:
                    break;
            }

            break;
        case WM_CLOSE:
            OnClose(hWnd, message, wParam, lParam);
            break;
        case WM_DROPFILES:
            OnDropFiles(hWnd, message, wParam, lParam);
            break;
        default:
            bRc = FALSE;
            break;
    }

    return bRc;
}

void fnCenterWindow(HWND hWnd)
{
    HWND hwndOwner;
    RECT rc;
    RECT rcDlg;
    RECT rcOwner;
    hwndOwner = GetParent(hWnd);

    if (NULL == hwndOwner) {
        hwndOwner = GetDesktopWindow();
    }

    GetWindowRect(hwndOwner, &rcOwner);
    GetWindowRect(hWnd, &rcDlg);
    CopyRect(&rc, &rcOwner);
    OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
    OffsetRect(&rc, -rc.left, -rc.top);
    OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
    SetWindowPos(hWnd,
                 HWND_TOP,
                 rcOwner.left + (rc.right / 2),
                 rcOwner.top + (rc.bottom / 2),
                 0,
                 0,
                 SWP_NOSIZE);
}

void fnSetCtrlText(int iCtrlId, const TCHAR* pcTip)
{
    HWND hWnd = NULL;

    if (NULL != g_hMainWnd) {
        hWnd = GetDlgItem(g_hMainWnd, iCtrlId);
        fnSetWndText(hWnd, pcTip);
    }
}

void fnSetWndText(HWND hWnd, const TCHAR* pcTip)
{
    if (NULL != hWnd) {
        SetWindowText(hWnd, (NULL != pcTip) ? pcTip : _T(""));
    }
}

LRESULT OnInitDialog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    g_hMainWnd = hWnd;
    fnSetWndText(g_hMainWnd, G_STR_PROG_NAME);
    fnCenterWindow(hWnd);
    return S_OK;
}

LRESULT OnClose(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ::MessageBox(g_hMainWnd, "关闭", "提示", MB_OK);
    EndDialog(hWnd, LOWORD(wParam));
    return S_OK;
}

LRESULT OnCancel(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ::MessageBox(g_hMainWnd, "取消", "提示", MB_OK);
    EndDialog(hWnd, LOWORD(wParam));
    return S_OK;
}

LRESULT OnOk(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ::MessageBox(g_hMainWnd, "确定", "提示", MB_OK);
    EndDialog(hWnd, LOWORD(wParam));
    return S_OK;
}

LRESULT OnDropFiles(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    POINT pt; 
    UINT uFilesCnt = 0;
    UINT uIndex = 0;
    HDROP hDropFile = (HDROP)wParam;
    char lpszFile[MAXBYTE] = {'\0'}; 

    DragQueryPoint(hDropFile, &pt); 

    uFilesCnt = DragQueryFile(hDropFile, -1, (LPSTR) NULL, 0); 
    for(uIndex = 0; uIndex < uFilesCnt; uIndex++) { 
        DragQueryFile(hDropFile, uIndex, lpszFile, sizeof(lpszFile)); 
        fnSetCtrlText(IDC_EDIT, lpszFile);
    } 

    DragFinish(hDropFile); 


    return S_OK;
}

void fnTest_On_WinMain()
{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值