Create a window and display pictures under windows with Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This is a package (library) I have written:

Github.com/jthmath/winapi

This package implements the Golang encapsulation of some Windows API functions.


As for why it is not available (e.g. walk), I will explain it in other words. Let's take a look at the code first.


1th Step, Register window class

Func myregisterclass (Hinstance winapi. HINSTANCE)   (atom uint16, err error)  {    var wc winapi. WNDCLASS    WC. STYLE = WINAPI.CS_HREDRAW | WINAPI.CS_VREDRAW    WC. PFNWNDPROC = WNDPROC    WC. CBCLSEXTRA = 0    WC. CBWNDEXTRA = 0    WC. HINSTANCE = HINSTANCE    WC. HICON = 0    WC. Hcursor, err = winapi. Loadcursorbyid (0, winapi. Idc_arrow)     if err != nil {         RETURN    }    WC. Hbrbackground = winapi. COLOR_WINDOW + 1    WC. pszmenuname =  ""     WC. pszclassname =  "main window class"     WC. Hiconsmall&nbSp;= 0    return winapi. RegisterClass (&WC)}

2nd step, create a window

WND, err: = WINAPI. CreateWindow ("Main window Class", "Golang Windows Programming", WINAPI. Ws_overlappedwindow, 0, WINAPI. Cw_usedefault, WINAPI. Cw_usedefault, WINAPI. Cw_usedefault, WINAPI. Cw_usedefault, 0, 0, Inst, 0) if err! = Nil {fmt. Println ("Create Window Failed") return 0}FMT. Println ("Create Window succeeded", WND) winapi. ShowWindow (WND, Winapi.sw_show) winapi. UpdateWindow (WND)

3rd step, main message loop

var msg winapi. Msgmsg.message = WINAPI. Wm_quit + 1//Let it not be equal to WINAPI. Wm_quitfor WINAPI. GetMessage (&msg, 0, 0, 0) > 0 {winapi. TranslateMessage (&msg) winapi. DispatchMessage (&MSG)}

These steps are no different than writing Windows programs in C or C + +, except that the current function is Golang style.


Of course, you should also map the corresponding WM_PAINT message.

Func wndproc (Hwnd winapi. HWND, MESSAGE UINT32, WPARAM UINTPTR, LPARAM UINTPTR)  uintptr {     var htemp winapi. Handle    switch message {    case winapi. Wm_create:        htemp, _ = winapi. Loadimagebyname (0,  "D:\\0004.bmp",             winapi. Image_bitmap, 0, 0, winapi. Lr_loadfromfile)         hbitmap = winapi. Hbitmap (htemp)     case winapi. Wm_paint:        onpaint (hWnd)     case winapi . Wm_destroy:        winapi. PostQuitMessage (0)     case winapi. Wm_command:        oncommand (hwnd, wParam, lparam)     default:        return  winapi. DefWindowProc (Hwnd, message, wparam, lparam)     }     Return 0}func onpaint (Hwnd winapi. HWND)  {    var err error    var ps winapi. Paintstruct    hdc, err := winapi. BeginPaint (HWND, &PS)     if err != nil {         return    }    defer winapi. EndPaint (HWND, &PS)  // defer  finally comes in handy     // HDC MDC  = CREATECOMPATIBLEDC (hdc);     mdc, err := winapi. CreateCompatibleDC (HDC)     if err != nil {         return&nbSp;   }    defer winapi. DeleteDC (MDC)     winapi. SelectObject (Mdc, winapi. Hgdiobj (HBITMAP))     winapi. BitBlt (Hdc, 0, 0, 480, 640, mdc, 0, 0, winapi. Srccopy)}

The effect is as follows:


Full code

package mainimport  (     "FMT"      "Runtime"       "GITHUB.COM/JTHMATH/WINAPI")//  global variable VAR HBITMAP WINAPI. Hbitmapfunc init ()  {    runtime. Gomaxprocs (runtime. NUMCPU ())}func main ()  {    inst, err := winapi. GetModuleHandle ("")     if err != nil {         fmt. Println ("Get instance failed")         return    }     r := winmain (inst,  "",  0)     fmt. PRINTLN ("WinMain function returns",  r)}func winmain (Inst winapi. Hinstance, cmd string, ncmdshow int32)  int32 {    var  err error    // 1.  Register window class     atom, err :=  myregisterclass (Inst)     if err != nil {        fmt. PRINTLN ("Register window class failed")         return 0    }     fmt. PRINTLN ("Register window class succeeded",  atom)     // 2.  Create window     wnd, err  := winapi. CreateWindow ("Main window class",  "golang windows  Programming",         WINAPI . Ws_overlappedwindow, 0,        winapi. Cw_usedefault, winapi. Cw_usedefault, winapi. Cw_usedefault, winapi. cw_usedefault,        0, 0, inst, 0)      if err != nil {        fmt. Println ("Create Window failed")         return 0    }     fmt. Println ("Create Window succeeded",  wnd) &NBSp;   winapi. ShowWindow (wnd, winapi.sw_show)     winapi. UpdateWindow (WND)     // 3.  main message loop     var msg winapi . Msg    msg. Message = winapi. wm_quit + 1 //  make it not equal to  WINAPI. Wm_quit    for winapi. GetMessage (&msg, 0, 0, 0)  > 0 {         winapi. TranslateMessage (&msg)         winapi. DispatchMessage (&msg)     }    return int32 (Msg. WParam)}func wndproc (Hwnd winapi. HWND, MESSAGE UINT32, WPARAM UINTPTR, LPARAM UINTPTR)  uintptr {     var htemp winapi. Handle    switch message {    case winapi. Wm_create:     &nBsp;  htemp, _ = winapi. Loadimagebyname (0,  "D:\\0004.bmp",             winapi. Image_bitmap, 0, 0, winapi. Lr_loadfromfile)         hbitmap = winapi. Hbitmap (htemp)     case winapi. Wm_paint:        onpaint (hWnd)     case winapi . Wm_destroy:        winapi. PostQuitMessage (0)     case winapi. Wm_command:        oncommand (Hwnd, wparam, lparam)      default:        return winapi. DefWindowProc (Hwnd, message, wparam, lparam)     }     Return 0}func onpaint (Hwnd winapi. HWND)  {    var err  Error    var ps winapi. Paintstruct    hdc, err := winapi. BeginPaint (HWND, &PS)     if err != nil {         return    }    defer winapi. EndPaint (HWND, &PS)  // defer  finally comes in handy     // HDC MDC  = CREATECOMPATIBLEDC (hdc);     mdc, err := winapi. CreateCompatibleDC (HDC)     if err != nil {         return    }    defer winapi. DeleteDC (MDC)     winapi. SelectObject (Mdc, winapi. Hgdiobj (HBITMAP)     //  the 4th and 5 parameters of this function are the width and height of the picture     //  for simplicity, I wrote it right here.     //  the actual project, of course, to use the program to get a     wInapi. BitBlt (Hdc, 0, 0, 480, 640, mdc, 0, 0, winapi. srccopy)}func oncommand (Hwnd winapi. HWND, WPARAM UINTPTR, LPARAM UINTPTR)  {    //  No special treatment required for the time being  wm_command    winapi. DefWindowProc (Hwnd, winapi. Wm_command, wparam, lparam)}func myregisterclass (Hinstance winapi. HINSTANCE)   (atom uint16, err error)  {    var wc winapi. WNDCLASS    WC. STYLE = WINAPI.CS_HREDRAW | WINAPI.CS_VREDRAW    WC. PFNWNDPROC = WNDPROC    WC. CBCLSEXTRA = 0    WC. CBWNDEXTRA = 0    WC. HINSTANCE = HINSTANCE    WC. HICON = 0    WC. Hcursor, err = winapi. Loadcursorbyid (0, winapi. Idc_arrow)   &NBsp; if err != nil {        return     }    WC. Hbrbackground = winapi. COLOR_WINDOW + 1    WC. pszmenuname =  ""     WC. pszclassname =  "main window class"     WC. Hiconsmall = 0    return winapi. RegisterClass (&WC)}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.