首页 > 代码库 > win32备忘

win32备忘

#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 = http://www.mamicode.com/NULL;

mif.dwTypeData = http://www.mamicode.com/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);

}


win32备忘