#include <iostream>
#include <Windows.h>
using namespace std;
HINSTANCE g_hInstance;
HWND hEdit, hButton;
static const char* CLASS_NAME = "MYCLASS";
HFONT SetMyFont(HDC hdc,LPCTSTR face ,int width,int height,int angle)
{
HFONT hFont;
hFont = CreateFont(
height, //字体的逻辑高度
width, //逻辑平均字符宽度
angle, //与水平线的角度
0, //基线方位角度
FW_REGULAR, //字形:常规
FALSE, //字形:斜体
FALSE, //字形:下划线
FALSE, //字形:粗体
GB2312_CHARSET, //字符集
OUT_DEFAULT_PRECIS, //输出精度
CLIP_DEFAULT_PRECIS, //剪截精度
PROOF_QUALITY, //输出品质
FIXED_PITCH | FF_MODERN, //倾斜度
face //字体
);
return hFont;
}
LRESULT CALLBACK onMainWndMessage(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static int cxChar, cyChar ;
switch (msg)
{
HDC hdc;
HFONT hFont;
PAINTSTRUCT ps;
case WM_CREATE :
hButton=CreateWindowEx(0,"BUTTON","打开",WS_CHILD | WS_VISIBLE | BS_FLAT | WS_BORDER,220,10,40,20,hwnd,NULL,((LPCREATESTRUCT) lParam)->hInstance,NULL);
CreateWindowEx(0,"Edit","Edit",WS_VISIBLE | WS_CHILD,10,10,50,20,hwnd,(HMENU)1000,g_hInstance,NULL);
CreateWindowEx(0,"Button","Button",WS_VISIBLE | WS_CHILD,15,30,35,20,hwnd,(HMENU)1001,g_hInstance,NULL);
break;
case WM_COMMAND:
if (LOWORD(wParam) == 1001 &&
HIWORD(wParam) == BN_CLICKED)
{
/* Our button was clicked. Close the window. */
//DestroyWindow (hwnd);
MessageBox(hwnd,"Button Clicked","Plain API",MB_OK);
}
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
DrawText(hdc, "hellow", (int)strlen("hellow"), &ps.rcPaint,
DT_CENTER|DT_SINGLELINE|DT_VCENTER); //显示文字(横向对中|不自动回行|纵向对中)
RECT rt1;
GetClientRect(hwnd,&rt1);
int x,y;
x = rt1.right / 2, y = rt1.bottom / 2;
//这2句是在窗口中间画横竖二条线,后面章节有详细说明
MoveToEx(hdc,x,0,NULL), LineTo(hdc,x,rt1.bottom);
MoveToEx(hdc,0,y,NULL), LineTo(hdc,rt1.right,y);
hFont = SetMyFont(hdc, (LPCTSTR)"宋体", 0, 32, -450);//创建字体
SelectObject(hdc, hFont);
SetTextColor(hdc,16711935);
SetBkMode(hdc, TRANSPARENT); //设置背景为透明
DrawText(hdc,"真的退出真的退出",strlen("真的退出真的退出"),&rt1,DT_WORDBREAK);
SetTextCharacterExtra(hdc, 6);
DrawText(hdc,"真中国地在奇巧大规模",strlen("真中国地在奇巧大规模"),&rt1,DT_WORDBREAK);
break;
case WM_CLOSE:
if(IDOK==MessageBox(NULL,"真的退出?","确认",MB_OKCANCEL|MB_ICONQUESTION))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
bool registerMyClass(HINSTANCE hInstance)
{
WNDCLASSEX wce = {0};
wce.cbSize = sizeof(wce);
wce.style = CS_VREDRAW|CS_HREDRAW;
wce.lpfnWndProc = &onMainWndMessage;
wce.hInstance = hInstance;//GetModuleHandle(0);
wce.hIcon = LoadIcon(0,MAKEINTRESOURCE(IDI_APPLICATION));
wce.hCursor = LoadCursor(0,MAKEINTRESOURCE(IDC_ARROW));
wce.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wce.lpszClassName = CLASS_NAME;
wce.hIconSm = wce.hIcon;
return 0!=RegisterClassEx(&wce);
}
bool createMyWindow(int cmdShow,HINSTANCE hInstance)
{
HWND mainWnd = CreateWindowEx(0,CLASS_NAME,"Demo", WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, 0,hInstance, 0);
if(0!=mainWnd)
{
ShowWindow(mainWnd,cmdShow);
UpdateWindow(mainWnd);
return true;
} else
{
return false;
}
}
int messageLoop()
{
MSG msg;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
class Drive
{
public:
Drive()
{
cout << "aa" << endl;
}
};
Drive d;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, char *, int cmdShow) {
g_hInstance = hInstance;
if (registerMyClass(hInstance) && createMyWindow(cmdShow,hInstance)) {
return messageLoop();
} else {
MessageBoxA(0, "创建失败", 0, MB_OK | MB_ICONSTOP);
return 0;
}
}
#include <Windows.h>
using namespace std;
HINSTANCE g_hInstance;
HWND hEdit, hButton;
static const char* CLASS_NAME = "MYCLASS";
HFONT SetMyFont(HDC hdc,LPCTSTR face ,int width,int height,int angle)
{
HFONT hFont;
hFont = CreateFont(
height, //字体的逻辑高度
width, //逻辑平均字符宽度
angle, //与水平线的角度
0, //基线方位角度
FW_REGULAR, //字形:常规
FALSE, //字形:斜体
FALSE, //字形:下划线
FALSE, //字形:粗体
GB2312_CHARSET, //字符集
OUT_DEFAULT_PRECIS, //输出精度
CLIP_DEFAULT_PRECIS, //剪截精度
PROOF_QUALITY, //输出品质
FIXED_PITCH | FF_MODERN, //倾斜度
face //字体
);
return hFont;
}
LRESULT CALLBACK onMainWndMessage(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static int cxChar, cyChar ;
switch (msg)
{
HDC hdc;
HFONT hFont;
PAINTSTRUCT ps;
case WM_CREATE :
hButton=CreateWindowEx(0,"BUTTON","打开",WS_CHILD | WS_VISIBLE | BS_FLAT | WS_BORDER,220,10,40,20,hwnd,NULL,((LPCREATESTRUCT) lParam)->hInstance,NULL);
CreateWindowEx(0,"Edit","Edit",WS_VISIBLE | WS_CHILD,10,10,50,20,hwnd,(HMENU)1000,g_hInstance,NULL);
CreateWindowEx(0,"Button","Button",WS_VISIBLE | WS_CHILD,15,30,35,20,hwnd,(HMENU)1001,g_hInstance,NULL);
break;
case WM_COMMAND:
if (LOWORD(wParam) == 1001 &&
HIWORD(wParam) == BN_CLICKED)
{
/* Our button was clicked. Close the window. */
//DestroyWindow (hwnd);
MessageBox(hwnd,"Button Clicked","Plain API",MB_OK);
}
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
DrawText(hdc, "hellow", (int)strlen("hellow"), &ps.rcPaint,
DT_CENTER|DT_SINGLELINE|DT_VCENTER); //显示文字(横向对中|不自动回行|纵向对中)
RECT rt1;
GetClientRect(hwnd,&rt1);
int x,y;
x = rt1.right / 2, y = rt1.bottom / 2;
//这2句是在窗口中间画横竖二条线,后面章节有详细说明
MoveToEx(hdc,x,0,NULL), LineTo(hdc,x,rt1.bottom);
MoveToEx(hdc,0,y,NULL), LineTo(hdc,rt1.right,y);
hFont = SetMyFont(hdc, (LPCTSTR)"宋体", 0, 32, -450);//创建字体
SelectObject(hdc, hFont);
SetTextColor(hdc,16711935);
SetBkMode(hdc, TRANSPARENT); //设置背景为透明
DrawText(hdc,"真的退出真的退出",strlen("真的退出真的退出"),&rt1,DT_WORDBREAK);
SetTextCharacterExtra(hdc, 6);
DrawText(hdc,"真中国地在奇巧大规模",strlen("真中国地在奇巧大规模"),&rt1,DT_WORDBREAK);
break;
case WM_CLOSE:
if(IDOK==MessageBox(NULL,"真的退出?","确认",MB_OKCANCEL|MB_ICONQUESTION))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
bool registerMyClass(HINSTANCE hInstance)
{
WNDCLASSEX wce = {0};
wce.cbSize = sizeof(wce);
wce.style = CS_VREDRAW|CS_HREDRAW;
wce.lpfnWndProc = &onMainWndMessage;
wce.hInstance = hInstance;//GetModuleHandle(0);
wce.hIcon = LoadIcon(0,MAKEINTRESOURCE(IDI_APPLICATION));
wce.hCursor = LoadCursor(0,MAKEINTRESOURCE(IDC_ARROW));
wce.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wce.lpszClassName = CLASS_NAME;
wce.hIconSm = wce.hIcon;
return 0!=RegisterClassEx(&wce);
}
bool createMyWindow(int cmdShow,HINSTANCE hInstance)
{
HWND mainWnd = CreateWindowEx(0,CLASS_NAME,"Demo", WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, 0,hInstance, 0);
if(0!=mainWnd)
{
ShowWindow(mainWnd,cmdShow);
UpdateWindow(mainWnd);
return true;
} else
{
return false;
}
}
int messageLoop()
{
MSG msg;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
class Drive
{
public:
Drive()
{
cout << "aa" << endl;
}
};
Drive d;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, char *, int cmdShow) {
g_hInstance = hInstance;
if (registerMyClass(hInstance) && createMyWindow(cmdShow,hInstance)) {
return messageLoop();
} else {
MessageBoxA(0, "创建失败", 0, MB_OK | MB_ICONSTOP);
return 0;
}
}