windows设计
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008;
制图工具:office 2010 powerpoint;
硬件信息:7G-3 笔记本;
真言
有些事不能着急,但是有些事不能不知道,有些事必须等,有些事还得必须做,有些事就是这些事:
对于资金:少支出,多收入
对于时间:少玩耍,多工作
对于感情:少动情,多平淡
题目
用windows技术,创建自己的windows应用程序。
代码
// test.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" #define MAX_LOADSTRING 100 #define WM_SETCOLOR WM_USER+1 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text struct DrawStruct { POINT start; POINT end; bool use; int penwidth; COLORREF pencolor; int shape; }; LRESULT CALLBACK SetWidth(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); //void DrawDraw(HWND hwnd,int shape,POINT s,POINT e,COLORREF color,int width); void DrawDraw(HWND hwnd,DrawStruct *mydraw); LRESULT CALLBACK ColorProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); // Foward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TEST); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_TEST; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static DrawStruct * mydraw = new DrawStruct; static HWND hwndcolor; int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; static HDC hMemDC; TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) { case WM_CREATE: mydraw->pencolor = RGB(1,1,1); mydraw->penwidth = 1; mydraw->shape = 1; mydraw->use = false; break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; case IDM_LINE: mydraw->shape = 1; break; case IDM_Juxing: mydraw->shape = 2; break; case IDM_Tuoyuan: mydraw->shape = 3; break; case IDM_Width: DialogBoxParam(hInst,(LPCTSTR)IDD_Penwidth,hWnd,(DLGPROC)SetWidth,(LPARAM)&(mydraw->penwidth)); break; case IDM_Color: if(! hwndcolor) hwndcolor = CreateDialog(hInst,(LPCTSTR)IDD_Color,hWnd,(DLGPROC)ColorProc); ShowWindow(hwndcolor,SW_SHOW); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_SETCOLOR: mydraw->pencolor = (COLORREF)lParam; break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); RECT rt; GetClientRect(hWnd, &rt); DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_LBUTTONDOWN: mydraw->start.x = LOWORD(lParam); mydraw->start.y = HIWORD(lParam); mydraw->end = mydraw->start; mydraw->use = true; break; case WM_LBUTTONUP: if(mydraw->use == true) { //DrawDraw(hWnd,mydraw.shape,mydraw.start,mydraw.end,mydraw.pencolor,mydraw.penwidth); DrawDraw(hWnd,mydraw); mydraw->end.x= LOWORD(lParam); mydraw->end.y= HIWORD(lParam); DrawDraw(hWnd,mydraw); //DrawDraw(hWnd,mydraw.shape,mydraw.start,mydraw.end,mydraw.pencolor,mydraw.penwidth); } mydraw->use = false; break; case WM_MOUSEMOVE: if(mydraw->use == true) { DrawDraw(hWnd,mydraw); //DrawDraw(hWnd,mydraw.shape,mydraw.start,mydraw.end,mydraw.pencolor,mydraw.penwidth); mydraw->end.x= LOWORD(lParam); mydraw->end.y= HIWORD(lParam); //DrawDraw(hWnd,mydraw.shape,mydraw.start,mydraw.end,mydraw.pencolor,mydraw.penwidth); DrawDraw(hWnd,mydraw); } break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Mesage handler for about box. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } LRESULT CALLBACK SetWidth(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { static int * width; switch (message) { case WM_INITDIALOG: width = (int *)lParam; SetDlgItemInt(hDlg,IDC_Width,*width,TRUE); return TRUE; case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: BOOL abuse; (*width) = GetDlgItemInt(hDlg,IDC_Width,&abuse,TRUE); EndDialog(hDlg, LOWORD(wParam)); return TRUE; case IDCANCEL: EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } //void DrawDraw(HWND hwnd,int shape,POINT s,POINT e,COLORREF color,int width) void DrawDraw(HWND hwnd,DrawStruct * mydraw) { HPEN pen,oldpen; pen = CreatePen(PS_SOLID,mydraw->penwidth,mydraw->pencolor); HDC hdc = GetDC(hwnd); oldpen = (HPEN)SelectObject(hdc,pen); SetROP2(hdc,R2_NOTXORPEN); switch(mydraw->shape) { case 1: MoveToEx(hdc,mydraw->start.x,mydraw->start.y,NULL); LineTo(hdc,mydraw->end.x,mydraw->end.y); break; case 2: Rectangle(hdc,mydraw->start.x,mydraw->start.y,mydraw->end.x,mydraw->end.y); break; case 3: Ellipse(hdc,mydraw->start.x,mydraw->start.y,mydraw->end.x,mydraw->end.y); break; } SelectObject(hdc,oldpen); DeleteObject(pen); ReleaseDC(hwnd,hdc); } LRESULT CALLBACK ColorProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; int i; HBRUSH hbr; HPEN hpen; COLORREF color; switch (message) { case WM_INITDIALOG: //return TRUE; break; case WM_PAINT: hdc = BeginPaint(hDlg, &ps); // TODO: Add any drawing code here... RECT rt; GetClientRect(hDlg, &rt); for(i =0;i<rt.right/50;i++){ color = RGB(20*i,255-30*i,50+10*i); hbr = CreateSolidBrush(color); hpen = CreatePen(PS_SOLID,1,color); SelectObject(hdc,hbr); SelectObject(hdc,hpen); Rectangle(hdc,50*i,0,50*(i+1),50); color = RGB(255-30*i,30*i,50+10*i); hbr = CreateSolidBrush(color); hpen = CreatePen(PS_SOLID,1,color); SelectObject(hdc,hbr); SelectObject(hdc,hpen); Rectangle(hdc,50*i,50,50*(i+1),rt.bottom); } EndPaint(hDlg, &ps); break; case WM_LBUTTONDOWN: color = GetPixel(GetDC(hDlg),LOWORD(lParam),HIWORD(lParam)); SendMessage(GetParent(hDlg),WM_SETCOLOR,WPARAM(0L),LPARAM(color)); break; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { //EndDialog(hDlg, LOWORD(wParam)); ShowWindow(hDlg,SW_HIDE); return TRUE; } break; } return FALSE; }
代码压缩包
http://download.csdn.net/detail/cqs_2012/7582253
原文:http://blog.csdn.net/cqs_experiment/article/details/36419453