読者です 読者をやめる 読者になる 読者になる

One Algorithm to Generate Gaussian Distribution in C

MD
#include <stdlib.h>
#include <math.h>
 
double gaussrand()
{
    static double V1, V2, S;
    static int phase = 0;
    double X;
     
    if ( phase == 0 ) {
        do {
            double U1 = (double)rand() / RAND_MAX;
            double U2 = (double)rand() / RAND_MAX;
             
            V1 = 2 * U1 - 1;
            V2 = 2 * U2 - 1;
            S = V1 * V1 + V2 * V2;
        } while(S >= 1 || S == 0);
         
        X = V1 * sqrt(-2 * log(S) / S);
    } else
        X = V2 * sqrt(-2 * log(S) / S);
         
    phase = 1 - phase;
 
    return X;
}

blog.csdn.net

Win32 API绘图避免闪烁

Windows

首先在创建窗口时候设置背景为NULL

wc.hbrBackground = NULL;

然后在WM_CREATE中

hdc = GetDC(hWnd); //获取设备
hdcBuffer = CreateCompatibleDC(hdc); //给设备分配一个内存空间
hBitMap = CreateCompatibleBitmap(hdc, 1200, 700); //创建一个cxClient, cyClient大小并且适应DC设备环境的位图
ReleaseDC(hWnd, hdc);
SelectObject(hdcBuffer, hBitMap); //将位图设置为hdcBuffer的画刷

在WM_PAINT函数中画图

PatBlt(hdcBuffer, 0, 0, 1200, 700, WHITENESS);
{Draw something;}
BitBlt(hdc, 0, 0, 1200, 700, hdcBuffer, 0, 0, SRCCOPY);

参考

blog.csdn.net

Win32API窗口输出浮点数

Windows

使用TextOut()在窗口中输出数值时一般的方式是,先将数值存到字符数组,再打印出来:

wsprintf(szData, TEXT("Data = %d"), data);

TextOut(hdc, x, y, szData, lstrlen(szData));

有一个问题是, wsprintf()这个函数无法处理浮点数。这里需要将其替换成sprintf():

sprintf(szData, "Data = %f",data);

替换后在本人使用平台(Visual C++ 2000 Express Chinese Simplified)出现乱码,发现需要改变字符集,修改方法为:

1.中文版:

项目-》设置属性-》配置属性-》常规-》项目默认值-》字符集

2.英文版:

Project-》Properties-》Configuration Properties-》General-》Project Defaults-》Character Set

参考

blog.csdn.net

Real Loop in Win32 Program

Windows

To introduce real loop in Win32 program. “GetMessage()” need to be replaced by “PeekMessage()” The traditional GetMessage loop looks like:

while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
        if (bRet == -1) {
            break;
        } else {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

The PeekMessage Program is like:

while(!Stop){
    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    DoCalculation();
    }

Ref:

关于Windows中的系统消息循环占用CPU的疑问

GetMessage函数是一个阻塞型的函数,当消息队列中没有消息时,GetMessage会处于阻塞状态。一旦有消息到达,进程会被唤醒,GetMessage马上返回。实现时,使用了一个信号量, GetMessage函数在确定没有消息可读时,对这个信号量进行一个V操作,从而使线程阻塞。而PostMessage、SendNotifyMessage、SendSyncMessage等任何一个发送消息函数在发送完消息之后,都会读取这个信号量的值,当发现这个值等于零时,即表示读消息的线程当前已阻塞,这时就会作一次P操作,来唤醒睡眠的线程。

blog.csdn.net

book.51cto.com

(Microsoft VC++ Express)LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

Windows

f:id:xuechui:20170318081909p:plain

There are several "cvtres.exe" in C disk, maybe installed by .NET Framework. Replace the old one in VC/bin by the first one(new?) in C disk.

f:id:xuechui:20170318081918p:plain

连接器LNK是通过调用cvtres.exe完成文件向coff格式的转换的,所以出现这种错误的原因就是cvtres.exe出现了问题。

 

在电脑里面搜索一下cvtres.exe,发现存在多个文件,使用最新的cvtres.exe替换老的文件即可,替换之前记得备份一下,如果不对,可以替换回来。


例如:我的电脑里面安装了vs2010,最近更新了系统,打了一些补丁,结果就出现这种错误了。在电脑里面搜索发现

C:\Program Files\Microsoft Visual Studio 10.0\VC\bin

C:\Windows\winsxs\x86_netfx-cvtres_for_vc_and_vb_b03f5f7f11d50a3a_6.1.7600.16385_none_ba476986f05abc65

C:\Windows\Microsoft.NET\Framework\v4.0.30319


这三个路径里面都有cvtres.exe文件,于是我尝试使用第二个路径里面的文件替换第一个路径的文件,问题解决。


参考资料如下:

http://stackoverflow.com/questions/10888391/link-fatal-error-lnk1123-failure-during-conversion-to-coff-file-invalid-or-c/14144713#14144713

blog.chinaunix.net

blog.csdn.net

マウスで円を描くコード

Windows

「猫でもわかるWindowsプログラミング」より

↓こんな感じ。 f:id:xuechui:20170227193417p:plain

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
ATOM InitApp(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
void DrawRect(HWND, POINTS, POINTS);

TCHAR szClassName[] = TEXT("Mouse1");
POINTS start, end, old_end;     //Start and End point of Rectangle


//windows prgram entry point
int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, 
                   LPSTR lpsCmdLine, int nCmdShow)
{
    MSG msg;
    BOOL bRet;
    
    if(!InitApp(hCurInst))
        return FALSE;
    if(!InitInstance(hCurInst, nCmdShow))
        return FALSE;
    
    //Get Message
    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0){
        if (bRet == -1){
            break;
        }else{
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int)msg.wParam;
}

//register window class
ATOM InitApp(HINSTANCE hInst)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInst;
    wc.hIcon = NULL;
    wc.hCursor = (HCURSOR)LoadImage(
    NULL, MAKEINTRESOURCE(IDC_ARROW),
    IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szClassName;
    wc.hIconSm = NULL;
    
    return (RegisterClassEx(&wc));
}

//Generage Window
BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
{
    HWND hWnd;
    
    hWnd = CreateWindow(szClassName, //Class Name
            TEXT("Neko demo Mouse"),     //Window Name
            WS_OVERLAPPEDWINDOW,    //Window Style
            CW_USEDEFAULT,          //x position
            CW_USEDEFAULT,          //y position
            CW_USEDEFAULT,          //Window Width
            CW_USEDEFAULT,          //Window Height
            NULL,
            NULL,
            hInst,
            NULL,
            );
            if (!hWnd)
                return FALSE;
            ShowWindow(hWnd, nCmdShow);
            UpdateWindow(hWnd);
            return TRUE;
}

//Window Procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    TCHAR szBuf[32];
    HDC hdc;
    HBRUSH hBrush;
    static BOOL bDraw;
    
    switch (msg){
        case WM_LBUTTONDOWN:
            bDraw = TRUE;
            old_end = start = MAKEPOINTS(lp);
            DrawRect(hWnd, start, old_end);
            break;
        case WM_MOUSEMOVE:
            if(bDraw){
                end = MAKEPOINTS(lp);
                DrawRect(hWnd, start, old_end);
                DrawRect(hWnd, start, end);
                old_end = end;
            }else{
                return DefWindowProc(hWnd, msg, wp, lp);
            }
            break;
        case WM_LBUTTONUP:
            if(bDraw){
                SetCursor(LoadCursor(NULL, IDC_ARROW));
                DrawRect(hWnd, start, end);
                bDraw = FALSE;
                
                wsprintf(szBuf, TEXT("(%d, %d) - (%d, %d)"),
                            start.x, start.y, end.x, end.y);
                SetWindowText(hWnd, szBuf);
                hdc = GetDC(hWnd);
                hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
                SelectObject(hdc, hBrush);
                Ellipse(hdc, start.x, start.y, end.x, end.y);
                ReleaseDC(hWnd, hdc);
            }else{
                return DefWindowProc(hWnd, msg, wp, lp);
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return (DefWindowProc(hWnd, msg, wp, lp));
    }
    return 0;
}



void DrawRect(HWND hWnd, POINTS beg, POINTS end)
{
    HDC hdc;
    hdc = GetDC(hWnd);
    SetROP2(hdc, R2_NOT);
    
    MoveToEx(hdc, beg.x, beg.y, NULL);
    LineTo(hdc, end.x, beg.y);
    LineTo(hdc, end.x, end.y);
    LineTo(hdc, beg.x, end.y);
    LineTo(hdc, beg.x, beg.y);
    LineTo(hdc, end.x, end.y);
    
    ReleaseDC(hWnd, hdc);
    return;
}