API窗口

以下爲在VC6++下編譯的API窗口源代碼,若在VS上可能會出現亂碼,

解決方案:1.在字符串前加"L"
    2.text("字符串");
    3._T("字符串");

#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
     static char szWndClassName[] = "hellowin";
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
//窗口共性
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;//窗口水平和垂直方向上均可自適應的變化
     wndclass.lpfnWndProc   = WndProc ;//與窗口過程有關====================================================
     wndclass.cbClsExtra    = 0 ;//爲窗口類額外分配的內存空間
     wndclass.cbWndExtra    = 0 ;//此處雖爲分配內存空間,但得告訴系統,否則出錯
     wndclass.hInstance     = hInstance ;//傳入實例句柄
     wndclass.hIcon /*句柄圖標,窗口小圖標*/        = LoadIcon (NULL, IDI_APPLICATION) ;//系統圖標
     wndclass.hCursor/*光標*/       = LoadCursor (NULL, IDC_ARROW/*箭頭*/) ;
     wndclass.hbrBackground/*背景顏色*/ = (HBRUSH) GetStockObject (WHITE_BRUSH/*白色*/) ;
     wndclass.lpszMenuName /*菜單*/ = NULL ; 
     wndclass.lpszClassName = szWndClassName ;//爲窗口類起一個名字======================================
     if (!RegisterClass (&wndclass/*窗口類的地址*/))
     {//註冊窗口類
          MessageBox (NULL, "註冊失敗","錯誤", MB_ICONERROR) ;
          return 0 ;
     }
//窗口句柄(參數),窗口個性
     hwnd = CreateWindow (szWndClassName,             // window class name
                          "喏~消除",  // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle,父窗口
                          NULL,                       // window menu handle,菜單
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters,額外參數
     //顯示窗口
     ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;//與上一條一般一起使用


     while (GetMessage (&msg, NULL, 0, 0))  //消息隊列
     {//取出
          TranslateMessage (&msg) ;//轉換
          DispatchMessage (&msg) ;//傳遞給Windows(統一調度)
     }
     return msg.wParam ;  //WM_QUIT,其實就是0
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{//WndProc窗口過程,操作系統調用的,==========================重點===========
HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
     switch (message)
     {
case WM_PAINT://窗口必須重繪的時候
          hdc = BeginPaint (hwnd, &ps) ;//返回設備描述表句柄
          GetClientRect (hwnd, &rect) ;         
          DrawText (hdc, TEXT ("這是一個窗口"), -1/*顯示的字節數,-1表示全部顯示*/, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER/*字在中央*/) ;
          EndPaint (hwnd, &ps) ;//
          return 0 ;
 case WM_DESTROY:
          PostQuitMessage (0) ;//發送到消息隊列中
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;//默認的處理,例如:最大化,最小化,拖動等
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章