您好,登錄后才能下訂單哦!
#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);
}
return 0;
}
LRESULT CALLBACK WindowProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
){
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,
TEXT("EDIT"),
TEXT("hello"),
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);
}
return 0;
case WM_DESTROY:{
PostQuitMessage(0);
return 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);
break;
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);
break;
default:
break;
}
}
return 0;
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);
}
break;
default:
break;
}
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);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。