工程:
WinMain.h
-
#ifndef _WINMAIN_H_
-
#define _WINMAIN_H_
-
-
#include <windows.h>
-
-
#define SCREEN_WIDTH 600
-
#define SCREEN_HEIGHT 400
-
-
/************************************************************************/
-
/* WinApplication窗口回调函数 */
-
/************************************************************************/
-
LRESULT CALLBACK WinLesson1Pro(
-
_In_ HWND hwnd,
-
_In_ UINT uMsg,
-
_In_ WPARAM wParam,
-
_In_ LPARAM lParam
-
);
-
-
/************************************************************************/
-
/* 消息提示框+处理事件 */
-
/************************************************************************/
-
int DisplayMessageBox(HWND hwnd, char * lpText, char * lpCaption, UINT uType);
-
-
#endif //_WINMAIN_H_
WinMain.cpp
-
#include "WinMain.h"
-
#include <stdio.h>
-
-
int WINAPI WinMain(
-
HINSTANCE hInstance,
-
HINSTANCE hPrevInstance,
-
/*LPWSTR*/LPSTR lpCmdLine,
-
int nShowCmd
-
)
-
{
-
WNDCLASS wndcls;
-
-
///< 构造窗体类
-
wndcls.cbClsExtra = 0;
-
wndcls.cbWndExtra = 0;
-
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
-
wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
-
wndcls.hIcon = LoadIcon(NULL, IDI_WINLOGO);
-
wndcls.hInstance = hInstance;
-
wndcls.lpfnWndProc = WinLesson1Pro;
-
wndcls.lpszClassName = "lesson1";
-
wndcls.lpszMenuName = NULL;
-
wndcls.style = CS_HREDRAW | CS_VREDRAW;
-
-
///< 注册窗体
-
RegisterClass(&wndcls);
-
-
///< 创建窗口
-
HWND hwnd;
-
hwnd = CreateWindow("lesson1", "小豆第一课学习",
-
WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
-
NULL, NULL, hInstance, NULL);
-
if (!hwnd)
-
{
-
return -1;
-
}
-
-
///< 显示窗口
-
ShowWindow(hwnd, SW_SHOWNORMAL);
-
-
///< 更新窗口
-
UpdateWindow(hwnd);
-
-
MSG msg;
-
///< 消息循环
-
while(GetMessage(&msg, hwnd, 0, 0) > 0)
-
{
-
TranslateMessage(&msg); ///< 转换消息
-
DispatchMessage(&msg); ///< 分发消息【消息被分发到回调函数(过程函数)】
-
}
-
-
return 0;
-
}
-
-
LRESULT CALLBACK WinLesson1Pro(
-
_In_ HWND hwnd,
-
_In_ UINT uMsg,
-
_In_ WPARAM wParam,
-
_In_ LPARAM lParam
-
)
-
{
-
HDC hdc;
-
char szChar[20];
-
-
switch (uMsg)
-
{
-
case WM_CHAR:
-
// The keyboard.
-
sprintf(szChar, "char is %c", wParam);
-
DisplayMessageBox(hwnd, szChar, "消息", 0);
-
break;
-
-
case WM_LBUTTONDOWN:
-
// The left mouse button.
-
DisplayMessageBox(hwnd, "The left mouse clicked", "鼠标左键", 0);
-
///< Text out
-
hdc = GetDC(hwnd);
-
TextOut(hdc, 0, 50, "文字", strlen("文字"));
-
ReleaseDC(hwnd, hdc);
-
break;
-
-
case WM_RBUTTONDOWN:
-
// The right mouse button.
-
break;
-
-
case WM_CLOSE:
-
// Close the window.
-
if (IDYES == DisplayMessageBox(hwnd, "是否结束?", "关闭", MB_YESNO))
-
{
-
DestroyWindow(hwnd);
-
return 0;
-
}
-
break;
-
-
case WM_CREATE:
-
// Initialize the window.
-
//DisplayResourceNAMessageBox(hwnd, "创建", "WM_CREATE", MB_HELP);
-
return 0;
-
-
case WM_PAINT:
-
// Paint the window's client area.
-
PAINTSTRUCT ptruct;
-
hdc = BeginPaint(hwnd, &ptruct);
-
TextOut(hdc, 0, 0, "绘制", strlen("绘制"));
-
EndPaint(hwnd, &ptruct);
-
return 0;
-
-
case WM_SIZE:
-
// Set the size and position of the window.
-
//DisplayResourceNAMessageBox(hwnd, "大小", "WM_SIZE", MB_HELP);
-
return 0;
-
-
case WM_DESTROY:
-
// Clean up window-specific data objects.
-
PostQuitMessage(0);
-
return 0;
-
-
default:
-
return DefWindowProc(hwnd, uMsg, wParam, lParam);
-
}
-
return 0;
-
}
-
-
int DisplayMessageBox(HWND hwnd, char * lpText, char * lpCaption, UINT uType)
-
{
-
int msgboxID = MessageBox(hwnd, lpText, lpCaption, uType);
-
-
switch (msgboxID)
-
{
-
case IDCANCEL:
-
// TODO: add code
-
break;
-
case IDTRYAGAIN:
-
// TODO: add code
-
break;
-
case IDCONTINUE:
-
// TODO: add code
-
break;
-
}
-
-
return msgboxID;
-
}
运行:
阅读(1740) | 评论(0) | 转发(0) |