#include <Windows.h>
#include <stdlib.h>
#define IDM_OPT1 301
#define IDM_OPT2 302
#define IDM_BTN1 303
#define IDM_EDT1 304
#define IDM_EDT2 305
#define IDM_EDT3 306
LRESULT CALLBACK WindowProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
HMENU hRoot;
HWND hwndButton;
HWND hwnText;
HWND hwnText1;
HWND hwnText2;
void CreateMyMenu();//建立菜單
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
){
CreateMyMenu();
WCHAR* cls_Name = L"My Class";
WNDCLASS wc = {sizeof(WNDCLASS)};
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = cls_Name;
wc.hInstance = hInstance;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
HWND hwnd = CreateWindow(
cls_Name,//類名
L"我的應用程式",//視窗标題文字
WS_OVERLAPPEDWINDOW,//視窗外觀樣式
38,//視窗相對于父級的x坐标
20,//視窗相對于父級的y坐标
800,//視窗的寬度
600,//視窗的高度
NULL,//沒有父視窗,為NULL
hRoot,//沒有菜單,為NULL
hInstance,//目前應用程式的執行個體句柄
NULL//沒有附加資料,為NULL
if (hwnd == NULL){
return 0;
}
//ShowWindow的第一個參數就是剛才建立的視窗的句柄,第二個參數控制視窗如何顯示
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
switch (uMsg)
{
case WM_CREATE:{
hwndButton = CreateWindow(
TEXT("button"),
TEXT("anniu"),
WS_CHILD | WS_VISIBLE,
100,80,80,40,
hwnd, (HMENU)IDM_BTN1,
((LPCREATESTRUCT)lParam)->hInstance,NULL
//預設帶邊框
hwnText = CreateWindow(
TEXT("EDIT"),
TEXT("hello"),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
250,50,120,40,hwnd,
(HMENU)IDM_EDT1,
((LPCREATESTRUCT)lParam)->hInstance, NULL
//擴充3D樣式
hwnText1 = CreateWindowEx(WS_EX_CLIENTEDGE,
WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL | ES_PASSWORD,
250,100,120,40,hwnd,
(HMENU)IDM_EDT2, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
hwnText2 = CreateWindowEx(
WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT("hello"),
WS_VSCROLL|WS_VISIBLE|WS_CHILD|ES_AUTOVSCROLL|ES_MULTILINE
, 250, 150, 120, 100, hwnd, (HMENU)IDM_EDT3, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
MoveWindow(hwnText2,250,200,120,200,TRUE);
case WM_DESTROY:{
PostQuitMessage(0);
case WM_COMMAND:{
switch (LOWORD(wParam))
case IDM_OPT1:
MessageBox(hwnd, L"feiji", L"tips", MB_OK);
break;
case IDM_OPT2:
MessageBox(hwnd, L"paodan", L"tips", MB_OK);
case IDM_BTN1:
wchar_t *szName;
int iLength;
iLength = GetWindowTextLength(hwnText);
szName = (wchar_t*)malloc(iLength + 6);
wcscat_s(szName,iLength+ 6, L"--New");
SetWindowText(hwnText, szName);
MessageBox(hwnd, L"this is a btn", L"tips", MB_OK);
default:
case WM_PAINT:{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
SetTextColor(ps.hdc, RGB(10, 0, 255));//設定文本顔色
DrawText(ps.hdc, L"朋友,你好", -1, &(ps.rcPaint), DT_CENTER);
//用于設定每個字元間隔的數組
int arr1[2] = { 45, 0 };
int arr2[3] = { 35, 40, 0 };
int arr3[2] = { 32, 0 };
POLYTEXT polys[] = {
{ 2, 2, 3, L"大家", ETO_CLIPPED, ps.rcPaint, &arr1[0] },
{ 2, 25, 3, L"新年好", ETO_CLIPPED, ps.rcPaint, &arr2[0] },
{ 30, 60, 3, L"快樂\0", ETO_CLIPPED, ps.rcPaint, &arr3[0] }
};
PolyTextOut(ps.hdc, &polys[0], 3);
//填充圖形
//建立畫刷
HBRUSH hb = CreateSolidBrush(RGB(0,255,0));
//畫刷選擇到目前DC中
HBRUSH orgBrs = (HBRUSH)SelectObject(ps.hdc, hb);
Ellipse(ps.hdc, 135, 35, 202, 170);
//選回原先的畫刷
SelectObject(ps.hdc, orgBrs);
DeleteObject(hb);
EndPaint(hwnd, &ps);
return DefWindowProc(hwnd, uMsg, wParam, lParam);
void CreateMyMenu(){
hRoot = CreateMenu();
if (!hRoot)
return;
HMENU pop1 = CreatePopupMenu();
AppendMenu(hRoot,
MF_POPUP,
(UINT_PTR)pop1,
L"操作");
AppendMenu(pop1,
MF_STRING,
IDM_OPT1,
L"飛機");
MENUITEMINFO mif;
mif.cbSize = sizeof(MENUITEMINFO);
mif.cch = 100;
mif.dwItemData = NULL;
mif.dwTypeData = L"機關槍";
mif.fMask = MIIM_ID | MIIM_STRING | MIIM_STATE;
mif.fState = MFS_ENABLED;
mif.fType = MIIM_STRING;
mif.wID = IDM_OPT2;
InsertMenuItem(pop1, IDM_OPT2, FALSE, &mif);