这个版本,和上一个示例[点我打开]基本一样,只是稍加美化.
需要说明的是:
如果读者想用我的代码,请稍作修改,因为我自定义了图标的,相应的有几处都需要修改,不过我想对于入门SDK的应该没什么问题.
这篇文章就不给编译后的程序了,最后放2张对比图吧.
[cpp]
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include "resource.h"
// 给按钮加上提示音
#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")
#define PlayTipWav PlaySound(_T("SystemStart"), NULL, SND_FILENAME | SND_ASYNC) // 调用PlayTipWav 即可
//使用系统主题
#pragma comment(linker, "/manifestdependency:\"type=’win32′ name=’Microsoft.Windows.Common-Controls’ version=’6.0.0.0′ processorArchitecture=’*’ publicKeyToken=’6595b64144ccf1df’ language=’*’\"")
// 初期定义
HINSTANCE hInst; // 当前实例
HFONT hFont; // 程序默认字体
LPCWSTR szTitle=_T("WinSDK 示例 Exp1"); // 标题栏文本
LPCWSTR szWindowClass=_T("Exp1"); // 主窗口类名
// 函数声明
LRESULT CALLBACK WndProcExp1(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
// 主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX Exp1;
//
Exp1.cbSize = sizeof(WNDCLASSEX);
Exp1.style = 3; // 窗口样式"CS_HREDRAW | CS_VREDRAW","0x0002+0x0001"=3
Exp1.lpfnWndProc = WndProcExp1;
Exp1.cbClsExtra = NULL;
Exp1.cbWndExtra = NULL;
Exp1.hInstance = hInstance;
Exp1.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON));
Exp1.hCursor = LoadCursor(NULL,IDC_ARROW);
Exp1.hbrBackground = (HBRUSH)(COLOR_WINDOW);
Exp1.lpszMenuName = NULL;
Exp1.lpszClassName = szWindowClass;
Exp1.hIconSm = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON));
// 关于窗口类的注册
if (!RegisterClassEx(&Exp1)) // 如果窗口类注册未成功
MessageBox(NULL,_T("窗口类注册失败!!!"),_T("错误!"),NULL);
/* else
MessageBox(NULL,_T("窗口类注册成功!!!"),_T("恭喜!"),NULL); */
//
hInst=hInstance;
//
// 开始创建窗口
HWND hWnd=CreateWindow(
szWindowClass,szTitle,WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME,
60,30,1024,768,NULL,NULL,hInstance,NULL);
//
// 窗口创建相关
if (!hWnd)
MessageBox(NULL,_T("创建窗口失败!!!"),_T("Oops!"),NULL);
/* else
MessageBox(NULL,_T("创建窗口成功!!!"),_T("恭喜!"),NULL); */
//
ShowWindow(hWnd,nCmdShow);
// UpdateWindow(hWnd);
//
// 主循环
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam; // 强制类型转换!
}
//

// 具体实现函数
LRESULT CALLBACK WndProcExp1(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
int CtrlID=LOWORD(wParam);
hFont=CreateFont(-12,0,0,0,FW_THIN,false,false,false,GB2312_CHARSET,
OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,
FF_MODERN,_T("微软雅黑"));
SelectObject(hdc,hFont);
HWND hBtnPlayBgm,hBtnAbout,hBtnAboutAuthor,hBtnStopBgm,hBtnArea1;
switch(message)
{
// 左键按下
case WM_LBUTTONDOWN:
MessageBox(hWnd,_T("您点击了左键"),_T("Tip"),NULL);
break;
// 右键按下
case WM_RBUTTONDOWN:
MessageBox(hWnd,_T("您点击了右键"),_T("Tip"),NULL);
break;
// 绘制相关
case WM_CREATE:
// 绘制区域1
hBtnArea1 = CreateWindow(_T("button"),_T("这里是区域1"),WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
5,5,1008,480,hWnd,(HMENU)0001,((LPCREATESTRUCT)lParam)->hInstance,NULL);
SendMessage(hBtnArea1,WM_SETFONT,(WPARAM)hFont,1);
// 关于按钮
hBtnAbout = CreateWindow(_T("button"),_T("关于程序"),WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
945,710,70,24,hWnd,(HMENU)0002,((LPCREATESTRUCT)lParam)->hInstance,NULL);
SendMessage(hBtnAbout,WM_SETFONT,(WPARAM)hFont,1);
// 关于作者
hBtnAboutAuthor = CreateWindow(_T("button"),_T("关于作者"),WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
870,710,70,24,hWnd,(HMENU)0003,((LPCREATESTRUCT)lParam)->hInstance,NULL);
SendMessage(hBtnAboutAuthor,WM_SETFONT,(WPARAM)hFont,1);
// 播放BGM
hBtnPlayBgm = CreateWindow(_T("button"),_T("播放BGM"),WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
795,710,70,24,hWnd,(HMENU)0004,((LPCREATESTRUCT)lParam)->hInstance,NULL);
SendMessage(hBtnPlayBgm,WM_SETFONT,(WPARAM)hFont,1);
// 停止BGM
hBtnStopBgm = CreateWindow(_T("button"),_T("停止BGM"),WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
720,710,70,24,hWnd,(HMENU)0005,((LPCREATESTRUCT)lParam)->hInstance,NULL);
SendMessage(hBtnStopBgm,WM_SETFONT,(WPARAM)hFont,1);
// 其他
break;
// 消息响应
case WM_COMMAND:
switch(CtrlID)
{
case 0001:
break;
case 0002:
PlayTipWav; // 播放声音
MessageBox(hWnd,_T("本示例适合入门的sdk开发者\n\n By ragnaroks"),_T("关于"),NULL);
break;
case 0003:
ShellExecute(NULL,_T("open"),_T("http://www.ragnaroks.org/about"),NULL,NULL,SW_SHOW);
break;
case 0004:
PlaySound(_T("res\\BGM.wav"),NULL, SND_FILENAME | SND_ASYNC);
break;
case 0005:
PlaySound(NULL,NULL, SND_FILENAME | SND_ASYNC);
break;
}
break;
// 绘制相关
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// 绘制设备上下文

// 结束绘制
EndPaint(hWnd, &ps);
break;
// 程序退出
case WM_DESTROY:
PostQuitMessage(0);
break;
// 其他
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
[/cpp]
[对比图在下面]:
[图跪了,当初也没别的备份]