Oracle VM VirtualBox 管理器->菜单->管理->虚拟介质管理
popen返回NULL
当程序写得越来越大,进程占用的内存也就越来越多,调用popen时会返回空的FILE指针,网上说原因是system或popen这样的系统函数,其内部实现是调用fork函数创建子进程,创建过程中会复制父进程堆、栈等资源,这样就容易造成创建失败,返回NULL。
以下是我写的用vfork替换fork调用的Vpopen类。
Vpopen.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #pragma once #include <stdio.h> #include <sys/types.h> #include <unistd.h> class Vpopen { FILE *fp_; pid_t pid_; int pipeFd_[2]; public: Vpopen(); ~Vpopen(); FILE *open(const char *cmd, const char *flags); void close(); }; |
win32渐变颜色矩形函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #define FILL_STYLE_VERTICAL 0 #define FILL_STYLE_HORIZONTAL 1 void fillGradientRect(HDC hdc, RECT *rc, COLORREF fromRGB, COLORREF toRGB, int fillStyle) { BITMAPINFO bmpInfo; memset(&bmpInfo, 0, sizeof(bmpInfo)); bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth = rc->right - rc->left; bmpInfo.bmiHeader.biHeight = rc->bottom - rc->top; bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = 32; bmpInfo.bmiHeader.biCompression = BI_RGB; BYTE *rgbBuffer = (BYTE *)malloc(bmpInfo.bmiHeader.biWidth * bmpInfo.bmiHeader.biHeight * 4); int width = bmpInfo.bmiHeader.biWidth; int height = bmpInfo.bmiHeader.biHeight; int rStart = fromRGB & 0xFF; int rEnd = toRGB & 0xFF; int gStart = (fromRGB & 0xFF00) >> 8; int gEnd = (toRGB & 0xFF00) >> 8; int bStart = (fromRGB & 0xFF0000) >> 16; int bEnd = (toRGB & 0xFF0000) >> 16; if (FILL_STYLE_VERTICAL == fillStyle) { double rStep = (rEnd - rStart) * 1.0 / height; double gStep = (gEnd - gStart) * 1.0 / height; double bStep = (bEnd - bStart) * 1.0 / height; for (int y = 0; y < height; y++) { BYTE r = (BYTE)(rStart + rStep * y); BYTE g = (BYTE)(gStart + gStep * y); BYTE b = (BYTE)(bStart + bStep * y); for (int x = 0; x < width; x++) { BYTE *p = rgbBuffer + ((height - y) * width - (width - x)) * 4; *p++ = b; *p++ = g; *p++ = r; } } } else { double rStep = (rEnd - rStart) * 1.0 / width; double gStep = (gEnd - gStart) * 1.0 / width; double bStep = (bEnd - bStart) * 1.0 / width; for (int x = 0; x < width; x++) { BYTE r = (BYTE)(rStart + rStep * x); BYTE g = (BYTE)(gStart + gStep * x); BYTE b = (BYTE)(bStart + bStep * x); for (int y = 0; y < height; y++) { BYTE *p = rgbBuffer + ((height - y) * width - (width - x)) * 4; *p++ = b; *p++ = g; *p++ = r; } } } StretchDIBits(hdc, rc->left, rc->top, width, height, 0, 0, width, height, rgbBuffer, &bmpInfo, DIB_RGB_COLORS, SRCCOPY); free(rgbBuffer); } void CMFCApplicationDlg::OnPaint() { CPaintDC dc(this); CRect rc(50, 50, 200, 200); fillGradientRect(dc.m_hDC, rc, RGB(255, 0, 255), RGB(0, 255, 0), FILL_STYLE_HORIZONTAL); CDialogEx::OnPaint(); } |
win32发消息触发窗口拖动
1 2 3 | ::ReleaseCapture(); ::SendMessage(m_hWnd, WM_NCLBUTTONDOWN, HTCAPTION, NULL); ::SendMessage(m_hWnd, WM_LBUTTONUP, NULL, NULL); |
win32响应WM_NCHITTEST消息后窗口未改变大小
对于popup style的窗体,要将其System Menu属性改为false,OnNcHitTest的返回正确值后,窗口才会改变大小。
win32限制改变窗口大小
响应WM_SIZING消息,消息函数中做如下处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | void CDemoDlg::OnSizing(UINT fwSide, LPRECT pRect) { if (pRect->right - pRect->left < minWidth_) { if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT || fwSide == WMSZ_BOTTOMLEFT) { pRect->left = pRect->right - minWidth_; } else pRect->right = pRect->left + minWidth_; } if (pRect->bottom - pRect->top < memHdcHeight_) { if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT || fwSide == WMSZ_TOPRIGHT) { pRect->top = pRect->bottom - minHeight_; } else pRect->bottom = pRect->top + minHeight_; } CDialogEx::OnSizing(fwSide, pRect); } |
如何让安卓APP第二次插上USB设备后不弹系统权限提示窗
方法是把Android App做成随USB设备插入时自启动就可以了。
具体做法是在App的AndroidManifest.xml里,把要自启动activity的地方加两处USB_DEVICE_ATTACHED就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 | <activity android:name="com.serenegiant.usbcameratest2.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/> </activity> |
refer to: 梦幻贝壳, https://blog.csdn.net/menghuanbeike/article/details/78752715
Javascript技巧
1 2 3 4 5 | <style style="display:block" contentEditable="true"> body { background:rgb(255, 255, 255); } </style> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | const movies = { list: [ { title: 'Heat' }, { title: 'Interstellar' }, { title: 'Savior' } ], [Symbol.iterator]() { let index = 0; return { next: () => { if (index < this.list.length) { const value = this.list[index++].title; return { value, done: false }; } return { done: true }; } }; } }; const [, secondMovieTitle = 'NonTitle'] = movies; console.log(secondMovieTitle); big = { foo: 'value Foo', bar: 'value Bar' }; var { foo = 'Unk' } = big; console.log(foo); var { ['foo']: name = 'Unknown' } = big; console.log(name); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | obj1 = { a() { console.log(this); }, b: 'hi', c: () => { console.log(this); } }; obj1.a(); obj1.c(); A1 = function() { console.log(this); }; a1 = new A1; A1(); |
Fall-back from the nopython compilation path to the object mode compilation
出现此提示的原因是用@jit标注的函数内存在非数值运算语句。
比如下例中,time()函数和print()函数都不能出现在foo()函数中。正确写法为:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import numba as nb import time @nb.jit def foo(x, y): s = 0 for i in range(x, y): s += i return s tt = time.time() print(foo(1, 100000000)) print('Time used: {} secs'.format(time.time() - tt)) |
本地的Documentation for Android SDK浏览还是太慢?
在Android Studio中,通过SDK Manager将Documentation for Android SDK下载到本地后,在Android SDK目录下找到docs\index.html,在浏览器中打开,发现加载还是好慢,原因是本地网页又去访问互联网了。
想到的办法就是阻止浏览器访问外网,可参照
在此Chrome插件中加入以下阻止前缀字符串
1 | http://www.google.com/jsapi |
就可在Chrome中极速查看安卓SDK文档了。