程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> windows程序設計讀書筆記1——創建窗口,程序設計讀書筆記

windows程序設計讀書筆記1——創建窗口,程序設計讀書筆記

編輯:關於C語言

windows程序設計讀書筆記1——創建窗口,程序設計讀書筆記


第一個win32程序,簡單的創建窗口:

#include <windows.h>
 
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("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 = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello Program"), // 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) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
     
     switch (message)
     {
     case WM_CREATE:return 0 ;
          
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          
          GetClientRect (hwnd, &rect) ;
          
          DrawText (hdc, TEXT ("Hello, Windows Program!"), -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) ;
}

窗口是由窗口類創建的即WNDCLASS,窗口類確定了處理窗口消息的窗口過程。

MSDN中是這樣定義的:

typedef struct {
  UINT      style;
  WNDPROC   lpfnWndProc;
  int       cbClsExtra;
  int       cbWndExtra;
  HINSTANCE hInstance;
  HICON     hIcon;
  HCURSOR   hCursor;
  HBRUSH    hbrBackground;
  LPCTSTR   lpszMenuName;
  LPCTSTR   lpszClassName;
} WNDCLASS, *PWNDCLASS;

在程序中,我們一般這樣定義和初始化

  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 = szAppName ;

wndclass.style字段表示類的風格,具體參加MSDN:

StyleAction CS_BYTEALIGNCLIENT

Aligns the window's client area on a byte boundary (in the x direction).

This style affects the width of the window and its horizontal placement on the display.

CS_BYTEALIGNWINDOW

Aligns the window on a byte boundary (in the x direction).

This style affects the width of the window and its horizontal placement on the display.

CS_CLASSDC Allocates one device context to be shared by all windows in the class.  CS_DBLCLKS

Sends a double-click message to the window procedure when the user double-clicks the mouse

while the cursor is within a window belonging to the class.

CS_DROPSHADOW

Windows XP: Enables the drop shadow effect on a window.

The effect is turned on and off through SPI_SETDROPSHADOW. 

CS_GLOBALCLASS

Specifies that the window class is an application global class.

For more information, see Application Global Classes.

CS_HREDRAW Redraws the entire window if a movement or size adjustment changes the width of the client area. CS_NOCLOSE Disables Close on the window menu. CS_OWNDC Allocates a unique device context for each window in the class. CS_PARENTDC

Sets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent.

A window with the CS_PARENTDC style bit receives a regular device context from the system's cache of device contexts.

CS_SAVEBITS Saves, as a bitmap, the portion of the screen image obscured by a window of this class.  CS_VREDRAW Redraws the entire window if a movement or size adjustment changes the height of the client area.

我們這裡選擇的是CS_HREDRAW | CS_VREDRAW,表示無論垂直還是水平尺寸的改變,窗口都將重繪。

wndclass.lpfnWndProc表示窗口過程設置,指向一個函數WndProc。

wndclass.cbClsExtra和cbWndExtra用於維護內部窗口結構中一些預留的空間,可以根據需要來使用這些預留的空間。

wndclass.hInstance表示應用程序的實例句柄即WinMain第一個參數。

wndclass.hIcon為該窗口類設定圖標。若要使用本地的圖標,第一個參數必須為hInstance實例句柄,第二個參數為圖標標示,對於預定義的圖標以IDI開頭。

wndclass.hCursor與上面類似,載入鼠標指針。

wndclass.hbrBackground指定背景色,這裡我們用白色畫刷。

wndclass.lpszMenuName即窗口菜單。

wndclass.lpszClassName為窗口類指定名字。

這樣窗口類的初始化完成,使用RegisterClass注冊類,下面就是窗口的創建了,

使用createWindow函數,MSDN定義如下:

HWND CreateWindow(
  __in  LPCTSTR lpClassName,   //窗口名稱
  __in  LPCTSTR lpWindowName,  //窗口標題
  __in  DWORD dwStyle,      //窗口風格
  __in  int x,           //x坐標
  __in  int y,            //y坐標
  __in  int nWidth,         //長度
  __in  int nHeight,       //高度 
  __in  HWND hWndParent,     //父窗口句柄
  __in  HMENU hMenu,            //窗口菜單句柄
  __in  HINSTANCE hInstance,    //程序實例句柄
  __in  LPVOID lpParam          //創建參數
);

CreateWindow只是在內部創建窗口,即windows分配了一片內存,要想顯示窗口還要使用ShowWindow函數。

第一個參數是CreateWindow創建的窗口句柄,第二個是WinMain接受的iCmdShow值,該參數決定窗口初始顯示格式。

UpdateWindow是重繪窗口,這是通過發送一條WM_PAINT消息完成的。

最後則是消息的循環,即不斷接受消息,當有消息時調用WndProc函數進行處理。

 


windows編程 創建窗口

呵呵沒關系的都是一些小錯誤慢慢的你就會了
比如win32編程時候用全大寫字母的NULL,而不是Null,沒有這種寫法的,在C語言中用全小寫的null.
還有用hInstance而不是hinstance.
case WM_CLOSE 而不是WM_DESTORY
VREDRAW拼寫也有錯誤
下面的程序師成功通過編譯的 您可以對比看下 希望對您有所幫助:)

// WNDCLASSEX.cpp : Defines the entry point for the application.
//
#define WIN_32_LEAN_AND_MEAN
//#include "stdafx.h"
#include <windows.h>
//#include <xwindows.h>

LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(msg){
case WM_CREATE: {return 0;}break;
case WM_PAINT: {hdc=BeginPaint(hwnd,&ps);EndPaint(hwnd,&ps);return 0;}break;

//WM_DESTORY
case WM_CLOSE:{PostQuitMessage(0); return 0;}break;
default:break;}

return(DefWindowProc(hwnd,msg,wparam,lparam));}

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hwnd;
MSG msg;

WNDCLASSEX winclass;
winclass.cbSize=sizeof(WNDCLASSEX);
winclass.style=CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
winclass.lpfnWndProc=WindowProc;
winclass.cbClsExtra=0;
winclass.cbWndExtra=0;
winclass.hInstance=hInstance;
winclass.hIcon=LoadIcon(NULL,IDI_WINLOGO);
winclass.hCursor=LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName=NULL;
winclass.lpszClassName="HenryTank1";
winclass.hIconSm=LoadIcon(NULL,ID......余下全文>>
 

windows程序設計

在winmain裡用函數DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,dlgproc,NULL);
再用這個(名稱自己起,但要和DialogBoxParam裡參數一致)回調函數處理對話框消息循環
我寫的一個處理函數,你參考一下
INT_PTR CALLBACK dlgproc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
if (LOWORD(wParam) == ID_Open)
{
OpenPeFile(hwndDlg);
return (INT_PTR)TRUE;
}
if (LOWORD(wParam) == ID_Close)
{
EndDialog(hwndDlg,NULL);
return (INT_PTR)TRUE;
}
if (LOWORD(wParam) == ID_About)
{
MessageBox(NULL,"PEView1.0\nWrite by iehx!","Auout",MB_OK);
return (INT_PTR)TRUE;
}
break;

case WM_INITDIALOG:
Init(hwndDlg);

return (INT_PTR)TRUE;
break;

case WM_CLOSE:
EndDialog(hwndDlg,NULL);
break;
}
return (INT_PTR)FALSE;
}
注意與窗口消息循環有所不同
我現在很喜歡這種純api建立簡單窗口的寫法,再不懂給我發消息
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved