Results 1 to 2 of 2

Thread: การสร้างหน้าต่าง Windows อย่างง่าย(มั้ง) ด้วย Win32

  1. #1
    Administrator asylu3's Avatar
    Join Date
    Jun 2000
    Location
    Thailand
    Posts
    3,557


    การสร้างหน้าต่าง Windows อย่างง่าย(มั้ง) ด้วย Win32

    อย่าพึ่งแปลกใจนะครับทำไม่ต้องสอนกันทำอะไรที่ถอยหลังเข้าคลองด้วย จริงๆอยากจะให้ลองเปลี่ยนจากการใช้ MFC แบบเดิมที่คลิ๊กไม่กี่ครั้งก็เสร็จ มาเป้นแบบ manual เขียนเองบ้าง

    ถ้ามีเวลาว่างจะมาอธิบายนะครับ

    [src]
    #include <windows.h>

    LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){

    return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,int nCMdShow){

    WNDCLASSEX wc;
    MSG msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "test";
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);


    RegisterClassEx(&wc);
    HWND hWnd=CreateWindowEx(wc.style,"test","test",wc.style,
    10,10,200,200,NULL,NULL,hInstance,NULL);

    if(hWnd == NULL)
    {
    MessageBox(NULL, "Window Creation Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }

    ShowWindow(hWnd,nCMdShow);
    UpdateWindow(hWnd);

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

    return msg.wParam;
    }

    [/src]

    จากที่เขียนกันมานาน เราก็ได้ Window อย่างมักง่าย เอ้ย อย่างง่าย (สมชื่อจริงๆ)


  2. #2
    Administrator asylu3's Avatar
    Join Date
    Jun 2000
    Location
    Thailand
    Posts
    3,557


    Re: การสร้างหน้าต่าง Windows อย่างง่าย(มั้ง) ด้วย Win32

    version 2 ง่ายกว่าเดิม เนื่องจากผมได้เขียน class ชื่อ SimpleWnd ซึ่งอยู่ใน header file SimpleW32.H เพื่อทำการ encapsulate รายละเอียดต่างๆไว้ class นี้มีรูปแบบดังนี้

    [src]
    #if !defined SIMPLEWND_H
    #define SIMPLEWND_H

    #include <windows.h>

    class SimpleWnd{

    public:
    void Show();
    SimpleWnd(HINSTANCE&,int);
    void Init();
    WNDCLASSEX wc;
    void MsgLoop();
    MSG msg;
    private:
    HWND hWnd;
    HINSTANCE Myinstance;
    int myCmdShow;



    };

    LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){

    return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    SimpleWnd::SimpleWnd(HINSTANCE &hInstance,int nCMDShow){

    myCmdShow=nCMDShow;
    Myinstance=hInstance;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "test";
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    }


    void SimpleWnd::Init(){

    RegisterClassEx(&wc);
    hWnd=CreateWindowEx(wc.style,"test","test",wc.style,
    10,10,200,200,NULL,NULL,Myinstance,NULL);

    if(hWnd == NULL)
    {
    MessageBox(NULL, "Window Creation Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);

    }


    }

    void SimpleWnd::Show(){

    ShowWindow(hWnd,myCmdShow);
    UpdateWindow(hWnd);
    }

    #endif
    [/src]

    แล้วมีรูปแบบการนำไปใช้ดังนี้
    [src]
    #include "SimpleW32.h"

    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,int nCMdShow){
    MSG msg;
    SimpleWnd obj(hInstance,nCMdShow);
    obj.Init();
    obj.Show();

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

    return msg.wParam;
    }
    [/src]

Similar Threads

  1. หาเอกสารใน Net อย่างง่าย ด้วย OutWit docs
    By frozenzombie in forum ทิปหรือเคล็ดลับการคอมพิวเตอร์ต่างๆ
    Replies: 0
    Last Post: 22-04-2009, 10:01 AM
  2. การ set path JAVA ด้วย DOS อย่างง่าย
    By chinaopsk in forum แนะความรู้ด้าน Programming ต่างๆ
    Replies: 0
    Last Post: 26-08-2008, 10:36 PM
  3. Replies: 7
    Last Post: 28-06-2008, 08:55 PM
  4. ใครมีหนังสือ maya basic, after effect มั้ง มั้ง อยากได้ครับ
    By Anonymous in forum E-Book, Video หรือบทความทั่วไปด้าน Computer
    Replies: 0
    Last Post: 20-04-2005, 06:46 PM
  5. Replies: 1
    Last Post: 07-04-2003, 04:32 AM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •