C language +win32api writes forms applications

  • 2020-05-09 18:54:45
  • OfStack

Saw 1 win32api programming knowledge, last night I search the Internet for many tutorials, a spruced-up version windows programming, code mangled, transcribing, translation to understand stiff, term is not professional, 1 windows. c programming, fuzzy, and using VC + + 6.0, because my computer can't vc + + 6.0, can only use vs2008, many operations and functions are not too 1 sample. Find a blog post on win32api programming on the web, copy it down, and compile it down to 10 errors... Dizzy dead.

Later, I referred to the following four steps and wrote MSDN in C language. I felt dizzy and my English was not good. In other words, kingsoft powerword translated the whole sentence of MSDN professionally.

Steps:
1. Registration window class;
2. Create forms;
3. Message loop;
4, write window message processing function.

Code:


#include <windows.h>
#include<tchar.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR szCmdLine, int nCmdShow)
{
    WNDCLASS wc;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;//(HBRUSH)GetStockObject();
    wc.lpszMenuName = NULL;
    wc.lpszClassName = _T("MyWindowClass");
    if (!RegisterClass(&wc))
    {
        MessageBox (NULL, _T(" Unable to register window class "), _T(" error "), MB_OK);
        return 0 ;
    }
    HWND newWindow = CreateWindow(
                                        _T("MyWindowClass"), 
                                        _T(" My first 1 a winapi The program "), 
                                        WS_OVERLAPPEDWINDOW, 
                                        0, 
                                        0, 
                                        CW_USEDEFAULT, 
                                        CW_USEDEFAULT, 
                                        NULL, 
                                        NULL, 
                                        hInstance, 
                                        NULL
                                    );
    if (NULL == newWindow)
    {
        MessageBox (NULL, _T(" Unable to create form "), _T(" error "), MB_OK);
        return 0;
    }
    ShowWindow(newWindow, nCmdShow);
    UpdateWindow(newWindow);
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
            case WM_DESTROY:
            {
                PostQuitMessage(0);
                break;
            }
            default :
                return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        return 0;
    }

It is a form with only the title bar, close button, minimize button, maximize/restore button, and display area.

Written in pure C, I'm so depressed, it's so long, win32api super macro, love and hate, bored to death


Related articles: