C++ 可视化窗口程序

第一次写出了个C++的可视化窗口,纪念一下,虽然是跟着孙鑫C++的教程视频做的,但好歹也算是熟悉了变成和运行的原理,这份教程还是真不错,就是年代有点久远了,应该是03年的吧

*注意:刚开始编译的时候显示GetStockObject()和TextOut()两个函数没有定义,调整一下codeblocks设置就行了,别的编译环境也一样,菜单栏Project ->Properties->Project settings 右下角->Project's build options->Linker settings->在Other linker options框格里面粘贴上-mwindows就行了

附上自己的代码


#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinSunProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
){
    switch(uMsg)
    {

    case WM_CHAR:
        char szChar[20];
        sprintf(szChar,"Char is %d",wParam);
        MessageBox(hwnd,szChar,"Weixin",0);
        break;
    case WM_LBUTTONDOWN:
        MessageBox(hwnd,"mouse clicked","weixin",0);
        HDC hdc;
        hdc=GetDC(hwnd);
        TextOut(hdc,0,50,"计算机",strlen("计算机"));
        ReleaseDC(hwnd,hdc);
        break;
    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT ps;
        hDC=BeginPaint(hwnd,&ps);
        TextOut(hDC,0,0,"微信",strlen("微信"));
        EndPaint(hwnd,&ps);
        break;
    case WM_CLOSE:
        if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO)){
            DestroyWindow(hwnd);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage((0));
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR LpCmdLine,
    int nCmdShow
){
    WNDCLASS wndcls;
    wndcls.cbClsExtra=0;
    wndcls.cbWndExtra=0;
    wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
    wndcls.hInstance=hInstance;
    wndcls.lpfnWndProc=WinSunProc;
    wndcls.lpszClassName="Weixin2003";
    wndcls.lpszMenuName=NULL;
    wndcls.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndcls);

    HWND hwnd;
    hwnd=CreateWindow("Weixin2003","北京微信科学技术中心",WS_OVERLAPPEDWINDOW,
                      0,0,600,400,NULL,NULL,hInstance,NULL);

    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    MSG msg;
    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章