大家好,这是本人写的第一篇博客,如果没能让你有所收获还望见谅,有何错误还望批评,下面回到正题
1.作为win32初学者如何让自己写的程序,能在无编译器的电脑运行?
想必大家肯定经历过将程序放到别人电脑上,会出现.dll库文件缺失的情况,那么该如何解决呢?
首先确保你的程序版本是Release版本,然后你需要将项目属性改成如下情况
代码生成中的运行库选择多线程(/MT)
下面贴代码
#include<windows.h>#include<math.h>#include"resource.h"#define Radius 30*sqrt(2)/2LRESULT CALLBACK WinPRoc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hInprestance, PSTR pCmdLine, int iCmdLine){ WNDCLASS wndcls; wndcls.cbWndExtra = 0; wndcls.cbClsExtra = 0; wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hInstance = hInstance; wndcls.hCursor = LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = LoadIcon(hInstance, (LPSTR)IDI_ICON1); wndcls.lpfnWndProc = WinProc; wndcls.lpszClassName = TEXT("WinForm"); wndcls.lpszMenuName = NULL; wndcls.style = CS_VREDRAW | CS_HREDRAW; RegisterClass(&wndcls); HWND hwnd; hwnd = CreateWindow("WinForm", "Marco", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdLine); UpdateWindow(hwnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0;}void DrawHeart(HDC hdc,int i,int cxClient,int cyClient){ //使改变窗口心自动调整改变 SetMapMode(hdc, MM_ISOTROPIC); SetWindowExtEx(hdc, 120, 90, NULL); SetViewportExtEx(hdc, cxClient, cyClient, NULL); SetWindowOrgEx(hdc, 80, 75, NULL); SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL); POINT pt[4] = { 50,80,80,50,110,80,80,110 }; HPEN hpen[4]; HBRUSH hbrush[4]; COLORREF color[4] = { RGB(255,0,255),RGB(0,0,255),RGB(255,0,0),RGB(0,0,0) }; for (int j = 0; j < 4; j++) { hpen[j] = CreatePen(PS_SOLID, 0, color[j]); hbrush[j] = CreateSolidBrush(color[j]); } SelectObject(hdc, hpen[i]); SelectObject(hdc, hbrush[i]); Polygon(hdc, pt, 4); Ellipse(hdc, 65 - Radius, 65 - Radius, 65 + Radius, 65 + Radius); Ellipse(hdc, 95 - Radius, 65 - Radius, 95 + Radius, 65 + Radius); DeleteObject(hpen[i]); DeleteObject(hbrush[i]);}LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hdc; PAINTSTRUCT ps; COLORREF color[4] = { RGB(255,0,255),RGB(0,0,255),RGB(255,0,0),RGB(255,255,255) }; static HWND hwndstatic; static int cxClient, cyClient; static int i; int j; static HBRUSH hbrushstatic; static int music[3] = { 102,103,104 }; static TCHAR * speak[4] = { "自从遇见你,我们就保持暧昧的关系,虽然痛苦但是很甜蜜", "当我们互相坦露后,就像一阵幸福的风吹来,你我承诺会到永远", "可现实中我们仍然需要自己的空间,就像这红色的心看似相爱却早已心生抱怨", "渐渐我们的心散去,我们选择成全彼此,其实岁月留给我们的只是空白" }; switch (message) { case WM_CREATE : SetTimer(hwnd, 1, 1000 * 60 *4.5, NULL); PlaySound((LPSTR)IDR_WAVE1, NULL, SND_RESOURCE | SND_ASYNC); hwndstatic = CreateWindow("static", NULL, WS_CHILD |WS_VISIBLE, 0, 10, 600, 40, hwnd,(HMENU)1, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); hbrushstatic = CreateSolidBrush(RGB(255,255, 255)); return 0; case WM_TIMER: i++; if (i < 4 && i >= 1) PlaySound((LPSTR)music[i - 1], NULL, SND_ASYNC | SND_RESOURCE); InvalidateRect(hwnd, NULL, TRUE); return 0; case WM_SIZE : cxClient = LOWord(lParam); cyClient = HIWORD(lParam); return 0; case WM_PAINT : hdc = BeginPaint(hwnd, &ps); if (i < 4) { DrawHeart(hdc, i, cxClient, cyClient); SetWindowText(hwndstatic, speak[i]); } EndPaint(hwnd, &ps); return 0; case WM_CTLCOLORSTATIC : j = GetWindowLong((HWND)lParam, GWL_ID); if (j == 1) { SetTextColor((HDC)wParam, color[i]); SetBkColor((HDC)wParam, RGB(255, 255, 255)); return (LRESULT)hbrushstatic; } break; case WM_DESTROY : PostQuitMessage(0); return 0; default : return DefWindowProc(hwnd, message, wParam, lParam); } return 0;}
新闻热点
疑难解答